try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
sheet.setColumnWidth(0,(short)(381 * 40 ));
row.setHeight((short) (156.75 * 20 ));
cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_TOP); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行
cell.setCellStyle(cellStyle);
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 24); // 字体高度
font.setFontName("宋体"); // 字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
HSSFFont font1 = workbook.createFont();
font1.setFontHeightInPoints((short) 8); // 字体高度
font1.setFontName("宋体"); // 字体
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
HSSFRichTextString ts= new HSSFRichTextString(" 入库标签\r\n 物料名称:火灾报警探头探测器(含底座、过渡板;电气柜内安装)\r\n发运计划号:20130731-01-01\r\n\r\n 图号:CCDZ120A-220-001\r\n\r\nSAP物料号:CNR0000009938\r\n\r\n 项目名称:上海地铁6号线增购车\r\n\r\n 数量:100");
ts.applyFont(0,10,font);
ts.applyFont(10,ts.length(),font1);
cell.setCellValue(ts);
FileOutputStream fOut = new FileOutputStream("d:\\test.xls");
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println("文件生成...");
}
poi 一个单元格不同字体
最新推荐文章于 2022-10-16 13:37:34 发布