在做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();
}
%>