Java poi匯出Excel下載到用戶端,poiexcel

來源:互聯網
上載者:User

Java poi匯出Excel下載到用戶端,poiexcel

Java poi 匯出Excel並下載到用戶端,具體內容如下

Maven配置,包含了其他檔案格式的依賴,就全貼出來了

<dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-excelant</artifactId>      <version>3.12</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-scratchpad</artifactId>      <version>3.12</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-ooxml</artifactId>      <version>3.8</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-ooxml-schemas</artifactId>      <version>3.8</version>    </dependency>

Service層

@Override  public void export(Long sblsh, String excelName, OutputStream out) {    try {      // 第一步,建立一個webbook,對應一個Excel檔案       HSSFWorkbook wb = new HSSFWorkbook();      //產生一個表格       HSSFSheet sheet = wb.createSheet(excelName);       // 第三步,在sheet中添加表頭第0行      HSSFRow row = sheet.createRow(0);            // 第四步,建立儲存格,並設定值表頭 設定表頭置中       HSSFCellStyle style = wb.createCellStyle();       style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個置中格式      HSSFCell cell = row.createCell(0);      cell.setCellStyle(style);            Byte kjzz = qyjbxxMapper.getKjzz(sblsh);      List<A> record = this.selectBySblsh(sblsh);        this.insertData(wb, sheet, row, record, out);      }    } catch (Exception e) {      logger.info(e.getMessage());    }  }    /**   * 匯入資料到表格中   * @param wb execl檔案   * @param sheet 表格   * @param row 表格行   * @param record 要匯出的資料   * @param out 輸出資料流   */  private void insertData(HSSFWorkbook wb,HSSFSheet sheet,HSSFRow row,List<A> record,      OutputStream out){    try {      row = sheet.createRow(1);      for(int i=0;i<title.length;i++){        row.createCell(i).setCellValue(title[i]);      }      for(int i=0;i<record.size();i++){        row = sheet.createRow(i+2);        A data = record.get(i);        row.createCell(0).setCellValue(data.getHc());        row.createCell(1).setCellValue(data.getXm());        BigDecimal je = data.getJe();        if(je!=null){          row.createCell(2).setCellValue(je.doubleValue());        }      }      //合併儲存格,前面2位代表開頭結尾行,後面2位代表開頭結尾列      CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1);      sheet.addMergedRegion(region);      wb.write(out);      out.flush();      out.close();      wb.close();    } catch (Exception e) {      logger.info(e.getMessage());    }  }

Controller

@RequestMapping("/export")  public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){    response.setContentType("octets/stream");    String excelName = "檔案名稱";    try {      response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");      OutputStream out = response.getOutputStream();      aService.export(sblsh,excelName ,out);    } catch (Exception e) {      e.printStackTrace();    }  }

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.