读取Excel模版插入数据并生成一个新的文件保存
本文中接受03的excel操作(跟07一样不过 其中的 HSSF 换成 XSSF)
FileInputStream fileInputStream = new FileInputStream(templeExcelPath); HSSFWorkbook xssfWorkbook = new HSSFWorkbook(fileInputStream); HSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); HSSFRow xssfRow = xssfSheet.createRow(xssfSheet.getLastRowNum() + 1); xssfRow.createCell(0).setCellValue(type);//插入类型 xssfRow.createCell(1).setCellValue(title);//插入题干 xssfRow.createCell(2).setCellValue(option);//插入选项 xssfRow.createCell(3).setCellValue(answer);//插入答案 xssfRow.createCell(4).setCellValue(remark);//插入讲解 xssfRow.createCell(5).setCellValue(complexity);//插入难度 xssfRow.createCell(6).setCellValue(score);//插入分数 HSSFCellStyle cellStyle = xssfWorkbook.createCellStyle(); HSSFFont font = xssfWorkbook.createFont(); font.setFontHeightInPoints((short) 11); cellStyle.setFont(font); cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); setCellStyle(cellStyle, xssfRow); FileOutputStream os = new FileOutputStream(exportExcelPath + fileName+".xls"); xssfWorkbook.write(os); fileInputStream.close(); os.close();
public static void setCellStyle(HSSFCellStyle cellStyle, HSSFRow row) { for (int i = 0; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); cell.setCellStyle(cellStyle); } }
时间: 2024-10-09 19:10:16