当服务器使用linux的时候,临时文件目录权限要和启动jboss用户的权限一样,否则数据倒出来不全或者导出来的excel到一半就导不出来了。
public String exportExcel() throws Exception{
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
OutputStream os = null;
String filename = "数据.xls"; //如果乱码需要encode编码
try {
response.reset();// 清空输出流
response.setContentType("application/vnd.ms-excel");// 定义输出类型
response.setHeader("Content-Disposition", "filename=" + filename + "");// 设定输出文件头
os = response.getOutputStream();// 获取输出流
long t0 = System.currentTimeMillis();
List<User> list = new ArrayList();
expordUtil(list, os);
long t1 = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (os != null) {
os.flush();
os.close();
}
}
return null;
}
//数据导出工具方法 list为导出的数据
public String expordUtil(List list,OutputStream os) throws Exception {
WorkbookSettings wbSetting = new WorkbookSettings();
wbSetting.setUseTemporaryFileDuringWrite(true); //表示允许使用临时文件
wbSetting.setTemporaryFileDuringWriteDirectory(new File("\\excel"));//
os.flush();
WritableWorkbook wbook = Workbook.createWorkbook(os);// 建立Excel文件
WritableSheet sheet = wbook.createSheet("学生信息", 0);// 为文件的sheet设置名字
WritableFont wfont;
WritableCellFormat wcformat; //格式化输出的excel
Label wlabel;
// 设置表头 start
wfont = new WritableFont(WritableFont.createFont("宋体"), 11,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.DARK_GREEN); // 设置表头的格式
//设置 输出的excel格式
wcformat = new jxl.write.WritableCellFormat(wfont);
wcformat.setBackground(Colour.AQUA);
wcformat.setLocked(true);
wcformat.setAlignment(Alignment.CENTRE);
wcformat.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);
//设置输出的第一列title
wlabel = new jxl.write.Label(0, 0, "姓名", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(1, 0, "性别", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(2, 0, "班级", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(3, 0, "学号", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(4, 0, "学校", wcformat);
sheet.addCell(wlabel);
// 设置表头 end
// 输出数据格式的设置 start
wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10,
WritableFont.NO_BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 设置表头的格式
wcformat = new jxl.write.WritableCellFormat(wfont);
wcformat.setBackground(Colour.WHITE);
wcformat.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);
wcformat.setLocked(false);
wcformat.setAlignment(Alignment.LEFT);
//设置小表头数据格式
wfont = new WritableFont(WritableFont.createFont("宋体"), 11,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.DARK_GREEN); // 设置表头的格式 11 为字体大小
WritableCellFormat wcformat1 = new jxl.write.WritableCellFormat(wfont);
wcformat1.setBackground(Colour.GRAY_25);
wcformat1.setLocked(false);
wcformat1.setAlignment(Alignment.CENTRE);
wcformat1.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);
//设置输出第二列数据
wlabel = new jxl.write.Label(0, 1, "王勇", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(1, 1, "男", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(2, 1, "一班", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(3 , 1, "20060512", wcformat);
sheet.addCell(wlabel);
wlabel = new jxl.write.Label(3 , 1, "绥化学院", wcformat);
sheet.addCell(wlabel);
//sheet.mergeCells(10, 0, 12, 0);
try {
//从内存中写入文件中
wbook.write();
//关闭资源,释放内存
wbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
// 输出数据格式的设置 end
return "success";
}