代码如下:
FileInputStream in = new FileInputStream(System.getProperty("user.dir")+"\\file\\测试报告.xls");
BufferedImage bufferImg = null;
try {
HSSFWorkbook work = new HSSFWorkbook(in);
HSSFSheet sheet = work.getSheetAt(0);
//图片
HSSFSheet sheet1 = work.getSheetAt(1);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
//依赖信息
HSSFRow row = sheet.getRow(7 );
row.getCell(3).setCellValue("1");
row = sheet.getRow(8);
row.getCell(3).setCellValue("2");
row=sheet.getRow(9);
row.getCell(3).setCellValue("3");
row=sheet.getRow(10);
row.getCell(3).setCellValue("4");
//追溯信息
row=sheet.getRow(7);
row.getCell(8).setCellValue("5");
row=sheet.getRow(8);
row.getCell(8).setCellValue("6");
row=sheet.getRow(9);
row.getCell(8).setCellValue("7");
row=sheet.getRow(10);
row.getCell(8).setCellValue("8");
//检测结果
row = sheet.getRow(0+13);
row.getCell(1).setCellValue("9");
row.getCell(2).setCellValue("10");
row.getCell(4).setCellValue("11");
row.getCell(6).setCellValue("12");
row.getCell(7).setCellValue("13");
row.getCell(8).setCellValue("14");
row.getCell(9).setCellValue("15");
row.getCell(10).setCellValue("16");
row.getCell(11).setCellValue("17");
row.getCell(12).setCellValue("18");
row.getCell(14).setCellValue("19");
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
FileInputStream stream = new FileInputStream(System.getProperty("user.dir")+"\\images\\test\\test.png");
bufferImg = ImageIO.read(stream);
ImageIO.write(bufferImg, "png", byteArrayOut);
//row.getCell(6).setCellValue(detection1ProjectEntity.getCharacter());
HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 0, 0,(short) 3, 3, (short) 8, 8);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
stream.close();
row =sheet.getRow(3);
row.getCell(10).setCellValue("认可");
row.getCell(12).setCellValue("确认");
row.getCell(14).setCellValue("测试");
row =sheet.getRow(4);
row.getCell(11).setCellValue("完成日期");
row.getCell(14).setCellValue("文件编号");
OutputStream out = new FileOutputStream("C:\\test\\测试-文件.xls");
work.setForceFormulaRecalculation(true);
work.write(out);
out.flush();
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
附加方法:
//创建行
public static void insertRow(HSSFWorkbook wb, HSSFSheet sheet, int starRow,int rows){
sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), rows,true,false);
starRow = starRow - 1;
for (int i = 0; i < rows; i++) {
HSSFRow sourceRow = null;
HSSFRow targetRow = null;
HSSFCell sourceCell = null;
HSSFCell targetCell = null;
short m;
starRow = starRow + 1;
sourceRow = sheet.getRow(starRow);
targetRow = sheet.createRow(starRow + 1);
targetRow.setHeight(sourceRow.getHeight());
//targetRow.setRowStyle(sourceRow.getRowStyle());
for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) {
sourceCell = sourceRow.getCell(m);
targetCell = targetRow.createCell(m);
//targetCell.setEncoding(sourceCell.getEncoding());
targetCell.setCellStyle(sourceCell.getCellStyle());
targetCell.setCellType(sourceCell.getCellType());
}
}
}
//判断是否是数字
public static boolean isNumber(String str){
Pattern pattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
//设置下划线
public HSSFRichTextString setLine(String title,String value, HSSFWorkbook wb){
Font font=wb.createFont();
font.setUnderline(Font.U_SINGLE);
HSSFRichTextString hssfRichTextString=new HSSFRichTextString(title+value);
hssfRichTextString.applyFont(title.length(),(title+value).length(),font);
return hssfRichTextString;
}
表格最终会生成sheet1对的表格内容以及sheet2的图片内容
觉得辛苦就扫一扫: