java导出excel文件(或导出csv文件)

封装的一个简单的导出excel的方法,很整洁简单

 

javaweb导出excel方法,后台controller:



  @Action("exportVpn")
public void exportVpn() throws IOException {

    //list数据,我这里放的是map,有序的linkedHashMap,如果用的是实体类要自己改下
List<LinkedHashMap> list = new ArrayList<LinkedHashMap>();
LinkedHashMap linkedHashMap = new LinkedHashMap();
linkedHashMap.put("a", "1111");
linkedHashMap.put("b", "2222");
linkedHashMap.put("c", "3333");
list.add(linkedHashMap);

//表头所有栏的名称
StringBuffer sbExceHeader = new StringBuffer();
sbExceHeader.append("资源名称,");
sbExceHeader.append("资源描述,");
sbExceHeader.append("资源类型,");
sbExceHeader.append("协议类型,");
sbExceHeader.append("应用程序路径,");
sbExceHeader.append("资源组名称,");
sbExceHeader.append("地址信息,");
sbExceHeader.append("允许的客户端");
String[] excelHeader = sbExceHeader.toString().split(",");

//创建工作表,并写入数据到工作表wb
HSSFWorkbook wb = myExportExcel(list, excelHeader, "rclist");

    String fileName = "test123.xlsx";//导出的excel文件名
    
    //response怎么获取都行,只要能拿到,我们公司这是封装好的用ServletActionContent全局调用出response
HttpServletResponse response = ServletActionContext.getResponse();//获取response,并设置response格式
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+fileName);//文件名
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();

}












/*
*导出excel,参数list有序map数据,表头栏名称,sheet表名*/ private HSSFWorkbook myExportExcel(List<LinkedHashMap> list, String[] excelHeader, String sheetName){ // String[] excelHeader = { "Sno", "Name", "Age"};//表头 HSSFWorkbook wb = new HSSFWorkbook(); if(UtilFunc.isBlank(sheetName)){ sheetName = "sheet1"; } HSSFSheet sheet = wb.createSheet(sheetName);//设备sheet表名称 HSSFRow row = sheet.createRow((int) 0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); for (int i = 0; i < excelHeader.length; i++) {//循环表头 HSSFCell cell = row.createCell(i); cell.setCellValue(excelHeader[i]); cell.setCellStyle(style); sheet.autoSizeColumn(i); // sheet.SetColumnWidth(i, 100 * 256); } //==============================循环写入数据========================== for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 1); // Student student = list.get(i); // row.createCell(0).setCellValue("111"); // row.createCell(1).setCellValue("222"); // row.createCell(2).setCellValue("333"); int column = 0;//循环每列数据 LinkedHashMap<String, String> mParam = (LinkedHashMap) list.get(i); for (String key:mParam.keySet()) { String value = mParam.get(key);// row.createCell(column).setCellValue(value); column++; } } return wb; }

 

 

web页面怎么调用呢,直接用get请求就可以了,连ajax都不用

//点击按钮,触发方法
function exportExcel() {
    
    location.href ="${rootPath}/park/network/exportVpn.action";

}

 

 

》》》》》》》》》》》》》》下面是导出*.csv文件》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

写了一个通用的导出csv文件方法:(注意导出csv文件与excel有点不同,导出csv用CsvWriter这个类,而excel用HSSFWorkbook)

这个方法是不包含表头的,用cw.write直接写入一行字符串用,号分隔,每个逗号就是一个单元格,换行可直接在字符串中用\n,不过用list更直接,list中的每个对象就是一行数据

/**
     * csv Write line
     *
     * @param csvLine
     *            csv line
     * @throws IOException IOException
     */
    private void exportCsvFile(List<String> csvLine) throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/csv;charset=GBK");
        response.setHeader("Content-Disposition", "inline; filename=repost.csv");


//        List<String> csvList2 = new ArrayList<String>();//测试数据
//        csvList2.add("测试111,B111");
//        csvList2.add("测试222,B222");
//        csvList2.add("测试333,B333");

        //创建csv对象:javaweb页面导出csv文件,如果是java直接生成指定地址用这个:CsvWriter cw = new CsvWriter(new PrintWriter(file));
        CsvWriter cw = new CsvWriter(response.getWriter());
        cw.write(csvLine);
        cw.flush();
        cw.close();


    }

 

posted @ 2020-09-25 09:57  下课后我要去放牛  阅读(2102)  评论(0编辑  收藏  举报