最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java使用poi生成excel的步驟

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

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

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

生成一個工作簿

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

生成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");
    //設置單元格寬度
    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("序號");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成績");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\測試生成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("序號");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成績");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\測試生成2010.xlsx");
        workbook.write(os);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

合并單元格

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

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

合并單元格后設置邊框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);

設置單元格樣式

左右居中 上下居中 自動換行

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

使用SpringMVC/SpringBoot導出excel

@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse httpServletResponse){
    try {
        //2010格式設置
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //2003格式設置
        response.setContentType("application/vnd.ms-excel");
        httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學生成績單.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("序號");
        cell0.setCellStyle(cellStyle);

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

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

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

總結(jié)

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

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

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

相關(guān)文章

  • Spring Cloud Feign性能優(yōu)化代碼實例

    Spring Cloud Feign性能優(yōu)化代碼實例

    這篇文章主要介紹了Spring Cloud Feign性能優(yōu)化代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot接收參數(shù)使用的注解實例講解

    SpringBoot接收參數(shù)使用的注解實例講解

    這篇文章主要介紹了詳解SpringBoot接收參數(shù)使用的幾種常用注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • MyBatis @Param注解的實現(xiàn)

    MyBatis @Param注解的實現(xiàn)

    本文主要介紹了MyBatis @Param注解的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • 淺談Spring6中的反射機制

    淺談Spring6中的反射機制

    Java反射機制是Java語言中一種動態(tài)(運行時)訪問、檢測、修改它本身的能力,主要作用是動態(tài)(運行時)獲取類的完整結(jié)構(gòu)信息、調(diào)用對象的方法,需要的朋友可以參考下
    2023-05-05
  • Java數(shù)據(jù)結(jié)構(gòu)之堆(優(yōu)先隊列)詳解

    Java數(shù)據(jù)結(jié)構(gòu)之堆(優(yōu)先隊列)詳解

    堆(優(yōu)先隊列)是一種典型的數(shù)據(jù)結(jié)構(gòu),其形狀是一棵完全二叉樹,一般用于求解topk問題。本文將利用Java語言實現(xiàn)堆,感興趣的可以學習一下
    2022-07-07
  • Java使用quartz實現(xiàn)定時任務示例詳解

    Java使用quartz實現(xiàn)定時任務示例詳解

    這篇文章主要為大家介紹了Java使用quartz實現(xiàn)定時任務示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • SpringBoot中間件之封裝統(tǒng)一白名單配置

    SpringBoot中間件之封裝統(tǒng)一白名單配置

    這篇文章主要介紹了SpringBoot中間件封裝統(tǒng)一白名單配置,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 聊聊@value注解和@ConfigurationProperties注解的使用

    聊聊@value注解和@ConfigurationProperties注解的使用

    這篇文章主要介紹了@value注解和@ConfigurationProperties注解的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot整合jsp,實現(xiàn)公交車站路線圖

    springboot整合jsp,實現(xiàn)公交車站路線圖

    這篇文章主要介紹了springboot整合jsp,實現(xiàn)公交車站路線圖的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2021-01-01
  • Springboot實現(xiàn)多服務器session共享

    Springboot實現(xiàn)多服務器session共享

    這篇文章主要為大家詳細介紹了Springboot實現(xiàn)多服務器session共享,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05

最新評論

磐安县| 霍城县| 晋中市| 同德县| 会宁县| 商河县| 丹凤县| 文化| 阿坝| 武功县| 清水河县| 普陀区| 新乐市| 巴彦淖尔市| 沁阳市| 县级市| 临泉县| 阳泉市| 红河县| 大连市| 山东省| 河曲县| 县级市| 利辛县| 辛集市| 鸡东县| 麟游县| 宣化县| 丽水市| 奉新县| 墨脱县| 大宁县| 馆陶县| 鸡泽县| 赫章县| 金平| 调兵山市| 安达市| 桃江县| 信宜市| 海原县|