關(guān)于SpringBoot將數(shù)據(jù)導(dǎo)出到excel表格

POI操作Excel
poi簡介
Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫的功能截型。

1宦焦、HSSF:HSSF 是Horrible SpreadSheet Format的縮寫赶诊,通過HSSF,你可以用純Java代碼來讀取寓调、寫入锄码、修改Excel文件。

2痛悯、POI EXCEL文檔結(jié)構(gòu)類

HSSFWorkbook excel文檔對象

HSSFSheet excel的sheet

HSSFRow excel的行

HSSFCell excel的單元格

HSSFFont excel字體

HSSFName 名稱

HSSFDataFormat 日期格式

HSSFHeader sheet頭

HSSFFooter sheet尾

HSSFCellStyle cell樣式

HSSFDateUtil 日期

HSSFPrintSetup 打印

HSSFErrorConstants 錯誤信息表

3载萌、導(dǎo)出Excel常用的方法:

HSSFWorkbook wb = new HSSFWorkbook(); //創(chuàng)建Excel工作簿對象

HSSFSheet sheet = wb.createSheet("new sheet"); //創(chuàng)建Excel工作表對象

HSSFRow row = sheet.createRow((short)0); //創(chuàng)建Excel工作表的行

cellStyle = wb.createCellStyle(); //創(chuàng)建單元格樣式

row.createCell((short)0).setCellStyle(cellStyle); //創(chuàng)建Excel工作表指定行的單元格

row.createCell((short)0).setCellValue(1); //設(shè)置Excel工作表的值

例如:

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.binshi.common.utils.common.R;
import com.binshi.common.utils.page.PageUtils;
import com.binshi.common.utils.page.Query;
import com.binshi.common.utils.tools.StringUtil;
import com.binshi.store.modules.car.dao.StorePersonEarningsDao;
import com.binshi.store.modules.car.entity.*;
import com.binshi.store.modules.car.service.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

 public R export(Long storeId,HttpServletResponse response) throws IOException {
        //獲取表頭數(shù)據(jù)
        String[] header = {"序號","姓名","電話","車牌","車輛詳情"};
        //獲取數(shù)據(jù)內(nèi)容
        List<StoreUserEntity> storeUserEntityList = this.selectList(new EntityWrapper<StoreUserEntity>().eq("store_id",storeId));

        //聲明一個工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //設(shè)置表頭樣式
        XSSFCellStyle headStyle = setHeadStyle(workbook);
        // 給單元格內(nèi)容設(shè)置另一個樣式
        XSSFCellStyle cellStyle = setCellStyle(workbook);

        //生成一個表格扭仁,設(shè)置表格名稱為"門店會員表"
        XSSFSheet sheet = workbook.createSheet("門店會員表");
        //設(shè)置表格列寬度為10個字節(jié)
        sheet.setDefaultColumnWidth(45);
        //創(chuàng)建第一行表頭
        XSSFRow headrow = sheet.createRow(0);
        sheet.setVerticallyCenter(true);
        //遍歷添加表頭
        for (int i = 0; i < header.length; i++) {
            //創(chuàng)建一個單元格
            XSSFCell cell = headrow.createCell(i);
            //創(chuàng)建一個內(nèi)容對象
            XSSFRichTextString text = new XSSFRichTextString(header[i]);
            //將內(nèi)容對象的文字內(nèi)容寫入到單元格中
            cell.setCellStyle(headStyle);
            cell.setCellValue(text);
        }


        //模擬遍歷數(shù)據(jù)內(nèi)容乖坠,把內(nèi)容加入表格
        for (int i = 0; i < storeUserEntityList.size(); i++) {
            XSSFRow row1 = sheet.createRow(i+1);
            for(int j=0;j<header.length;j++){
                XSSFCell cell = row1.createCell(j);
                XSSFRichTextString text = new XSSFRichTextString();
                if(j==0){
                    text = new XSSFRichTextString(i+1+"");
                }else if(j==1){
                    if(storeUserEntityList.get(i).getName()!=null){
                        text = new XSSFRichTextString(storeUserEntityList.get(i).getName());
                    }
                }else if(j==2){
                    if(storeUserEntityList.get(i).getPhone()!=null){
                        text = new XSSFRichTextString(storeUserEntityList.get(i).getPhone());
                    }
                }else if(j==3){
                    if(storeUserEntityList.get(i).getCarCodeTotal()!=null){
                        text = new XSSFRichTextString(storeUserEntityList.get(i).getCarCodeTotal());
                    }
                }else if(j==4){
                    if(storeUserEntityList.get(i).getCarName()!=null){
                        text = new XSSFRichTextString(storeUserEntityList.get(i).getCarName());
                    }
                }
                cell.setCellStyle(cellStyle);
                cell.setCellValue(text);
            }
        }
      // 下載導(dǎo)出
        String filename = "報(bào)銷申請表.xlsx";
        // 設(shè)置頭信息
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        //一定要設(shè)置成xlsx格式
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename , "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            filename = URLEncoder.encode(filename, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename="+filename+";"+"filename*=utf-8''"+filename);
        //創(chuàng)建一個輸出流
        ServletOutputStream outputStream = response.getOutputStream();
        //寫入數(shù)據(jù)
        workbook.write(outputStream);
        return R.ok("導(dǎo)出成功");
    }

導(dǎo)出后的excel表格如下圖所示:


a.jpg

自此仰迁,使用POI將數(shù)據(jù)導(dǎo)出到excel表格完成顽分。

ps:關(guān)于導(dǎo)出數(shù)據(jù)會遇到的問題:poi和poi-ooxml版本不一致可能會出現(xiàn)錯誤卒蘸。
把版本該成一致就好啦

     <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10</version>
        </dependency>
<!--         https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10</version>
        </dependency>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末澄步,一起剝皮案震驚了整個濱河市和泌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌梯皿,老刑警劉巖东羹,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件忠烛,死亡現(xiàn)場離奇詭異,居然都是意外死亡冤议,警方通過查閱死者的電腦和手機(jī)斟薇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恕酸,“玉大人,你說我怎么就攤上這事蕊温「は洌” “怎么了义矛?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵犯眠,是天一觀的道長。 經(jīng)常有香客問我症革,道長鸯旁,這世上最難降的妖魔是什么噪矛? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮铺罢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘韭赘。我一直安慰自己缩滨,他們只是感情好泉瞻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布袖牙。 她就那樣靜靜地躺著司忱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪畴蹭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機(jī)與錄音糊闽,去河邊找鬼锻离。 笑死铺峭,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的汽纠。 我是一名探鬼主播卫键,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼虱朵!你這毒婦竟也來了莉炉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碴犬,失蹤者是張志新(化名)和其女友劉穎絮宁,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體服协,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡绍昂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了偿荷。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窘游。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖跳纳,靈堂內(nèi)的尸體忽然破棺而出忍饰,到底是詐尸還是另有隱情,我是刑警寧澤寺庄,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布艾蓝,位于F島的核電站,受9級特大地震影響斗塘,放射性物質(zhì)發(fā)生泄漏赢织。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一馍盟、第九天 我趴在偏房一處隱蔽的房頂上張望敌厘。 院中可真熱鬧,春花似錦朽合、人聲如沸俱两。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宪彩。三九已至,卻和暖如春讲婚,著一層夾襖步出監(jiān)牢的瞬間尿孔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留活合,地道東北人雏婶。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像白指,于是被迫代替她去往敵國和親留晚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355