Spring Boot - Excel導出功能

1.引入poi依賴

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

2.建立Excel導出工具類

public class ExcelUtils {

    /**
     * Excel表格導出
     *
     * @param response    HttpServletResponse對象
     * @param excelData   Excel表格的數據,封裝爲List<List<String>>
     * @param sheetName   sheet的名字
     * @param fileName    導出Excel的文件名
     * @param columnWidth Excel表格的寬度
     * @throws IOException 拋IO異常
     */
    public static void exportExcel(HttpServletResponse response,
                                   List<List<String>> excelData,
                                   String sheetName,
                                   String fileName,
                                   int columnWidth) throws IOException {
        //聲明一個工做簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一個表格,設置表格名稱
        HSSFSheet sheet = workbook.createSheet(sheetName);
        //設置表格列寬度
        sheet.setDefaultColumnWidth(columnWidth);

        //寫入List<List<String>>中的數據
        int rowIndex = 0;
        for (List<String> data : excelData) {
            //建立一個row行,而後自增1
            HSSFRow row = sheet.createRow(rowIndex++);
            //遍歷添加本行數據
            for (int i = 0; i < data.size(); i++) {
                //建立一個單元格
                HSSFCell cell = row.createCell(i);
                //建立一個內容對象
                HSSFRichTextString text = new HSSFRichTextString(data.get(i));
                //將內容對象的文字內容寫入到單元格中
                cell.setCellValue(text);
            }
        }

        //準備將Excel的輸出流經過response輸出到頁面下載
        //八進制輸出流
        response.setContentType("application/octet-stream");
        //設置導出Excel的名稱
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //刷新緩衝
        response.flushBuffer();
        //workbook將Excel寫入到response的輸出流中,供頁面下載該Excel文件
        workbook.write(response.getOutputStream());
        //關閉workbook
        workbook.close();
    }
}

3.建立service

@Service
public class ExcelService {

    public List<List<String>> excelTest() {
        List<List<String>> excelData = new ArrayList<>();

        //設置表頭
        List<String> head = new ArrayList<>();
        head.add("列1");
        head.add("列2");
        head.add("列3");
        excelData.add(head);

        //設置數據
        List<String> data1 = new ArrayList<>();
        data1.add("A1");
        data1.add("A2");
        data1.add("A3");
        excelData.add(data1);
        List<String> data2 = new ArrayList<>();
        data2.add("B1");
        data2.add("B2");
        data2.add("B3");
        excelData.add(data2);
        
        return excelData;
    }
}

4.建立controller

@RestController
@RequestMapping("/demo/excel")
public class ExcelController {
    @Autowired
    private ExcelService excelService;

    /**
     * Excel表格導出接口
     * http://localhost:8998/demo/excel/export
     *
     * @param response response對象
     * @throws IOException 拋IO異常
     */
    @GetMapping("/export")
    public void excelDownload(HttpServletResponse response) throws IOException {
        List<List<String>> excelData = excelService.excelTest();
        String sheetName = "測試";
        String fileName = "test.xls";
        ExcelUtils.exportExcel(response, excelData, sheetName, fileName, 15);
    }
}

5.啓動項目,瀏覽器中訪問 http://localhost:8998/demo/excel/export ,獲得文件以下:

 

 

項目已上傳github:https://github.com/HeliosFz/SpringBoot-Excelios

相關文章
相關標籤/搜索