介于导入数据操作,有时不需要做事务回滚,但是又需要记录错误的数据和err时,可以用xls返回,且可以直接通过outputStream来完成而不需要模板文件
加载 jxl.jar 包
Workbook book = Workbook.getWorkbook(is); 这个用来读文件
WritableWorkbook result = Workbook.createWorkbook(os); 这个用于写文件
response.setHeader("Content-disposition", "attachment;filename= result.xls"); //客户端得到的文件名
response.setContentType("application/x-download");//设置为下载application/x-download
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma","no-cache");
WritableWorkbook result = Workbook.createWorkbook(response.getOutputStream());
WritableSheet wsheet = result.createSheet("错误报告", 0);
WritableFont font = new WritableFont(WritableFont.TAHOMA, 11, WritableFont.NO_BOLD);// 定义字体
font.setColour(Colour.BLACK);// 字体颜色
WritableCellFormat wc = new WritableCellFormat(font);
wc.setAlignment(Alignment.LEFT); // 设置左对齐
wc.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
wc.setBackground(jxl.format.Colour.BLUE_GREY); // 设置单元格的背景颜色
for(int r=0;r<rows;r++){
wsheet.setRowView(r, 250); // 设置行高
for(int c=0;c<cells;c++){
wsheet.setColumnView(c, 30);// 设置列宽
wsheet.addCell(new Label(c,r,message, wc));
}
}
//最后写入
result.write();
result.close();