1. jxl 模板导出下载 jxl版本 2.6.12
备注: 由于jxl自身原因在windows平台下生成的文件正常打开,linux下 打开异常 “不能读取文件”
估计在linux下jxl写excel头信息不正常,以致office无法识别。
代码:
/**
* 下载某个学员的报名表
* @return
*/
public String download(){
List<SearchFilter> filters=new ArrayList<SearchFilter>();
SearchFilter f2=new SearchFilter("identityCard", SearchFilter.Operator.EQ,this.downIdentityCard);
filters.add(f2);
Specification<Training> spec = DynamicSpecifications.bySearchFilter(filters, Training.class,true);
this.trainings=this.trainingService.findAll(spec, new Sort(new Sort.Order(Sort.Direction.ASC, "subject.id"),new Sort.Order(Sort.Direction.ASC, "id")));
String templateName="training";
String fileSeparator = System.getProperty("file.separator");
String path = ServletActionContext.getServletContext().getRealPath("template");
String filePath =path+fileSeparator+ templateName+".xls";
OutputStream os=null;
Workbook wb=null;
WritableWorkbook wwb=null;
WritableSheet wws;
try{
response.reset();// 清空输出流
response.setContentType("application/msexcel; charset=utf-8"); // 定义输出类型
response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(templateName)+".xls");
os = response.getOutputStream();// 取得输出流
wb=Workbook.getWorkbook(new FileInputStream(distFile));
wwb = Workbook.createWorkbook(os, wb);
wws = wwb.getSheet(0);
if(Util.isNotEmpty(this.trainings)){
Training training=this.trainings.get(0);
Label A00 = (Label)wws.getWritableCell(0,0);
A00.setString(new String(training.getSubject().getTrainingName().getBytes(),"UTF-8")+"报名登记表");
Label A21 = (Label)wws.getWritableCell(2,1);
A21.setString(training.getSignNumber());
Label A22 = (Label)wws.getWritableCell(2,2);
A22.setString(training.getStudentName());
Label A23 = (Label)wws.getWritableCell(2,3);
A23.setString(DateUtil.getStringFromDate(training.getBirthday(), "yyyy-MM-dd"));
Label A24 = (Label)wws.getWritableCell(2,4);
A24.setString(DictUtils.getDictLabel(training.getBackground() + "", "continuing.background", ""));
Label A25 = (Label)wws.getWritableCell(2,5);
A25.setString(training.getGraduateSchool());
Label A26 = (Label)wws.getWritableCell(2,6);
A26.setString(training.getTeachingSubject());
Label A27 = (Label)wws.getWritableCell(2,7);
A27.setString(training.getPost());
Label A28 = (Label)wws.getWritableCell(2,8);
A28.setString(training.getEmail());
Label A29 =(Label) wws.getWritableCell(2,9);
A29.setString(training.getMobile());
Label A62 = (Label)wws.getWritableCell(6,2);
A62.setString(DictUtils.getDictLabel(training.getSex()+"","XB",""));
Label A63 = (Label)wws.getWritableCell(6,3);
A63.setString(training.getIdentityCard());
Label A64 = (Label)wws.getWritableCell(6,4);
A64.setString(DictUtils.getDictLabel(training.getDegree()+"","training.degree",""));
Label A65 = (Label)wws.getWritableCell(6,5);
A65.setString(training.getYourMajor());
Label A96 = (Label)wws.getWritableCell(9,6);
A96.setString(training.getWorkUnit());
Label A97 = (Label)wws.getWritableCell(9,7);
A97.setString(training.getTitle());
Label A98 = (Label)wws.getWritableCell(9,8);
A98.setString(training.getQq());
Label A99 = (Label)wws.getWritableCell(9,9);
A99.setString(DictUtils.getDictLabel(training.getLodging()+"","training.lodging",""));
Label A82 = (Label)wws.getWritableCell(8,2);
A82.setString(training.getNationality());
}
wwb.write();
}catch (Exception ex){
ex.printStackTrace();
}
finally {
try {
wwb.close();
wb.close();
os.flush();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
2. POI 模板导出下载 POI 版本 3.9 生成的excel 在windows ,linux下均正常打开
代码:
/**
* 下载某个学员的报名表
* @return
*/
public String download(){
List<SearchFilter> filters=new ArrayList<SearchFilter>();
SearchFilter f2=new SearchFilter("identityCard", SearchFilter.Operator.EQ,this.downIdentityCard);
filters.add(f2);
Specification<Training> spec = DynamicSpecifications.bySearchFilter(filters, Training.class,true);
this.trainings=this.trainingService.findAll(spec, new Sort(new Sort.Order(Sort.Direction.ASC, "subject.id"),new Sort.Order(Sort.Direction.ASC, "id")));
String templateName="training";
String fileSeparator = System.getProperty("file.separator");
String path = ServletActionContext.getServletContext().getRealPath("template");
String filePath =path+fileSeparator+ templateName+".xls";
String filePath2 =path+fileSeparator+ templateName+"2.xls";
OutputStream os=null;
Workbook wb=null;
Sheet wws=null;
try{
response.reset();// 清空输出流
response.setContentType("application/msexcel; charset=utf-8"); // 定义输出类型
response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(templateName)+".xls");
InputStream in=new FileInputStream(file);
wb = new HSSFWorkbook(in);
wws = wb.getSheetAt(0);
if(Util.isNotEmpty(this.trainings)){
Training training=this.trainings.get(0);
Row row0= wws.getRow(0);
Cell cell00=row0.getCell(0);
cell00.setCellValue(training.getSubject().getTrainingName()+"报名登记表");
Row row1=wws.getRow(1);
Cell cell12=row1.getCell(2);
cell12.setCellValue(training.getSignNumber());
Row row2=wws.getRow(2);
Cell cell22=row2.getCell(2);
cell22.setCellValue(training.getStudentName());
Cell cell26=row2.getCell(6);
cell26.setCellValue(DictUtils.getDictLabel(training.getSex()+"","XB",""));
Cell cell28=row2.getCell(8);
cell28.setCellValue(training.getNationality());
Row row3=wws.getRow(3);
Cell cell32=row3.getCell(2);
cell32.setCellValue(DateUtil.getStringFromDate(training.getBirthday(), "yyyy-MM-dd"));
Cell cell36=row3.getCell(6);
cell36.setCellValue(training.getIdentityCard());
Row row4=wws.getRow(4);
Cell cell42=row4.getCell(2);
cell42.setCellValue(DictUtils.getDictLabel(training.getBackground() + "", "continuing.background", ""));
Cell cell46=row4.getCell(6);
cell46.setCellValue(DictUtils.getDictLabel(training.getDegree()+"","training.degree",""));
Row row5 =wws.getRow(5);
Cell cell52=row5.getCell(2);
cell52.setCellValue(training.getGraduateSchool());
Cell cell56=row5.getCell(6);
cell56.setCellValue(training.getYourMajor());
Row row6=wws.getRow(6);
Cell cell62=row6.getCell(2);
cell62.setCellValue(training.getTeachingSubject());
Cell cell69=row6.getCell(9);
cell69.setCellValue(training.getWorkUnit());
Row row7=wws.getRow(7);
Cell cell72=row7.getCell(2);
cell72.setCellValue(training.getPost());
Cell cell79=row7.getCell(9);
cell79.setCellValue(training.getTitle());
Row row8=wws.getRow(8);
Cell cell82=row8.getCell(2);
cell82.setCellValue(training.getEmail());
Cell cell89=row8.getCell(9);
cell89.setCellValue(training.getQq());
Row row9=wws.getRow(9);
Cell cell92=row9.getCell(2);
cell92.setCellValue(training.getMobile());
Cell cell99=row9.getCell(9);
cell99.setCellValue(DictUtils.getDictLabel(training.getLodging()+"","training.lodging",""));
}
os = response.getOutputStream();// 取得输出流
wb.write(os);
}catch (Exception ex){
ex.printStackTrace();
}
finally {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}