JXL生成Excel,并提供下载(1:生成Excel)

时间:2023-01-09 05:46:00
public String exportExcel(long id) {
String preeReviewName = "文件名"; String filePath = 路径名;
WritableWorkbook wwb =null;
try {
wwb = Workbook.createWorkbook(new File(filePath + preeReviewName +".xls"));
writeReviewGeneral(wwb, id);
//一个Excel表若有多个sheet页,可以加多个
wwb.write();
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return preeReviewName;
} private void writeReviewGeneral(WritableWorkbook wwb,long id){
//获取数据库的数据
PreeReviewInfo result = preeReviewInfoDS.getPreeReviewInfo(id).getT();
//数据库各个字段的值放进List
List<String> list = new ArrayList<String>();
list.add(result.getPreeReviewName());//项目名称
list.add(result.getPreeReviewTarget());//评审对象
list.add(ReviewTypes.getDescription(result.getPreeReviewType()));//评审方式
list.add(result.getPreeReviewScale());//评审规模
list.add(result.getProcessName(););//项目当前阶段:所处过程
list.add(result.getPreeCompere());//主持人
list.add(result.getDocumentCreator());//作者
list.add(result.getPreeRegistrar());//记录员
list.add(result.getPreeActor());//评审员
list.add(result.getExpert());//关键资源:专家
try {
WritableSheet sheet = wwb.createSheet("评审概况",);
//设置头部格式
WritableCellFormat headerFormat = getExcelHeadStyle();
//设置内容格式
WritableCellFormat contentFormat = getExcelContentStyle();
for(int i =; i < list.size(); i++){
sheet.setColumnView(, );//列宽
sheet.setColumnView(, );//列宽 sheet.setRowView(i, ); //行高
sheet.addCell(new Label(,i,ExportSummary.reviewGeneral[i],headerFormat));
sheet.addCell(new Label(,i,list.get(i),contentFormat));
} } catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
} private WritableCellFormat getExcelHeadStyle(){
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);
headerFormat.setFont(font);
try {
//内容水平居中显示
headerFormat.setAlignment(jxl.format.Alignment.CENTRE);
} catch (WriteException e) {
e.printStackTrace();
} return headerFormat;
} private WritableCellFormat getExcelContentStyle(){
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat contentFormat = new WritableCellFormat(NumberFormats.TEXT);
contentFormat.setFont(font);
try {
//允许换行
contentFormat.setWrap(true);
} catch (WriteException e) {
e.printStackTrace();
}
return contentFormat;
}