POI导出-----工作集锦

方法一:代码生成


 步骤一:获取数据源

步骤二:设计Excel表格

步骤三:定义Excel文件在服务器中的完整生成路径

步骤四:在服务器中生成Excel文件

步骤五:将服务器中生成的文件下载到本地

 

public void   reportExport (Listbox  reportListbox)  throws IOException,InterruptedException {

  try {

   // 第一步,创建一个webbook,对应一个Excel文件
   HSSFWorkbook wb = new HSSFWorkbook();
   // 第二步,在webbook中添加一个sheet,对应Excel文件中的一个sheet页
   HSSFSheet sheet = wb.createSheet("课题统计");

   // 1、定义该sheet页的列
   sheet.setColumnWidth((short) 0, (short) 100 * 20);
   sheet.setColumnWidth((short) 1, (short) 100 * 40);
   sheet.setColumnWidth((short) 2, (short) 100 * 40);
   sheet.setColumnWidth((short) 3, (short) 100 * 40);
   sheet.setColumnWidth((short) 4, (short) 100 * 80);

 

   // 2、定义该sheet页的样式(字体)

   HSSFFont  fontBoldStyle = wb.createFont();
   fontBoldStyle.setFontName("黑体");
   fontBoldStyle.setFontHeightInPoints((short) 20);
   fontBoldStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

 

   HSSFFont fontSongStyle = wb.createFont();
   fontSongStyle.setFontName("宋体");
   fontSongStyle.setFontHeightInPoints((short) 16);
   fontSongStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

 

   // 3、定义该sheet页的样式(单元格样式

   HSSFCellStyle  style = wb.createCellStyle();                                  // 样式对象1(标题)
   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);                   // 水平
   style.setFont(fontBoldStyle);

 

   HSSFCellStyle songStyle = wb.createCellStyle();                                  // 样式对象2(列头)
   songStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
   songStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);                   // 水平
   songStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
   songStyle.setBorderLeft((short) 1);
   songStyle.setBorderRight((short) 1);
   songStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
   songStyle.setFont(fontSongStyle);

 

   //  4、定义该sheet页的第一行标题

   HSSFRow row = sheet.createRow(0);


   // 单元格合并( 四个参数分别是:起始行,起始列,结束行,结束列)
   sheet.addMergedRegion ( new Region (0 , (short) 0 , 1 , (short) 4));
   HSSFCell ce = row.createCell((short) 0);

   ce.setCellValue("指导老师课题统计汇总 ");    // 表格的第一行第一列显示的数据
   ce.setCellStyle(style); // 样式,居中

 

   // 5、定义该sheet页的第三行,即表头(第一行和第二行合并了)

   row = sheet.createRow(2);

 

   // 定义行的每个单元格
   HSSFCell cell = row.createCell(0);
   cell.setCellValue("序号");
   cell.setCellStyle(songStyle);

 

   cell = row.createCell(1);
   cell.setCellValue("老师姓名");
   cell.setCellStyle(songStyle);


   cell = row.createCell(2);
   cell.setCellValue("职称");
   cell.setCellStyle(songStyle);


   cell = row.createCell(3);
   cell.setCellValue("出题数");
   cell.setCellStyle(songStyle);


   cell = row.createCell(4);
   cell.setCellValue("部门");
   cell.setCellStyle(songStyle);

 

   // 第三步,从第四行开始为每行的每个单元格填充数据此处为方法传进来的数据

   int i = 0;
   List<Listitem> lists = reportListbox.getItems();
   for (Listitem item : lists) {
    row = sheet.createRow( i + 3 );


    cell = row.createCell(0);
    cell.setCellValue(i + 1);
    cell.setCellStyle(songStyle);

 

    cell = row.createCell(1);
    cell.setCellValue(((Listcell) item.getChildren().get(0)).getLabel());
    cell.setCellStyle(songStyle);

 

    cell = row.createCell(2);
    cell.setCellValue(((Listcell) item.getChildren().get(1)).getLabel());
    cell.setCellStyle(songStyle);

 

    cell = row.createCell(3);
    cell.setCellValue(((Listcell) item.getChildren().get(2)).getLabel());
    cell.setCellStyle(songStyle);

 

    cell = row.createCell(4);
    cell.setCellValue(((Listcell) item.getChildren().get(3)).getLabel());
    cell.setCellStyle(songStyle);

    i++;
   }

 

   // 第五步,在服务器中生成此Excel文件(excel文件的写)
