【javaWeb】Struts 2导出excel文件

一、功能:将界面table数据,导出为excel文件保存

二、具体实现:

2.1 jsp

序号 书名 作者 价格
1 科幻世界 匿名 10
2 语文 驾教育出版社 23.5
3 落花 张三 10.5

注:button: <input type="button" value="导出excel" onclick="exportExcel()">

确定 引入调用的正确 js文件

2.2 js

function exportExcel(){
    alert("导出"); 
    var archiveaccount = document.forms['arcAccount'];
    archiveaccount.submit();
}

2.3 action

 

public String exportExcel() throws Exception {
        List<List<Object>> data = getExportData();    //将表格数据转换为list,此方法省略
        String fn = new String(("ArchiveAccount"+new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())).getBytes(), "ISO-8859-1")+ ".xls";    //设置导出文件名
        if(createExcel(fn, data, "统计结果")){
            return "success";
        }
           return "init";
    }

 

 

   /**
     * 创建新的Excel 工作簿
     * */
    public boolean createExcel(String filename, List<List<Object>> data,String sheetName) {
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        HSSFRow[] rows = null;
        HSSFCell[][] cells = null;
        workbook = new HSSFWorkbook();
        if (sheetName.trim().equals("")) {
            sheet = workbook.createSheet("表");
        } else {
            sheet = workbook.createSheet(sheetName);
        }
        try {
            int row = 0;
            int col = 0;
            if (data.size() > 0) {
                row = data.size();
                col = data.get(0).size();
            }
            rows = new HSSFRow[row];
            cells = new HSSFCell[row][col];
            for (int i = 0; i < data.size(); i++) {
                rows[i] = sheet.createRow((short) i);
                for (int j = 0; j < data.get(i).size(); j++) {
                    cells[i][j] = rows[i].createCell((short) j);
                    cells[i][j].setCellType(HSSFCell.CELL_TYPE_STRING);
                    if (data.get(i).get(j) != null
                            && !data.get(i).get(j).toString().trim().equals("")) {
                        cells[i][j].setCellValue(data.get(i).get(j).toString()
                                .trim());
                    } else {
                        cells[i][j].setCellValue("");
                    }
                }
            }
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
            HttpUitls.getResponse().setHeader("Content-disposition","attachment; filename=" +filename);
            workbook.write(baos);
            byte[] ba = baos.toByteArray();  
            excelStream = new ByteArrayInputStream(ba);  
            return true;
        } catch (Exception e) {
            return false;
        }
    }

 

2.4 struts.xml

 

 <action name="*ArchiveAccount" class="com.personalize.action.archiveaccount.ArchiveAccountAction" method="{1}">
            <result name="init">jspNew/archiveaccount/ArchiveAccount.jsp</result>
             <result name="success" type="stream">
                    <param name="contentType">application/vnd.ms-excel</param>     <!-- 注意这里的ContentType -->
                    <param name="contentDisposition">attachment;filename="${fileName}"</param> <!--  导出文件名 -->
                    <param name="inputName">excelStream</param> <!-- 这里需要和Action里的变量名一致 -->
                    <param name="bufferSize">4096</param>
             </result>
        </action>

三、执行效果

点击导出弹出文件下载对话框,点击保存,弹出选择路径对话框,选择路径点击保存即可

【javaWeb】Struts 2导出excel文件

你可能感兴趣的:(java,html,struts)