1. 程式人生 > >spring-boot匯出excel

spring-boot匯出excel

pring-boot匯出excel

本文使用Apache POL實現excel文件的匯出

pom.xml 新增Apache POL依賴

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

匯出excel

匯出excel比較重要的api有以下幾個,其他的並無難點

  • 建立一個excel檔案工作薄;(HSSFWorkbook workbook = new HSSFWorkbook())
  • 建立一張表;HSSFSheet sheet = workbook.createSheet("統計表")
  • 建立一行;HSSFRow row = sheet.createRow(0)
  • 填充一列資料; row.createCell(0).setCellValue("資料")
  • 設定一個單元格樣式;cell.setCellStyle(style)
  • package com.example.demo.controller;
    
    import com.example.demo.domain.User;
    import com.example.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    import java.util.List;
    
    @RestController
    @RequestMapping(value = "/excel")
    public class ExcelController {
    
        @Autowired
        private UserService userService;
    
        //建立表頭
        private void createTitle(HSSFWorkbook workbook,HSSFSheet sheet){
            HSSFRow row = sheet.createRow(0);
            //設定列寬,setColumnWidth的第二個引數要乘以256,這個引數的單位是1/256個字元寬度
            sheet.setColumnWidth(1,12*256);
            sheet.setColumnWidth(3,17*256);
    
            //設定為居中加粗
            HSSFCellStyle style = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setBold(true);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setFont(font);
    
            HSSFCell cell;
            cell = row.createCell(0);
            cell.setCellValue("ID");
            cell.setCellStyle(style);
    
    
            cell = row.createCell(1);
            cell.setCellValue("顯示名");
            cell.setCellStyle(style);
    
            cell = row.createCell(2);
            cell.setCellValue("使用者名稱");
            cell.setCellStyle(style);
    
            cell = row.createCell(3);
            cell.setCellValue("建立時間");
            cell.setCellStyle(style);
        }
    
        //生成user表excel
        @GetMapping(value = "/getUser")
        public String getUser(HttpServletResponse response) throws Exception{
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("統計表");
            createTitle(workbook,sheet);
            List<User> rows = userService.getAll();
    
            //設定日期格式
            HSSFCellStyle style = workbook.createCellStyle();
            style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    
            //新增資料行,並且設定單元格資料
            int rowNum=1;
            for(User user:rows){
                HSSFRow row = sheet.createRow(rowNum);
                row.createCell(0).setCellValue(user.getId());
                row.createCell(1).setCellValue(user.getName());
                row.createCell(2).setCellValue(user.getUsername());
                HSSFCell cell = row.createCell(3);
                cell.setCellValue(user.getCreate_time());
                cell.setCellStyle(style);
                rowNum++;
            }
    
            String fileName = "匯出excel例子.xls";
    
            //生成excel檔案
            buildExcelFile(fileName, workbook);
    
            //瀏覽器下載excel
            buildExcelDocument(fileName,workbook,response);
    
            return "download excel";
        }
    
        //生成excel檔案
        protected void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{
            FileOutputStream fos = new FileOutputStream(filename);
            workbook.write(fos);
            fos.flush();
            fos.close();
        }
    
        //瀏覽器下載excel
        protected void buildExcelDocument(String filename,HSSFWorkbook workbook,HttpServletResponse response) throws Exception{
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        }
    
    
    }