JXL根据指定Excel模板导出xls
//1.导入xls模板
File file = new File("data/lglk/excle/LandNeed.xls");
Workbook workbook = Workbook.getWorkbook(file);
HttpServletResponse response = Application.getResponse();
response.setContentType("application/x-msdownload");
String fileName = "用地需求项目库";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName + ".xls");
OutputStream os = response.getOutputStream();
WorkbookSettings wbSettings = new WorkbookSettings ();
wbSettings.setWriteAccess(null);
// 2.创建工作表
WritableWorkbook writableWorkbook = Workbook.createWorkbook(os, workbook,wbSettings);
//3.将内容写入writableWorkbook
landNeedService.createXls(landNeedList,writableWorkbook);
// 4.写入文件
writableWorkbook.write();
writableWorkbook.close();
workbook.close();
os.close();
//将内容写入writableWorkbook
public void createXls(List<LandNeed> landNeedList, WritableWorkbook wbook) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
WritableSheet wsheet = wbook.getSheet(0);
int row = wsheet.getRows();
int index = 1;
for (LandNeed landNeed : landNeedList) {
Label excelTitle1 = new Label(0, row, index+"");
wsheet.addCell(excelTitle1);
Label excelTitle2 = new Label(1, row, landNeed.getProjectName());
wsheet.addCell(excelTitle2);
Label excelTitle3 = new Label(2, row, landNeed.getProjectDesc());
wsheet.addCell(excelTitle3);
Label excelTitle4 = null;
if(landNeed.getIndustryPosition()!=null){
excelTitle4 = new Label(3, row, landNeed.getIndustryPosition().getTypeName());
}else{
excelTitle4 = new Label(3, row, "");
}
wsheet.addCell(excelTitle4);
Label excelTitle5 = null;
if(landNeed.getIndustry()!=null){
excelTitle5 = new Label(4, row, landNeed.getIndustry().getName());
}else{
excelTitle5 = new Label(4, row, "");
}
wsheet.addCell(excelTitle5);
Label excelTitle6 = new Label(5, row, landNeed.getSalesAmount()+"");
wsheet.addCell(excelTitle6);
Label excelTitle7 = new Label(6, row, landNeed.getRatal()+"");
wsheet.addCell(excelTitle7);
Label excelTitle8 = new Label(7, row, landNeed.getNeedArea()+"");
wsheet.addCell(excelTitle8);
Label excelTitle9 = new Label(8, row, landNeed.getBuildArea()+"");
wsheet.addCell(excelTitle9);
Label excelTitle10 = new Label(9, row, landNeed.getGroup().getName());
wsheet.addCell(excelTitle10);
Label excelTitle11 = new Label(10, row, landNeed.getAddressArea());
wsheet.addCell(excelTitle11);
Label excelTitle12 = new Label(11, row, landNeed.getProposeArea()+"");
wsheet.addCell(excelTitle12);
Label excelTitle13 = new Label(12, row, landNeed.getProjectMarch());
wsheet.addCell(excelTitle13);
Label excelTitle14 = new Label(13, row, landNeed.getLinkman());
wsheet.addCell(excelTitle14);
Label excelTitle15 = new Label(14, row, landNeed.getPhone());
wsheet.addCell(excelTitle15);
Label excelTitle16 = new Label(15, row, format.format(landNeed.getEnregisterDate()));
wsheet.addCell(excelTitle16);
Label excelTitle17 = new Label(16, row, landNeed.getDescription());
wsheet.addCell(excelTitle17);
index++;
row++;
}
} catch (Exception e) {
e.printStackTrace();
}
}