Java poi導(dǎo)出Excel下載到客戶端
更新時間:2018年02月02日 11:36:32 作者:紫薇帝星的故事
這篇文章主要為大家詳細介紹了Java poi導(dǎo)出Excel下載到客戶端的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Java poi 導(dǎo)出Excel并下載到客戶端,具體內(nèi)容如下
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 { // 第一步,創(chuàng)建一個webbook,對應(yīng)一個Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //生成一個表格 HSSFSheet sheet = wb.createSheet(excelName); // 第三步,在sheet中添加表頭第0行 HSSFRow row = sheet.createRow(0); // 第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創(chuàng)建一個居中格式 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()); } } /** * 導(dǎo)入數(shù)據(jù)到表格中 * @param wb execl文件 * @param sheet 表格 * @param row 表格行 * @param record 要導(dǎo)出的數(shù)據(jù) * @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位代表開頭結(jié)尾行,后面2位代表開頭結(jié)尾列 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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Java使用POI導(dǎo)出Excel(一):單sheet
- Java利用POI實現(xiàn)導(dǎo)入導(dǎo)出Excel表格
- Java用POI導(dǎo)入導(dǎo)出Excel實例分析
- Java中用POI實現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel
- Java中利用POI優(yōu)雅的導(dǎo)出Excel文件詳解
- Java使用poi組件導(dǎo)出Excel格式數(shù)據(jù)
- Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
- java poi導(dǎo)出圖片到excel示例代碼
- java使用poi導(dǎo)出Excel的方法
- Java使用POI導(dǎo)出Excel(二):多個sheet
相關(guān)文章
東方通TongWeb結(jié)合Spring-Boot使用的實現(xiàn)
本文主要介紹了東方通TongWeb結(jié)合Spring-Boot使用的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07通過端口1433連接到主機127.0.0.1的 TCP/IP 連接失敗,錯誤:“connect timed out”的解
這篇文章主要介紹了通過端口1433連接到主機127.0.0.1的 TCP/IP 連接失敗,錯誤:“connect timed out”的解決方法,需要的朋友可以參考下2015-08-08如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01Java中的DelayQueue實現(xiàn)原理及應(yīng)用場景詳解
這篇文章主要介紹了Java中的DelayQueue實現(xiàn)原理及應(yīng)用場景詳解,DelayQueue是一個沒有邊界BlockingQueue實現(xiàn),加入其中的元素必需實現(xiàn)Delayed接口,當(dāng)生產(chǎn)者線程調(diào)用put之類的方法加入元素時,會觸發(fā)Delayed接口中的compareTo方法進行排序,需要的朋友可以參考下2023-12-12