导出excel

    技术2022-05-19  26

    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());   }

      


    最新回复(0)