List<Map<String, String>> labelInformations = new ArrayList();
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表sheeet
HSSFSheet sheet = workbook.createSheet();
HSSFCellStyle cellStyle = workbook.createCellStyle();
// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
HSSFFont font = workbook.createFont(); //设置字体的样式
font.setFontHeightInPoints((short)11); //字体大小只能是整数
//如果想将字体变成11.5
font.setFontHeight((short)(11.5*20));//这才是11.5号字体
//font.setBold(true);//加粗
//font.setFontName("simsun"); //字体
//font.setFontName("微软雅黑"); //字体
font.setFontName("宋体"); //字体
cellStyle.setFont(font);
// sheet.setColumnWidth(0, 256*width+184);
sheet.setColumnWidth(0, 27 * 256 + 184);
sheet.setColumnWidth(1, 600);
sheet.setColumnWidth(2, 27 * 256 + 184);
sheet.setColumnWidth(3, 600);
sheet.setColumnWidth(4, 27 * 256 + 184);
// sheet.setDefaultRowHeightInPoints((float) 20*256);无效
// cell.setCellStyle(cellStyle);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 追加数据
for (int i = 0; i <= labelInformations.size() / 3; i++) {
HSSFRow row1 = sheet.createRow(3 * i);
HSSFRow row2 = sheet.createRow(3 * i + 1);
HSSFRow row3 = sheet.createRow(3 * i + 2);
row1.setHeightInPoints((float) 20);
row2.setHeightInPoints((float) 20);
row3.setHeightInPoints((float) 12);
for (int a = 0; a < 3; a++) {
if (3 * i + a >= labelInformations.size())
break;
HSSFCell cell1 = row1.createCell(a * 2);
HSSFCell cell2 = row2.createCell(a * 2);
HSSFCell cell3 = row3.createCell(a * 2);
cell1.setCellStyle(cellStyle);
cell2.setCellStyle(cellStyle);
cell3.setCellStyle(workbook.createCellStyle());
Map<String, String> labelInformation = labelInformations.get(3 * i + a);
// 添加图片-
String barcodepath = SHORTBARCODE_PATH + "noNumber" + labelInformation.get("product_code") + ".png";
BarcodeUtil.generateByCODE128(labelInformation.get("product_code"), barcodepath, 0.4, 10, 0);
HSSFClientAnchor anchor = new HSSFClientAnchor(650, 80, 990, 200, (short) (2 * a), 3 * i,
(short) (2 * a), 3 * i);
BufferedImage bufferImg = ImageIO.read(new File(barcodepath));
ImageIO.write(bufferImg, "png", byteArrayOut);
byte[] imgtypes = byteArrayOut.toByteArray();
int puctureIndex = workbook.addPicture(imgtypes, Workbook.PICTURE_TYPE_PNG);
patriarch.createPicture(anchor, puctureIndex);
cell1.setCellValue(
labelInformation.get("sterilized_lot_number") + "/" + labelInformation.get("product_code"));
cell2.setCellValue(
labelInformation.get("bracket_model") + " " + labelInformation.get("bracket_spec"));
}
}
// 创建一个文件
String timestamp = System.currentTimeMillis() + "";
File file = new File(IDENTIFICATIONBAR_PATH + timestamp + ".xls");
file.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
workbook.write(stream);
stream.close();
poi4设置字体格式
最新推荐文章于 2024-04-09 17:28:47 发布