使用poi導出excel

我們在做這個項目的時候,需要導出一個excel文件,所以我就想到了使用poi導出數(shù)據(jù)

下面把代碼貼出來,供大家參考,親測有效,希望小伙伴不要走彎路。


首先把poi? jar包導入項目中,我把我導入的節(jié)點提供給大家

<dependency>? ?

?<groupId>org.apache.poi</groupId>

? ? <artifactId>poi</artifactId>

? ? <version>3.9</version>

</dependency>

第一步,先從controller開始

@RequestMapping("/poi/load")

public String poi(Integer soulId, HttpServletResponse response, HttpSession session){

logger.info("進入poi/poi接口");

? ? HSSFWorkbook hssfWorkbook =poiService.selectBySoulId(soulId, response);

? ? session.setAttribute("hssfWorkbook",hssfWorkbook);

? ? return "test/test";

}

我們的業(yè)務需求是前臺傳來soulId的值來下載一個excel文件



第二步,進入service層

@Override

? ? public HSSFWorkbook? selectBySoulId(Integer soulId, HttpServletResponse response) {

? ? //從數(shù)據(jù)庫獲取數(shù)據(jù)

? ? ? ? List>list =sysConfigMapper.selectBySoulId(soulId);

? ? ? ? //excel標題

? ? ? ? String[]title ={"商品編號","商品名稱","商品規(guī)格","商品價格","貨架名稱"};

? ? ? ? //excel文件名

? ? ? ? String fileName ="臺賬詳情表" +System.currentTimeMillis() +".xls";

? ? ? ? //內容列表 行、列

? ? ? ? int size =list.size();

? ? ? ? String [] []content =new String [size][title.length];

? ? ? ? //sheet名

? ? ? ? String sheetName ="全家臺賬詳情表";

? ? ? ? if(size>0){

? ? ? ? ? ?for (int i =0; i<size; i++){

? ? ? ? ? ? ? ? ? ? Map?map =list.get(i);

? ? ? ? ? ? ? ? try{

? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][0] =map.get("ABIARTICLEID").toString(); //商品編號

? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][1] =map.get("ABIARTICLENAME").toString(); //商品名稱

? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][2] =map.get("ABISPEC").toString();//商品規(guī)格

? ? ? ? ? ? ? ? ? ? ? ? ? ? content[i][3] =map.get("ASPISTANDARDSALESPRICE").toString();//商品價格TITLE

? ? ? ? ? ? ? ? ? ? ? ? ? ? content[i][4] =map.get("TITLE").toString();//貨架名稱

? ? ? ? ? ? ? ? ? ?}catch (Exception e){

? ? ? ? ? ? ? ? }

? ? ? ? }

}

// 創(chuàng)建HSSFWorkbook

? ? ? ? HSSFWorkbook wb =ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

? ? ? ? try {

? ? ? ? ? ? ? ? ? ?ExportUtil.exportExcel(response, fileName, wb);

? ? ? ? ? ? }catch (Exception e) {

? ? ? ? ? ? }

? ? ? ? ? ? ? ? return? wb;

? ? }

service的接口

/**

* 查詢poi下載所需要的數(shù)據(jù)

*/

public HSSFWorkbook? selectBySoulId(Integer soulId, HttpServletResponse response);


第三步? ?mapper層

<select id="selectBySoulId" parameterType="Integer" resultType="java.util.HashMap">

這里是你自己的業(yè)務,你所需要下載的數(shù)據(jù),返回的是一個List<Map<String,Object>>格式

</select>

mapper層接口

/**

* 查詢poi下載所需要的數(shù)據(jù)

*/

public?List<Map<String,Object>>?selectBySoulId (Integer soulId)


第四步? ?分別是??ExcelUtil類? ?和? ?ExportUtil類??

public class ExcelUtil {

/**

* 導出Excel

*

? ? * @param sheetName? ?sheet名稱

? ? * @param title? ? 標題

? ? * @param content 內容

? ? * @param wb? HSSFWorkbook對象

? ? */

? ? public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[]title, String[][]content, HSSFWorkbook wb){

? ? ? ?//第一步 創(chuàng)建一個HSSFWorkbook, 對應一個Excel文件

? ? ? ? ? ? ? ?if (wb ==null){

? ? ? ? ? ? ? ? ? ? ? ? wb =new HSSFWorkbook();

? ? ? ? ? ? ? ? ?}

? ? ? ?//第二步, 在workbook中添加一個sheet,對應excel文件中的sheet

? ? ? ? HSSFSheet sheet = wb.createSheet(sheetName);

? ? ? ? //第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數(shù)列數(shù)有限制

? ? ? ? HSSFRow row =sheet.createRow(0);

? ? ? ? //第四步,創(chuàng)建單元格,并設置表頭 設置表頭居中

? ? ? ? HSSFCellStyle style = wb.createCellStyle();

? ? ? ? //創(chuàng)建一個居中格式

? ? ? ? style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

? ? ? ? //設置字體

? ? ? ? HSSFFont font = wb.createFont();

? ? ? ? //設置為楷體

? ? ? ? font.setFontName("楷體");

? ? ? ? //創(chuàng)建邊框對象

? ? ? ? HSSFCellStyle setBorder = wb.createCellStyle();

? ? ? ? //設置自動換行

? ? ? ? setBorder.setWrapText(true);

? ? ? ? //聲明列對象

? ? ? ? HSSFCell cell;

? ? ? ? //創(chuàng)建標題

? ? ? ? ? for(int i =0; i<title.length; i++){

? ? ? ? ? ? ? ? ?cell = row.createCell(i);

? ? ? ? ? ? ? ? ?cell.setCellValue(title[i]);

? ? ? ? ? ? ? ? cell.setCellStyle(style);

? ? ? ? ? }

? ? ? ?//創(chuàng)建內容

? ? ? ? for(int i =0; i<content.length; i++){

? ? ? ? ? ? ? ? String str =",";

? ? ? ? ? ? ? ? System.out.println(content[i]);

? ? ? ? ? ? ? ? row =sheet.createRow(i +1);

? ? ? ? ? ? ? ?for (int j =0; j<content[i].length; i++){

? ? ? ? ? ? ? ? ? ? ?//將內容按照順序賦給對應的列對象

? ? ? ? ? ? ? ? ? ? ? row.createCell(j).setCellValue(String.valueOf(content[i][j]));

? ? ? ? ? ? ? }

}

? ? ? ? ? ? ? ? ?return wb;

? ? }

}


public class ExportUtil {

public static void exportExcel(HttpServletResponse response, String fileName, HSSFWorkbook wb)throws? Exception{

fileName =new String(fileName.getBytes(),"ISO-8859-1");

? ? ? ? response.setContentType("application/octet-stream;charset=ISO-8859-1");

? ? ? ? response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

? ? ? ? response.addHeader("Pargam", "no-cache");

? ? ? ? response.addHeader("Cache-Control", "no-cache");

? ? ? ? OutputStream os =response.getOutputStream();

? ? ? ? wb.write(os);

? ? ? ? os.flush();

? ? ? ? os.close();

? ? }

}

第五步

也就是前端請求

<button id="js-export" type="button" class="btn btn-primary">導出Excel

? ? $('#js-export').click(function(){

window.location.href="${pageContext.request.contextPath}/poi/load.do";

? ? });

</script>


至此,大工告成了,希望能幫助到更多的小伙伴,給個支持再走把!

不懂的還可以給我留言,或者留下聯(lián)系方式,我來幫大家。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容