使用Spring MVC生成Excel文件 .
阿新 • • 發佈:2019-01-04
{
//設定response方式,使執行此controller時候自動出現下載頁面,而非直接使用excel開啟
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition",
"attachment; filename=""+"excel.xls"+""");
//構造資料
Student stu1=new Student("gaoxiang1","male1 ","20060101",1);
Student stu2=new Student("gaoxiang2","male2","20060102",2);
Student stu3=new Student("gaoxiang3","male3","20060103",3);
Student stu4=new Student("gaoxiang4","male4","20060104",4);
Student stu5=new Student("gaoxiang5","male5","20060105",5);
ArrayList stuList=new ArrayList();
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
//產生Excel表頭
HSSFSheet sheet=workbook.createSheet("studentList");
HSSFRow header=sheet.createRow(0); //第0行
//產生標題列
header.createCell((short )0).setCellValue("name");
header.createCell((short)1).setCellValue("sex");
header.createCell((short)2).setCellValue("date");
header.createCell((short)3).setCellValue("count");
HSSFCellStyle cellStyle=workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
//填充資料
int rowNum=1;
for (Iterator iter = stuList.iterator(); iter.hasNext();) ...{
Student element = (Student) iter.next();
HSSFRow row=sheet.createRow(rowNum++);
row.createCell((short)0).setCellValue(element.getName().toString());
row.createCell((short)1).setCellValue(element.getSex().toString());
row.createCell((short)2).setCellValue(element.getDate().toString());
row.getCell((short)2).setCellStyle(cellStyle);
row.createCell((short)3).setCellValue(element.getCount());
}
//列總和計算
HSSFRow row=sheet.createRow(rowNum);
row.createCell((short)0).setCellValue("TOTAL:");
String formual="SUM(D2:D"+rowNum+")"; //D2到D[rowNum]單元格起(count資料)
row.createCell((short)3).setCellFormula(formual);
}
//設定response方式,使執行此controller時候自動出現下載頁面,而非直接使用excel開啟
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition",
"attachment; filename=""+"excel.xls"+""");
//構造資料
Student stu1=new Student("gaoxiang1","male1
Student stu2=new Student("gaoxiang2","male2","20060102",2);
Student stu3=new Student("gaoxiang3","male3","20060103",3);
Student stu4=new Student("gaoxiang4","male4","20060104",4);
Student stu5=new Student("gaoxiang5","male5","20060105",5);
ArrayList stuList=new ArrayList();
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
//產生Excel表頭
HSSFSheet sheet=workbook.createSheet("studentList");
HSSFRow header=sheet.createRow(0); //第0行
//產生標題列
header.createCell((short
header.createCell((short)1).setCellValue("sex");
header.createCell((short)2).setCellValue("date");
header.createCell((short)3).setCellValue("count");
HSSFCellStyle cellStyle=workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
//填充資料
int rowNum=1;
for (Iterator iter = stuList.iterator(); iter.hasNext();) ...{
Student element = (Student) iter.next();
HSSFRow row=sheet.createRow(rowNum++);
row.createCell((short)0).setCellValue(element.getName().toString());
row.createCell((short)1).setCellValue(element.getSex().toString());
row.createCell((short)2).setCellValue(element.getDate().toString());
row.getCell((short)2).setCellStyle(cellStyle);
row.createCell((short)3).setCellValue(element.getCount());
}
//列總和計算
HSSFRow row=sheet.createRow(rowNum);
row.createCell((short)0).setCellValue("TOTAL:");
String formual="SUM(D2:D"+rowNum+")"; //D2到D[rowNum]單元格起(count資料)
row.createCell((short)3).setCellFormula(formual);
}