E:\workspaces\ums\WebRoot\exportExcel

 

   // 服务器中的完整保存路径
   String xlsFile = Labels.getLabel("ProjectPath")+ Labels.getLabel("WebRoot") + "/exportExcel" ;
   File path = new File(xlsFile);
   if (!path.exists()) {
    path.mkdirs();
   }
   File f = new File( path+"/"+fileName+ "老师课题汇总.xls");


   // 创建文件流
   FileOutputStream  fOut = new FileOutputStream ( f );
   // 写文件数据到指定路径的硬盘
   wb.write ( fOut );
   // 操作结束,关闭文件
   fOut.flush();
   if (fOut != null)
    fOut.close();

 

   // 第六步,将服务器中的生成文件下载到本地



 
    -----设置Excel文件名的编码
   String encodedName = URLEncoder.encode( "老师课题汇总.xls","UTF-8");   

    -----不带编码的下载(“ f ”:服务器中文件的生成路径,此方法适合英文的文件名)
   Filedownload.save( f ,  "xls");                               

    -----带编码的下载   (此方法适合中文的文件名,同下)                                                                
   Filedownload.save(new FileInputStream ( f ) , "xls" , encodedName);                       

    -----带编码的下载             
   Filedownload.save(new FileInputStream ( f ) , "application/octet-stream" ,  encodedName); 

  } catch (Exception e) {
   Messagebox.show("导出EXCEL出错!请联系管理员");
   e.printStackTrace();
  }
 }

 

方法二:套用模板

 

public void ReportExport(Listbox reportListbox) throws IOException,InterruptedException {

 

//第一步、创建模板存放的路径的输入流,读取模板的当前工作薄(excel文件的读)



 
  String templatefile="D:/workspace/ums/WebRoot/templateFile/statExportFile/TEACHER_TOPIC_STAT_EXPORT_TEMP.xls";
  File file=new File(templatefile);
  HSSFWorkbook  workbook=new HSSFWorkbook(new FileInputStream(file));
  HSSFSheet     worksheet=workbook.getSheet("data");

 

//定义数据的样式
  HSSFFont fontSongStyle = workbook.createFont();
  fontSongStyle.setFontName("宋体");
  fontSongStyle.setFontHeightInPoints((short) 16);
  fontSongStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
  
  HSSFCellStyle songStyle = workbook.createCellStyle(); // 样式对象
  songStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
  songStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
  songStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
  songStyle.setBorderLeft((short) 1);
  songStyle.setBorderRight((short) 1);
  songStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
  songStyle.setFont(fontSongStyle);

 

//第二步、创建当前工作薄的新行,导入数据
  int i = 0;
  List<Listitem> lists = reportListbox.getItems();
  for (Listitem item : lists) {
   HSSFRow row = worksheet.createRow(i + 2);
   HSSFCell cell = row.createCell(1);
   cell.setCellValue(i + 1);
   cell.setCellStyle(songStyle);

   cell = row.createCell(2);
   cell.setCellValue(((Listcell) item.getChildren().get(0)).getLabel());
   cell.setCellStyle(songStyle);
   
   cell = row.createCell(3);
   cell.setCellValue(((Listcell) item.getChildren().get(1)).getLabel());
   cell.setCellStyle(songStyle);

   cell = row.createCell(4);
   cell.setCellValue(((Listcell) item.getChildren().get(2)).getLabel());
   cell.setCellStyle(songStyle);

   cell = row.createCell(5);
   cell.setCellValue(((Listcell) item.getChildren().get(3)).getLabel());
   cell.setCellStyle(songStyle);

   i++;
  }

 

第三步,在服务器中生成此Excel文件(excel文件的写)



 

//将存储了数据的模板文件导出到输出文件流,创建一个新的报表文件

  worksheet.setGridsPrinted(true);
  File f = new File("D:/workspace/ums/WebRoot/templateFile/statExportFile/老师课题汇总.xls");
  FileOutputStream fOut = new FileOutputStream(f);
  workbook.write(fOut);
  fOut.flush();
  if (fOut != null)
   fOut.close();
  第四步,将服务器中的生成文件下载到本地
  String encodedName = URLEncoder.encode("老师课题汇总.xls","UTF-8");// 设置Excel文件名的编码
  Filedownload.save(new FileInputStream(f), "xls",encodedName);//带编码的下载

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值