导出excel

struts-xml

<result name="createExcel" type="stream">
     <param name="contentType"> 
                     application/vnd.ms-excel 
                 </param> 
                 <param name="inputName">excelStream</param>
                 <param name="contentDisposition"> 
                     filename="export.xls" 
                 </param> 
                 <param name="bufferSize">1024</param> 
  </result>

 

 

java

      public String createExcel(){
     excelStream = pndDeviceService.getExcelInputStream();
     return "createExcel";
    }

 

serviceImpl

   public String createExcel(OutputStream os,List<PndDevice> list) {
  try {
   String str = "";
   WritableWorkbook wbook = Workbook.createWorkbook(os);    // OutputStream os建立excel文件new File("f:/PndDevice.xls"
   WritableSheet wsheet = wbook.createSheet("PND设备资料表", 0); // 工作表名称
   // 设置Excel字体
   WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
     WritableFont.BOLD, false,
     jxl.format.UnderlineStyle.NO_UNDERLINE,
     jxl.format.Colour.BLACK);
   WritableCellFormat titleFormat = new WritableCellFormat(wfont);
   String[] title = { "产品型号", "序列号", "Sim卡号", "通讯码", "当前地图版本",
     "供应商ID", "Pnd设备Id", "创建时间" };
   // 设置Excel表头
   for (int i = 0; i < title.length; i++) {
    Label excelTitle = new Label(i, 0, title[i], titleFormat);
    wsheet.addCell(excelTitle);
   }
   int c = 1; // 用于循环时Excel的行号
   // ClassroomService cs = new ClassroomService();
  // List list = pndDeviceDAO.findAll(); // 这个是从数据库中取得要导出的数据
   Iterator it = list.iterator();
   while (it.hasNext()) {
    PndDevice pd = (PndDevice) it.next();
    Label content1 = new Label(0, c, pd.getProductType());
    Label content2 = new Label(1, c, pd.getSerialNum());
    Label content3 = new Label(2, c, pd.getSimCardNum().toString());
    Label content4 = new Label(3, c, pd.getCommunicationCode());
    Label content5 = new Label(4, c, pd.getCurrentMapVersion());
    Label content6 = new Label(5, c, pd.getSuppliersId());
    Label content7 = new Label(6, c, pd.getPndDeviceId());
    Label content8 = new Label(7, c, pd.getCreateTime().toString());
    wsheet.addCell(content1);
    wsheet.addCell(content2);
    wsheet.addCell(content3);
    wsheet.addCell(content4);
    wsheet.addCell(content5);
    wsheet.addCell(content6);
    wsheet.addCell(content7);
    wsheet.addCell(content8);
    c++;
   }
   str = "正确";
   wbook.write(); // 写入文件
   wbook.close();
   os.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "true";
 }

 public InputStream getExcelInputStream() { 
  //将OutputStream转化为InputStream 
  ByteArrayOutputStream out = new ByteArrayOutputStream(); 
  List<PndDevice> list = pndDeviceDAO.findAll();
  createExcel(out,list); 
  return new ByteArrayInputStream(out.toByteArray()); 
 }

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值