row = sheet.createRow(i+1); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setWrapText(true); row.setRowStyle(cellStyle);
制定某一列的宽度:需要是256的倍数
默认的宽度,不需要,很奇怪
// 第一步,创建一个webbook,对应一个Excel文件 if(wb == null){ wb = new HSSFWorkbook(); } // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); sheet.setDefaultColumnWidth(20); sheet.setColumnWidth(0,5120); sheet.setColumnWidth(1,5120); sheet.setColumnWidth(2,25600); sheet.setColumnWidth(3,10240); sheet.setColumnWidth(4,2560); sheet.setColumnWidth(5,2560); sheet.setColumnWidth(6,2560); sheet.setColumnWidth(7,2560); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 //设置超链接的字体和颜色 hlink_style = wb.createCellStyle(); HSSFFont hlink_font = wb.createFont(); hlink_font.setUnderline(HSSFFont.U_SINGLE); hlink_font.setColor(HSSFColor.BLUE.index); hlink_style.setFont(hlink_font); //设置大的标题的字体 bigFontstyle = wb.createCellStyle(); HSSFFont big_font = wb.createFont(); big_font.setFontHeight((short)256); bigFontstyle.setFont(big_font); //设置大的红色标题的字体 bigRedFontStyle = wb.createCellStyle(); HSSFFont big_red_font = wb.createFont(); big_red_font.setFontHeight((short)256); big_red_font.setColor(HSSFColor.RED.index); bigRedFontStyle.setFont(big_red_font); //绿色的working标记的字体 greenFontStyle = wb.createCellStyle(); HSSFFont green_font = wb.createFont(); green_font.setColor(HSSFColor.GREEN.index); greenFontStyle.setFont(green_font); //红色的working标记的字体 redFontStyle = wb.createCellStyle(); HSSFFont red_font = wb.createFont(); red_font.setColor(HSSFColor.RED.index); redFontStyle.setFont(red_font); HSSFCell cell = null; //创建标题 for(int i=0;i<title.length;i++){ cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); }
参考:
http://www.educity.cn/wenda/371805.html
HSSFCell 设立样式
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 = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {
HSSFRow row = sheet.getRow(j);
if (row != null) {
// 。。。操作}
}
}
}
// 目标文件
FileOutputStream fos = new FileOutputStream(objectPath);
// 写文件
wb.write(fos);
fos.close();
2、得到列和单元格
HSSFRow row = sheet.getRow(i);
HSSFCell cell = row.getCell((short) j);
3、设置sheet名称和单元格内容为中文
wb.setSheetName(n, "中文",HSSFCell.ENCODING_UTF_16);
cell.setEncoding((short) 1);
cell.setCellValue("中文");
4、单元格内容未公式或数值,可以这样读写
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.getNumericCellValue();
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);
//得到所有区域
sheet.getNumMergedRegions() ;
7、常用方法
根据单元格不同属性返回字符串数值
public String getCellStringValue(HSSFCell cell) {
String cellValue = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
if (cellValue.trim().equals("") || cellValue.trim().length() <= 0)
cellValue = " ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellValue = String.valueOf(cell.getNumericCellValue());
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.setDataFormat(df.getFormat("0.00%"));// 设置单元格数据格式
cell.setCellFormula(string);// 给单元格设公式
style.setRotation(short rotation);// 单元格内容的旋转的角度
cell.setCellStyle(style);
10、插入图片
// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
// 读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName + "/stencil.xlt");
fs = new POIFSFileSystem(fos);
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 10, 10);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
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 = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {
HSSFRow row = sheet.getRow(j);
if (row != null) {
// 。。。操作}
}
}
}
// 目标文件
FileOutputStream fos = new FileOutputStream(objectPath);
// 写文件
wb.write(fos);
fos.close();
2、得到列和单元格
HSSFRow row = sheet.getRow(i);
HSSFCell cell = row.getCell((short) j);
3、设置sheet名称和单元格内容为中文
wb.setSheetName(n, "中文",HSSFCell.ENCODING_UTF_16);
cell.setEncoding((short) 1);
cell.setCellValue("中文");
4、单元格内容未公式或数值,可以这样读写
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.getNumericCellValue();
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);
//得到所有区域
sheet.getNumMergedRegions() ;
7、常用方法
根据单元格不同属性返回字符串数值
public String getCellStringValue(HSSFCell cell) {
String cellValue = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
if (cellValue.trim().equals("") || cellValue.trim().length() <= 0)
cellValue = " ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellValue = String.valueOf(cell.getNumericCellValue());
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.setDataFormat(df.getFormat("0.00%"));// 设置单元格数据格式
cell.setCellFormula(string);// 给单元格设公式
style.setRotation(short rotation);// 单元格内容的旋转的角度
cell.setCellStyle(style);
10、插入图片
// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
// 读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName + "/stencil.xlt");
fs = new POIFSFileSystem(fos);
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 10, 10);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow