欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用poi生成excel的步驟

 更新時(shí)間:2022年04月16日 12:36:14   作者:歪頭兒在帝都  
2010以上格式使用XSSFWorkBook對(duì)象, 2003格式使用HSSFWorkBook對(duì)象, 其他對(duì)象操作基本一樣,本文重點(diǎn)給大家介紹java使用poi生成excel的相關(guān)知識(shí),感興趣的朋友一起看看吧

使用poi生成excel通常包含一下幾個(gè)步驟

  • 創(chuàng)建一個(gè)工作簿
  • 創(chuàng)建一個(gè)sheet
  • 創(chuàng)建一個(gè)Row對(duì)象
  • 創(chuàng)建一個(gè)cell對(duì)象(1個(gè)row+1個(gè)cell構(gòu)成一個(gè)單元格)
  • 設(shè)置單元格內(nèi)容
  • 設(shè)置單元格樣式. 字體 字體大小 是否加粗
  • 保存
  • 關(guān)閉流對(duì)象

生成一個(gè)工作簿

2010以上格式使用XSSFWorkBook對(duì)象, 2003格式使用HSSFWorkBook對(duì)象, 其他對(duì)象操作基本一樣.

生成2003格式

public void test1() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    
    Font font = workbook.createFont();
    font.setFontName("宋體"); 
    font.setFontHeightInPoints((short) 12);
    cellStyle.setFont(font);
   
    HSSFSheet sheet = workbook.createSheet("Sheet1");
    //設(shè)置單元格寬度
    sheet.setColumnWidth(0, 30 * 256);
    sheet.setColumnWidth(1, 30 * 256);
    sheet.setColumnWidth(2, 30 * 256);
    
    Row row0 = sheet.createRow(0);
    Cell cell0 = row0.createCell(0);
    cell0.setCellValue("序號(hào)");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成績(jī)");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\測(cè)試生成2003.xls");
        workbook.write(os);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

生成2010以上格式

@Test
public void test2() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    
    Font font = workbook.createFont();
    font.setFontName("宋體");
    font.setFontHeightInPoints((short) 12);
    cellStyle.setFont(font);
    
    
    XSSFSheet sheet = workbook.createSheet("Sheet1");
    Row row0 = sheet.createRow(0);
    Cell cell0 = row0.createCell(0);
    cell0.setCellValue("序號(hào)");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成績(jī)");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\測(cè)試生成2010.xlsx");
        workbook.write(os);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

合并單元格

合并單元格在生成excel中算常見(jiàn)的一個(gè)場(chǎng)景, 通常先合并單元, 單元格內(nèi)容居中,并設(shè)置單元格邊框.
poi合并單元格使用CellRangeAddress類, 構(gòu)造函數(shù)包括4個(gè)參數(shù)firstRow, lastRow, firstCol, lastCol根據(jù)自己需要傳入行和列.

public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
}

合并單元格后設(shè)置邊框poi已提供了RegionUtil靜態(tài)類, 可直接使用.

CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);

RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);

設(shè)置單元格樣式

左右居中 上下居中 自動(dòng)換行

cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);

使用SpringMVC/SpringBoot導(dǎo)出excel

@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse httpServletResponse){
    try {
        //2010格式設(shè)置
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //2003格式設(shè)置
        response.setContentType("application/vnd.ms-excel");
        httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學(xué)生成績(jī)單.xls", "utf-8"));

        ServletOutputStream outputStream = httpServletResponse.getOutputStream();

        HSSFWorkbook workbook = new HSSFWorkbook();

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        Font font = workbook.createFont();
        font.setFontName("宋體");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        HSSFSheet sheet = workbook.createSheet("Sheet1");
        Row row0 = sheet.createRow(0);
        Cell cell0 = row0.createCell(0);
        cell0.setCellValue("序號(hào)");
        cell0.setCellStyle(cellStyle);

        Cell cell1 = row0.createCell(1);
        cell1.setCellValue("姓名");

        Cell cell2 = row0.createCell(2);
        cell2.setCellValue("成績(jī)");

        workbook.write(outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

總結(jié)

如果你在web項(xiàng)目中導(dǎo)出excel后,打開(kāi)excel文件時(shí)提示文件已損壞,但是文件還可以打開(kāi), 則需要在HttpServletResponse上設(shè)置響應(yīng)頭, 2003和2010設(shè)置方式不同
2003
response.setContentType("application/vnd.ms-excel");
2010
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

下載文件名如果包含中文的話需要編碼
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學(xué)生成績(jī)單.xls", "utf-8"));
這種設(shè)置在瀏覽器里下載文件中文是沒(méi)問(wèn)題的, 只是如果你使用Swagger或者Postman測(cè)試的話,文件名還是經(jīng)過(guò)編碼的, 這個(gè)沒(méi)問(wèn)題說(shuō)明文件下載已經(jīng)沒(méi)問(wèn)題.

到此這篇關(guān)于java使用poi生成excel的文章就介紹到這了,更多相關(guān)java生成excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flowable流程引擎API與服務(wù)

    Flowable流程引擎API與服務(wù)

    這篇文章主要介紹了Flowable流程引擎API與服務(wù),引擎API是與Flowable交互的最常用手段,總?cè)肟邳c(diǎn)是ProcessEngine,使用ProcessEngine,可以獲得各種提供工作流或BPM方法的服務(wù),下面我們來(lái)詳細(xì)了解
    2023-10-10
  • Java?Rabbitmq中四種集群架構(gòu)的區(qū)別詳解

    Java?Rabbitmq中四種集群架構(gòu)的區(qū)別詳解

    這篇文章主要為大家詳細(xì)介紹了Java?Rabbitmq中四種集群架構(gòu)的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • JavaBean實(shí)體類處理外鍵過(guò)程解析

    JavaBean實(shí)體類處理外鍵過(guò)程解析

    這篇文章主要介紹了JavaBean實(shí)體類處理外鍵過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Mybatis錯(cuò)誤引起的程序啟動(dòng)卡死問(wèn)題及解決

    Mybatis錯(cuò)誤引起的程序啟動(dòng)卡死問(wèn)題及解決

    這篇文章主要介紹了Mybatis錯(cuò)誤引起的程序啟動(dòng)卡死問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 基于application和bootstrap的加載順序及區(qū)別說(shuō)明

    基于application和bootstrap的加載順序及區(qū)別說(shuō)明

    這篇文章主要介紹了application和bootstrap的加載順序及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java 中Map詳解及實(shí)例代碼

    java 中Map詳解及實(shí)例代碼

    這篇文章主要介紹了java 中Map詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java 多線程學(xué)習(xí)詳細(xì)總結(jié)

    Java 多線程學(xué)習(xí)詳細(xì)總結(jié)

    本文主要介紹 Java 多線程的知識(shí)資料,這里整理了詳細(xì)的多線程內(nèi)容,及簡(jiǎn)單實(shí)現(xiàn)代碼,有需要的朋友可以參考下
    2016-09-09
  • java中javaBean與Bean的深入理解

    java中javaBean與Bean的深入理解

    這篇文章主要介紹了java中javaBean與Bean的深入理解的相關(guān)資料,這里說(shuō)明他們的優(yōu)點(diǎn)及使用方法,需要的朋友可以參考下
    2017-08-08
  • springboot集成fastDfs過(guò)程代碼實(shí)例

    springboot集成fastDfs過(guò)程代碼實(shí)例

    這篇文章主要介紹了springboot集成fastDfs過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Hibernate組件映射代碼詳解

    Hibernate組件映射代碼詳解

    這篇文章主要介紹了Hibernate組件映射代碼詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評(píng)論