public void materialAgeList(List materialList, String path) {
try {
//设置响应头(以下这一坨)
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");
String fileName = java.net.URLEncoder.encode("仓库物资账龄.xls", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("UTF-8"), "GBK"));
OutputStream os = response.getOutputStream();
File file = new File(path);
WritableWorkbook wwb;
wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("仓库物资账龄", 0);
// 创建单元格样式
WritableCellFormat wcf = new WritableCellFormat();
WritableCellFormat wcf1 = new WritableCellFormat(
new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.RED));
wcf.setAlignment(Alignment.LEFT);
wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
wcf1.setAlignment(Alignment.CENTRE);
wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
// 设置表头
ws.mergeCells(0, 0, 4, 1);//设置第一列、第一行和 第一列、第二行合并
ws.addCell(new Label(0, 0, "物资账龄", wcf1));
String[] columns = { "物资编号", "物资名称", "物资数量", "物资价格", "账龄"};
for (int i = 0; i < columns.length; i++) {
// 设置列宽
ws.setColumnView(i, 30);
ws.addCell(new Label(i, 2, columns[i], wcf));
}
for (int i = 0; i < materialList.size(); i++) {
Map map = (Map) materialList.get(i);
ws.addCell(new Label(0, i + 3, changeToStr(map.get("物资编号")), wcf));
ws.addCell(new Label(1, i + 3, changeToStr(map.get("物资名称")), wcf));
ws.addCell(new Label(2, i + 3, changeToStr(map.get("物资数量")), wcf));
ws.addCell(new Label(3, i + 3, changeToStr(map.get("物资价格")), wcf));
ws.addCell(new Label(4, i + 3, changeToStr(map.get("账龄")), wcf));
}
wwb.write();
wwb.close();
} catch (Exception e) {
log.error(e.getMessage());
}
}
struts2 导出excel扩展名为.action
最新推荐文章于 2021-02-12 19:01:38 发布