HSSFCell设置样式
1、遍历workbook
// load源⽂件
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fs);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
HSSFSheet sheet = wb.getSheetAt(i);
for (int j = FirstRowNum(); j < LastRowNum(); j++) {
HSSFRow row = Row(j);
if (row != null) {
// 。。。操作}
}
}
}
// ⽬标⽂件
FileOutputStream fos = new FileOutputStream(objectPath);
// 写⽂件
wb.write(fos);
fos.close();
2、得到列和单元格
HSSFRow row = Row(i);
HSSFCell cell = Cell((short) j);
3、设置sheet名称和单元格内容为中⽂
wb.setSheetName(n, "中⽂",HSSFCell.ENCODING_UTF_16);
cell.setEncoding((short) 1);
cell.setCellValue("中⽂");
4、单元格内容未公式或数值,可以这样读写
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
5、设置列宽、⾏⾼
sheet.setColumnWidth((short)column,(short)width);
row.setHeight((short)height);
6、添加区域,合并单元格
Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo,(short)columnTo); sheet.addMergedRegion(region);
//得到所有区域
7、常⽤⽅法
根据单元格不同属性返回字符串数值
public String getCellStringValue(HSSFCell cell) {
String cellValue = "";
switch (CellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellValue = StringCellValue();
if (im().equals("") || im().length() <= 0)
cellValue = " ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = String.NumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellValue = String.NumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue = " ";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
break;
case HSSFCell.CELL_TYPE_ERROR:
break;
default:
break;
}
return cellValue;
}
8、常⽤单元格边框格式
虚线HSSFCellStyle.BORDER_DOTTED
实线HSSFCellStyle.BORDER_THIN
public static HSSFCellStyle getCellStyle(short type) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(type);// 下边框
style.setBorderLeft(type);// 左边框
style.setBorderRight(type);// 右边框
style.setBorderTop(type);// 上边框
return style;
}
9、设置字体和内容位置
HSSFFont f = wb.createFont();
f.setFontHeightInPoints((short) 11);// 字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 style.setRotation(short rotation);// 单元格内容的旋转的⾓度 HSSFDataFormat df = wb.createDataFormat();
style1.Format("0.00%"));// 设置单元格数据格式
cell.setCellFormula(string);// 给单元格设公式
style.setRotation(short rotation);// 单元格内容的旋转的⾓度
cell.setCellStyle(style);
10、插⼊图⽚
// 先把读进来的图⽚放到⼀个ByteArrayOutputStream中,以便产⽣ByteArray ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); BufferedImage bufferImg = ad(new File("ok.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);htmlborder
// 读进⼀个excel模版
FileInputStream fos = new FileInputStream(filePathName + "/stencil.xlt");
fs = new POIFSFileSystem(fos);
// 创建⼀个⼯作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = ateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 10, 10);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论