[JSP] - [导出EXCEL]

在做JSP导出某个列表内容到Excel表格的时候,碰到这么个情况:导出的表格用office07修改后发给别人,接收人用03打开显示为空。

 

解决方案:重新编写了该导出功能的JSP,代码如下:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List,java.io.*,jxl.*,jxl.write.*"%>
<%    
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment;filename="
            + new String("导出示例".getBytes("GBK"), "iso8859-1") + ".xls");
    List demoList = (List) request.getAttribute("demoList");
    OutputStream os = response.getOutputStream();
    try {
        WritableWorkbook wwb = Workbook.createWorkbook(os);
        String[] exportFieldsName = { "导出字段1", "导出字段2", "导出字段3", "导出字段4", ...... };
        int cols = 0;
        if (exportFieldsName != null) {
            cols = exportFieldsName.length;
        }
        Label label = null;
        WritableSheet sheet = null;
        int sheetnum = demoList.size() / 10000 + 1;
        sheet = wwb.createSheet("导出数据", 0);
        if (demoList.isEmpty()) {
            label = new Label(0, 1, "无数据");
            sheet.addCell(label);
        }
        for (int i = 0; i < sheetnum; i++) {
            sheet = wwb.createSheet("第" + (i + 1) + "页", i);
            // 表头
            for (int j = 0; j < cols; j++) {
                label = new Label(j, 0, exportFieldsName[j]);
                sheet.addCell(label);
                label = null;
            }
            int curRow = 1;
            for (int k = 10000 * i; k < 10000 * (i + 1)
                    && k < demoList.size(); k++) {
                Object[] obj = (Object[]) demoList.get(k);
                
                // 导出字段1
                label = new Label(0, curRow, obj[a] == null ? ""
                        : obj[a].toString());
                sheet.addCell(label);
                
                // 导出字段2
                String field2 = obj[b] == null ? "" : obj[b].toString();
                label = new Label(1, curRow, field2);
                sheet.addCell(label);
                
                // 导出字段3
                String field3_old = obj[c] == null ? "" : obj[c].toString();
                String field3_new = "";
                /*
                    If the value of field3_old is not exactly what you need,
                    you can do something according to it, 
                    then 
                */
                label = new Label(2, curRow, field3_new);
                sheet.addCell(label);
                
                ......
                
                curRow++;
            }
        }
        wwb.write();
        wwb.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值