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

SpringBoot生成Excel文件的實(shí)現(xiàn)示例

 更新時(shí)間:2025年02月27日 09:47:26   作者:xxxmine  
本文介紹了Spring Boot項(xiàng)目中生成Excel文件,使用了Apache POI庫(kù),包括poi和poi-ooxml依賴,通過(guò)遍歷用戶信息列表,將數(shù)據(jù)寫入Excel文件,感興趣的可以了解一下

在Springboot以及其他的一些項(xiàng)目中,或許我們可能需要將數(shù)據(jù)查詢出來(lái)進(jìn)行生成Excel文件進(jìn)行數(shù)據(jù)的展示,或者用于進(jìn)行郵箱發(fā)送進(jìn)行附件添加

依賴引入

此處demo使用maven依賴進(jìn)行使用

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi</artifactId>
     <version>5.2.3</version>
</dependency>
<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>5.2.3</version>
</dependency>

依賴說(shuō)明

  • poi-ooxml:這個(gè)依賴主要用于處理 .xlsx 格式的 Excel 文件(Excel 2007 及以上版本)。它依賴于 poi 和 ooxml-schemas 等其他庫(kù),提供了對(duì) Office Open XML 格式文件的支持。
  • poi:這是 Apache POI 的核心庫(kù),提供了基本的 Excel 操作功能,包括創(chuàng)建工作簿、工作表、行和單元格等。

 代碼實(shí)例

此處我們需要將用戶的信息使用lis進(jìn)行傳輸,通過(guò)遍歷這個(gè)list集合進(jìn)行向我們事先生成的excel文件中進(jìn)行添加數(shù)據(jù)

public class ExcelGeneratorUtil {

    public static void generateExcel(List<Pitch> pitches) {
        try {
            //創(chuàng)建一個(gè)新的 Excel 工作簿對(duì)象,XSSFWorkbook 用于處理 .xlsx 格式的 Excel 文件
            Workbook workbook = new XSSFWorkbook();

            //創(chuàng)建一個(gè)文件輸出流,用于將工作簿中的數(shù)據(jù)寫入到名為 recommendedColleges.xlsx文件中
             FileOutputStream fileOut = new FileOutputStream("recommendedColleges.xlsx");
            
            //在工作簿中創(chuàng)建一個(gè)名為 Recommended Colleges 的工作表
            Sheet sheet = workbook.createSheet("Recommended Colleges");

            // 創(chuàng)建表頭 選擇第0行進(jìn)行作為表頭 并進(jìn)行設(shè)置表頭信息
            Row headerRow = sheet.createRow(0);
            Cell headerCell1 = headerRow.createCell(0);
            headerCell1.setCellValue("學(xué)校編號(hào)");
            Cell headerCell2 = headerRow.createCell(1);
            headerCell2.setCellValue("學(xué)校名稱");
            Cell headerCell3 = headerRow.createCell(2);
            headerCell3.setCellValue("專業(yè)編號(hào)");
            Cell headerCell4 = headerRow.createCell(3);
            headerCell4.setCellValue("專業(yè)名稱");
            Cell headerCell5 = headerRow.createCell(4);
            headerCell5.setCellValue("專業(yè)最低分");
            Cell headerCell6 = headerRow.createCell(5);
            headerCell6.setCellValue("專業(yè)最低位次");

            // 填充數(shù)據(jù) 初始化行號(hào)為 1
            int rowNum = 1;
            for (Pitch p : pitches) {
                //在工作表中創(chuàng)建新的行,并將行號(hào)加 1
                Row row = sheet.createRow(rowNum++);
                Cell cell1 = row.createCell(0);
                cell1.setCellValue(p.getSchoolCode());
                Cell cell2 = row.createCell(1);
                cell2.setCellValue(p.getSchoolName());
                Cell cell3 = row.createCell(2);
                cell3.setCellValue(p.getpCode());
                Cell cell4 = row.createCell(3);
                cell4.setCellValue(p.getpName());
                Cell cell5 = row.createCell(4);
                cell5.setCellValue(p.getLowestScore());
                Cell cell6 = row.createCell(5);
                cell6.setCellValue(p.getLowestRank());
            }
            
            //置每列的寬度,參數(shù) 1 是列索引,參數(shù) 2 是列寬,單位是 1/256 個(gè)字符寬度
            sheet.setColumnWidth(0, 15 * 256);
            sheet.setColumnWidth(1, 50 * 256);
            sheet.setColumnWidth(2, 15 * 256);
            sheet.setColumnWidth(3, 20 * 256);
            sheet.setColumnWidth(4, 15 * 256);
            sheet.setColumnWidth(5, 15 * 256);

            //將工作簿中的數(shù)據(jù)寫入到文件輸出流中,即保存到文件中
            workbook.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

最終實(shí)現(xiàn)了excel文件的創(chuàng)建和使用

到此這篇關(guān)于SpringBoot生成Excel文件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot生成Excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論