您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Java中實現導出Excel,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
首先,引入需要依賴的jar包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
編寫一個工具類:
package exceloutput; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.UUID; /** * @author haozz * @date 2018/6/6 9:57 * @description excel導出抽象工具類 **/ public abstract class ExportAbstractUtil { public void write(HttpServletResponse response, Workbook workbook){ String fileName = UUID.randomUUID().toString()+".xls"; pwrite(response,workbook,fileName); } public void write(HttpServletResponse response,Workbook workbook,String fileName){ if(StringUtils.isEmpty(fileName)){ fileName = UUID.randomUUID().toString()+".xls"; } pwrite(response,workbook,fileName); } public void write(HttpServletResponse response, List<List<String>> lists,String fileName){ if(StringUtils.isEmpty(fileName)){ fileName = UUID.randomUUID().toString()+".xls"; } SXSSFWorkbook workbook = new SXSSFWorkbook(lists.size()); SXSSFSheet sheet = workbook.createSheet(fileName.substring(0,fileName.indexOf(".xls"))); Integer rowIndex = 0; Row row = null; Cell cell = null; for(List<String> rowData: lists ){ Integer columnIndex = 0; row = sheet.createRow(rowIndex++); for(String columnVal:rowData){ cell = row.createCell(columnIndex++); cell.setCellValue(columnVal); } } pwrite(response,workbook,fileName); } private void pwrite(HttpServletResponse response,Workbook workbook,String fileName){ response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); try { response.addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); fileName= UUID.randomUUID().toString()+".xls"; response.addHeader("Content-Disposition", "attachment; filename="+fileName); } try { workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
有了這個工具類就可以實現Excel導出了,代碼不難,這里就不多解釋了。
在SpringBoot項目中編寫一個導出Excel的Controller,并繼承上面的ExportAbstractUtil,給出一個接口用作測試:
package com.csdn.myboot.controller; import com.csdn.myboot.utils.ExportAbstractUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @Controller @RequestMapping(value = "/index") public class HelloCtrl extends ExportAbstractUtil{ @RequestMapping(value = "/testExcelOutPut") @ResponseBody public void testExcelOutPut(HttpServletResponse response){ //拼接數據start List<List<String>> lists = new ArrayList<List<String>>(); String rows[] = {"year","month","day"}; List<String> rowsTitle = Arrays.asList(rows); lists.add(rowsTitle); for(int i = 0; i<=9;i++){ String [] rowss = {"1","2","3"}; List<String> rowssList = Arrays.asList(rowss); lists.add(rowssList); } //拼接數據end write(response,lists,"導出Excel.xls"); } }
上述內容就是怎么在Java中實現導出Excel,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。