POI下载excel文件

 
 
package com.bwton.infotech.tram.utils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Map; public class ExcelUtil { public static String NO_DEFINE = "no_define";//未定义的字段 public static String DEFAULT_DATE_PATTERN="yyyy年MM月dd日";//默认日期格式 public static int DEFAULT_COLOUMN_WIDTH = 17; /** * 导出Excel 97(.xls)格式 ,少量数据 * @param title 标题行 * @param headMap 属性-列名 * @param jsonArray 数据集 * @param datePattern 日期格式,null则用默认日期格式 * @param colWidth 列宽 默认 至少17个字节 * @param out 输出流 */ public static void exportExcel(String title, Map headMap, JSONArray jsonArray, String datePattern, int colWidth, OutputStream out) { if(datePattern==null) datePattern = DEFAULT_DATE_PATTERN; // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); workbook.createInformationProperties(); workbook.getDocumentSummaryInformation().setCompany("*****公司"); SummaryInformation si = workbook.getSummaryInformation(); si.setAuthor("JACK"); //填加xls文件作者信息 si.setApplicationName("导出程序"); //填加xls文件创建程序信息 si.setLastAuthor("最后保存者信息"); //填加xls文件最后保存者信息 si.setComments("JACK is a programmer!"); //填加xls文件作者信息 si.setTitle("POI导出Excel"); //填加xls文件标题信息 si.setSubject("POI导出Excel");//填加文件主题信息 si.setCreateDateTime(new Date()); //表头样式 HSSFCellStyle titleStyle = workbook.createCellStyle(); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont titleFont = workbook.createFont(); titleFont.setFontHeightInPoints((short) 20); titleFont.setBoldweight((short) 700); titleStyle.setFont(titleFont); // 列头样式 HSSFCellStyle headerStyle = workbook.createCellStyle(); //headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont headerFont = workbook.createFont(); headerFont.setFontHeightInPoints((short) 12); headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setFont(headerFont); // 单元格样式 HSSFCellStyle cellStyle = workbook.createCellStyle(); //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont cellFont = workbook.createFont(); cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); cellStyle.setFont(cellFont); // 生成一个(带标题)表格 HSSFSheet sheet = workbook.createSheet(); // 声明一个画图的顶级管理器 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 定义注释的大小和位置,详见文档 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); // 设置注释内容 comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.setAuthor("JACK"); //设置列宽 int minBytes = colWidth iter = headMap.keySet().iterator(); iter .hasNext();) { String fieldName = iter.next(); properties[ii] = fieldName; headers[ii] = fieldName; int bytes = fieldName.getBytes().length; arrColWidth[ii] = bytes < minBytes ? minBytes : bytes; sheet.setColumnWidth(ii,arrColWidth[ii]*256); ii++; } // 遍历集合数据,产生数据行 int rowIndex = 0; for (Object obj : jsonArray) { if(rowIndex == 65535 || rowIndex == 0){ if ( rowIndex != 0 ) sheet = workbook.createSheet();//如果数据超过了,则在第二页显示 HSSFRow titleRow = sheet.createRow(0);//表头 rowIndex=0 titleRow.createCell(0).setCellValue(title); titleRow.getCell(0).setCellStyle(titleStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1)); HSSFRow headerRow = sheet.createRow(1); //列头 rowIndex =1 for(int i=0;i headMap, JSONArray jsonArray, String datePattern, int colWidth, OutputStream out) { if(datePattern==null) { datePattern = DEFAULT_DATE_PATTERN; } // 声明一个工作薄 SXSSFWorkbook workbook = new SXSSFWorkbook(1000);//缓存 workbook.setCompressTempFiles(true); //表头样式 CellStyle titleStyle = workbook.createCellStyle(); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); Font titleFont = workbook.createFont(); titleFont.setFontHeightInPoints((short) 20); titleFont.setBoldweight((short) 700); titleStyle.setFont(titleFont); // 列头样式 CellStyle headerStyle = workbook.createCellStyle(); //headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); Font headerFont = workbook.createFont(); headerFont.setFontHeightInPoints((short) 12); headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setFont(headerFont); // 单元格样式 CellStyle cellStyle = workbook.createCellStyle(); //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); Font cellFont = workbook.createFont(); cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); cellStyle.setFont(cellFont); // 生成一个(带标题)表格 SXSSFSheet sheet = workbook.createSheet(); //设置列宽 int minBytes = colWidth iter = headMap.keySet().iterator(); iter .hasNext();) { String fieldName = iter.next(); properties[ii] = fieldName; headers[ii] = headMap.get(fieldName); int bytes = fieldName.getBytes().length; arrColWidth[ii] = bytes < minBytes ? minBytes : bytes; sheet.setColumnWidth(ii,arrColWidth[ii]*256); ii++; } // 遍历集合数据,产生数据行 int rowIndex = 0; for (Object obj : jsonArray) { if(rowIndex == 65535 || rowIndex == 0){ if ( rowIndex != 0 ) sheet = workbook.createSheet();//如果数据超过了,则在第二页显示 SXSSFRow titleRow = sheet.createRow(0);//表头 rowIndex=0 titleRow.createCell(0).setCellValue(title); titleRow.getCell(0).setCellStyle(titleStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1)); SXSSFRow headerRow = sheet.createRow(1); //列头 rowIndex =1 for(int i=0;i headMap, JSONArray ja, String dataPattern, HttpServletResponse response){ try { ByteArrayOutputStream os = new ByteArrayOutputStream(); ExcelUtil.exportExcelX(title,headMap,ja,dataPattern,0,os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面 response.reset(); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename="+ new String((title + ".xlsx").getBytes(), "iso-8859-1")); response.setContentLength(content.length); //设置cookie, 必须 Cookie fileDownload=new Cookie("fileDownload", "true"); fileDownload.setPath("/"); response.addCookie(fileDownload); ServletOutputStream outputStream = response.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(outputStream); byte[] buff = new byte[8192]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); outputStream.flush(); outputStream.close(); }catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { /*int count = 100000; JSONArray ja = new JSONArray(); for(int i=0;i<100000;i++){ Student s = new Student(); s.setName("POI"+i); s.setAge(i); s.setBirthday(new Date()); s.setHeight(i); s.setWeight(i); s.setSex(i/2==0?false:true); ja.add(s); } Map headMap = new LinkedHashMap(); headMap.put("name","姓名"); headMap.put("age","年龄"); headMap.put("birthday","生日"); headMap.put("height","身高"); headMap.put("weight","体重"); headMap.put("sex","性别"); String title = "测试"; *//* OutputStream outXls = new FileOutputStream("E://a.xls"); System.out.println("正在导出xls...."); Date d = new Date(); ExcelUtil.exportExcel(title,headMap,ja,null,outXls); System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d.getTime())+"ms"); outXls.close();*//* // try { OutputStream outXlsx = new FileOutputStream("E://b.xlsx"); System.out.println("正在导出xlsx...."); Date d2 = new Date(); ExcelUtil.exportExcelX(title,headMap,ja,null,0,outXlsx); System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d2.getTime())+"ms"); outXlsx.close(); } catch (IOException e) { e.printStackTrace(); }*/ } public static void test(HttpServletResponse response){ /*int count = 10000; JSONArray ja = new JSONArray(); for(int i=0;i<10000;i++){ Student s = new Student(); s.setName("POI"+i); s.setAge(i); s.setBirthday(new Date()); s.setHeight(i); s.setWeight(i); s.setSex(i/2==0?false:true); ja.add(s); } Map headMap = new LinkedHashMap(); headMap.put("name","姓名"); headMap.put("age","年龄"); headMap.put("birthday","生日"); headMap.put("height","身高"); headMap.put("weight","体重"); headMap.put("sex","性别"); String title = "测试"; *//* OutputStream outXls = new FileOutputStream("E://a.xls"); System.out.println("正在导出xls...."); Date d = new Date(); ExcelUtil.exportExcel(title,headMap,ja,null,outXls); System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d.getTime())+"ms"); outXls.close();*//* // try { OutputStream outXlsx = new FileOutputStream("E://b.xlsx"); System.out.println("正在导出xlsx...."); Date d2 = new Date(); ExcelUtil.exportExcelX(title,headMap,ja,null,0,outXlsx); System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d2.getTime())+"ms"); outXlsx.close(); } catch (IOException e) { e.printStackTrace(); }*/ } }

function bindingExport(url) {
	/*导出excle*/
    $("#exportData").unbind();
	$("#exportData").click(function () {
		layer.closeAll('loading');
		var index = layer.load(1, {
			shade: [0.5, '#000'] //0.5透明度的黑色背景
		});
		$.fileDownload(url, {
			httpMethod: 'POST',
			data: [],
			successCallback: function() {
				layer.closeAll('loading');
			},
			failCallback: function(responseHtml, url) {
				layer.msg('加载中..');
			}
		});
	})
}

使用ajax异步下载excel时 用 jquery.fileDownload.js 插件 必须在response设置cookie, 下载函数中已经设置.

jar包是:
 

你可能感兴趣的:(前端,后端)