POI生成Excel表格
一、生成Excel表格的步骤
- 生成Excel表格的文件名;
- 创建文件;
// 生成文件目录
File file = new File(request.getSession().getServletContext().getRealPath("/") + “/download/”);
// 生成文件名
File excelFile = new File(request.getSession().getServletContext().getRealPath("/") + “/download/” + fileName);
- 创建Excel工作簿,设置Excel样式;
// 输出路径
FileOutputStream fileOut = new FileOutputStream(excelFile);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle(); // 样式对象style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框HSSFFont f = wb.createFont();
f.setFontName(“宋体”);
f.setFontHeightInPoints((short) 10);// 字号style.setFont(f);
- 生成sheet
HSSFSheet sheet = wb.createSheet(examPrintService.correctFileName(examName+“分析表”));
sheet.setColumnWidth(0, 6500);
- 生成行
HSSFRow row = sheet.createRow((short) 0);
- 生成每一行中的每一个小单元格
HSSFCell ce = row.createCell((short) 0);
- 导出文件
wb.write(fileOut);
fileOut.close();
注:其中addMergedRegion可以将单元格进行合并处理。
示例
// 生成Excel表格
public void printExamTopicInfo(List<Map<String, Object>> printData,HttpServletRequest request)throws SystemException{
1.生成Excel表格的文件名
String fileName = "测试.xls";
DecimalFormat df = new DecimalFormat("0.00");//格式化小数
try {
// 创建文件目录
File file = new File(request.getSession().getServletContext().getRealPath("/") + "/download/");
if (!file.exists()) {
file.mkdirs();
}
// 创建文件
File excelFile = new File(request.getSession().getServletContext().getRealPath("/") + "/download/" + fileName);
if (!excelFile.exists()) {
excelFile.createNewFile();
}
// 输出路径
FileOutputStream fileOut = new FileOutputStream(excelFile);
// 创建excel工作簿,设置excel样式
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
HSSFFont f = wb.createFont();
f.setFontName("宋体");
f.setFontHeightInPoints((short) 10);// 字号
style.setFont(f);
HSSFSheet sheet = wb.createSheet(examPrintService.correctFileName(examName+"分析表"));
sheet.setColumnWidth(0, 6500);
HSSFRow row = sheet.createRow((short) 0);
HSSFCell ce = row.createCell((short) 0);
ce.setCellValue("学校");
ce.setCellStyle(style);
HSSFCell ce1 = row.createCell((short) 1);
ce1.setCellValue("应考人数");
ce1.setCellStyle(style);
HSSFCell ce2 = row.createCell((short) 2);
ce2.setCellValue("");
ce2.setCellStyle(style);
HSSFCell ce3 = row.createCell((short) 3);
ce3.setCellValue("");
ce3.setCellStyle(style);
HSSFCell ce4 = row.createCell((short) 4);
ce4.setCellValue("");
ce4.setCellStyle(style);
if("1".equals(pubFlag)){
HSSFCell ce5 = row.createCell((short) 5);
ce5.setCellValue("平均等级");
ce5.setCellStyle(style);
}else{
HSSFCell ce5 = row.createCell((short) 5);
ce5.setCellValue("平均分");
ce5.setCellStyle(style);
}
HSSFCell ce6 = row.createCell((short) 6);
ce6.setCellValue("合格率");
ce6.setCellStyle(style);
HSSFRow row1 = sheet.createRow((short) 1);
HSSFCell ce10 = row1.createCell((short) 0);
ce10.setCellValue("");
ce.setCellStyle(style);
HSSFCell ce11 = row1.createCell((short) 1);
ce11.setCellValue("");
ce11.setCellStyle(style);
HSSFCell ce12 = row1.createCell((short) 2);
ce12.setCellValue("");
ce12.setCellStyle(style);
HSSFCell ce13 = row1.createCell((short) 3);
ce13.setCellValue("人数");
ce13.setCellStyle(style);
HSSFCell ce14 = row1.createCell((short) 4);
ce14.setCellValue("%");
ce14.setCellStyle(style);
HSSFCell ce15 = row1.createCell((short) 5);
ce15.setCellValue("");
ce15.setCellStyle(style);
HSSFCell ce16 = row1.createCell((short) 6);
ce16.setCellValue("");
ce16.setCellStyle(style);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 4));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6));
int rowNew = 2;
for(int i=0; i<printData.size(); i++){
row = sheet.createRow(rowNew);
HSSFCell cell = row.createCell(0);
HSSFCell cell1 = row.createCell(1);
HSSFCell cell2 = row.createCell(2);
HSSFCell cell3 = row.createCell(3);
HSSFCell cell4 = row.createCell(4);
HSSFCell cell5 = row.createCell(5);
HSSFCell cell6 = row.createCell(6);
if(printData.get(i).get("dept_name") != null){
cell.setCellValue(printData.get(i).get("dept_name")==null?"":printData.get(i).get("dept_name").toString());
}else{
cell.setCellValue("");
}
cell.setCellStyle(style);
int yCount = Integer.parseInt(printData.get(i).get("count_stu_normal").toString())
+ Integer.parseInt(printData.get(i).get("count_without_stu").toString())
+ Integer.parseInt(printData.get(i).get("count_special_stu").toString());
cell1.setCellValue(yCount);
cell1.setCellStyle(style);
cell2.setCellValue(printData.get(i).get("count_stu_normal").toString());
cell2.setCellStyle(style);
cell3.setCellValue(printData.get(i).get("ratio4").toString());
cell3.setCellStyle(style);
int d1 = Integer.parseInt(printData.get(i).get("ratio4").toString());
int a2 = Integer.parseInt(printData.get(i).get("count_stu_normal").toString());
String num4 = df.format(((float)d1/a2)*100);//返回的是String类型
cell4.setCellValue(num4);
cell4.setCellStyle(style);
// 平均分
if("1".equals(pubFlag)){
cell5.setCellValue(printData.get(i).get("score_avg_level")+"");
cell5.setCellStyle(style);
}else{
cell5.setCellValue(printData.get(i).get("score_avg")+"");
cell5.setCellStyle(style);
}
// 合格率
double passRatio = 100 - Double.parseDouble(num4);
cell6.setCellValue(passRatio);
cell6.setCellStyle(style);
rowNew ++;
}
wb.write(fileOut);
fileOut.close();
}catch (IOException e) {
e.printStackTrace();
}
}
上一篇: Apache POI生成Excel
下一篇: poi生成Excel文件