Spring Boot Web应用程序下载Excel文件 - simplesolution


在本文中,我们将逐步创建Spring Boot Web应用程序并实现下载Excel文件功能。我使用Spring Tool Suite 4创建用于演示的应用程序和代码编辑器。点击标题见原文图示。
如果您正在使用Maven,则在xml下面添加:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

实现ExcelFileExporter类:

public class ExcelFileExporter {
    
    public static ByteArrayInputStream contactListToExcelFile(List<Customer> customers) {
        try(Workbook workbook = new XSSFWorkbook()){
            Sheet sheet = workbook.createSheet("Customers");
            
            Row row = sheet.createRow(0);
            CellStyle headerCellStyle = workbook.createCellStyle();
            headerCellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
            headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            
// Creating header
            Cell cell = row.createCell(0);
            cell.setCellValue(
"First Name");
            cell.setCellStyle(headerCellStyle);
            
            cell = row.createCell(1);
            cell.setCellValue(
"Last Name");
            cell.setCellStyle(headerCellStyle);
    
            cell = row.createCell(2);
            cell.setCellValue(
"Mobile");
            cell.setCellStyle(headerCellStyle);
    
            cell = row.createCell(3);
            cell.setCellValue(
"Email");
            cell.setCellStyle(headerCellStyle);
            
            
// Creating data rows for each customer
            for(int i = 0; i < customers.size(); i++) {
                Row dataRow = sheet.createRow(i + 1);
                dataRow.createCell(0).setCellValue(customers.get(i).getFirstName());
                dataRow.createCell(1).setCellValue(customers.get(i).getLastName());
                dataRow.createCell(2).setCellValue(customers.get(i).getMobileNumber());
                dataRow.createCell(3).setCellValue(customers.get(i).getEmail());
            }
    
            
// Making size of column auto resize to fit with data
            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);
            sheet.autoSizeColumn(2);
            sheet.autoSizeColumn(3);
            
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return new ByteArrayInputStream(outputStream.toByteArray());
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

对外控制类:

@Controller
public class DownloadExcelController {
    
    @RequestMapping("/")
    public String index() {
        return
"index";
    }
    
    @GetMapping(
"/download/customers.xlsx")
    public void downloadCsv(HttpServletResponse response) throws IOException {
        response.setContentType(
"application/octet-stream");
        response.setHeader(
"Content-Disposition", "attachment; filename=customers.xlsx");
        ByteArrayInputStream stream = ExcelFileExporter.contactListToExcelFile(createTestData());
        IOUtils.copy(stream, response.getOutputStream());
    }

    private List<Customer> createTestData(){
        List<Customer> customers = new ArrayList<Customer>();
        customers.add(new Customer(
"Vernon", "Barlow", "0123456789", "test1@simplesolution.dev"));
        customers.add(new Customer(
"Maud", "Brock", "0123456788", "test2@simplesolution.dev"));
        customers.add(new Customer(
"Chyna", "Cowan", "0123456787", "test3@simplesolution.dev"));
        customers.add(new Customer(
"Krisha", "Tierney", "0123456786", "test4@simplesolution.dev"));
        customers.add(new Customer(
"Sherry", "Rosas", "0123456785", "test5@simplesolution.dev"));
        return customers;
    }
}

访问:http://localhost:8080/download/customers.xlsx


源码下载