1.介绍
导出是用的poi,poi版本3.11,网上用的较多的是poi的HSSF的API,对比了一下发现,HSSF主要针对2007年以前的excel版本,XSSF主要针对2007年及以上版本。
官方主页http://poi.apache.org/index.html,
API文档http://poi.apache.org/apidocs/index.html
2.实现步骤:
1.打开或新建一个excel文件对象。(可以打开一个已做好的excel模板,也可以新建一个空白excel)
2.用文件对象打开或新建一个sheet对象。
3.用sheet获取一行或新建一行Row对象。
4.用Row获取或新建一个cell对象(单元格)。
2.上代码
public String printReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
String ss = request.getSession().getServletContext().getRealPath("/").replace("\\", "/");
DeleteFileDate(ss+"/download");
File upPath = new File(request.getSession().getServletContext().getRealPath("download"));
upPath.mkdirs();
//新建一个Excel
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个sheet
workbook.createSheet();
//获取第一个sheet
XSSFSheet xssfSheet = workbook.getSheetAt(0);
//创建一行
XSSFRow row = xssfSheet.createRow(0);
//边框样式
XSSFCellStyle styleLine = workbook.createCellStyle();
styleLine.setBorderBottom(XSSFCellStyle.BORDER_THIN);//下边框
styleLine.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
styleLine.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
styleLine.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
List<Kpi> list = this.selectAllBasicKPI();
for (int i = 0; i < list.size(); i++) {
XSSFCell cell = row.getCell(0);
if (cell == null) {
cell = row.createCell(0);
}
createRowAndCell(list.get(i).getKpiName(), row, cell, i, styleLine);
}
//获取Tomcat的根目录中的download文件夹
String path = request.getSession().getServletContext().getRealPath("download") + File.separatorChar;
String newFileName = "数据汇总.xlsx";
File newFile = new File(path, newFileName);
FileOutputStream fout =null;
try{
//下载到指定位置
fout = new FileOutputStream(newFile);
workbook.write(fout);
}catch (Exception e){
e.printStackTrace();
}finally {
if (fout !=null){
fout.close();
workbook.close();
}
}
Map<String, Object> jsonMap = new HashMap<String, Object>();
//System.out.println("导出成功");
jsonMap.put(STATUS_PARAMETER_NAME, Status.success);
jsonMap.put(MESSAGE_PARAMETER_NAME, "导出成功!");
jsonMap.put("fileAddress", "/download/"+newFile.getName());
//ajax(response, jsonMap);
return NONE;
}
/**
*根据当前row行,来创建index标记的列数,并赋值数据
*/
private void createRowAndCell(Object obj, XSSFRow row, XSSFCell cell, int index, XSSFCellStyle styleLine) {
cell = row.getCell(index);
if (cell == null) {
cell = row.createCell(index);
cell.setCellStyle(styleLine);
}
if (obj != null){
cell.setCellValue(obj.toString());
}else{
cell.setCellValue("");
}
}
/**
* 删除之前的文件
*/
public void DeleteFileDate(String savePlace) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);
// System.out.print(date);
cal.add(Calendar.DATE, -1);// 减去一天(昨天)
long yesterday = cal.getTime().getTime();// 获取时间
File file = new File(savePlace);// 文件夹路径
File[] tempList = file.listFiles();// 获取子目录
if (tempList != null){
for (File i : tempList) {
if (yesterday <= i.lastModified()) {
// log.info(i.getName() + "为当天文件,不可删除!!");//輸出在服務器
// System.out.println(i.getName() + "为当天文件,不可删除!!");// 输出在控制台
} else {
i.delete();
}
}
}
}