以下是一个示例代码,可以将商品数据下载到 Excel 文档并且通过商品分类进行小计显示到每一种商品下:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelExport {
    
    public static void main(String[] args) throws IOException {
        // 模拟商品数据
        List<Product> products = new ArrayList<>();
        products.add(new Product('A', 'category1', 10));
        products.add(new Product('B', 'category1', 20));
        products.add(new Product('C', 'category2', 30));
        products.add(new Product('D', 'category2', 40));
        products.add(new Product('E', 'category2', 50));
        
        // 按商品分类进行小计
        Map<String, Integer> categoryTotalMap = new HashMap<>();
        for (Product product : products) {
            String category = product.getCategory();
            int price = product.getPrice();
            int total = categoryTotalMap.getOrDefault(category, 0) + price;
            categoryTotalMap.put(category, total);
        }
        
        // 创建Excel文档
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet('Products');
        
        // 创建表头
        Row headerRow = sheet.createRow(0);
        Cell headerCell1 = headerRow.createCell(0);
        headerCell1.setCellValue('Product Name');
        Cell headerCell2 = headerRow.createCell(1);
        headerCell2.setCellValue('Category');
        Cell headerCell3 = headerRow.createCell(2);
        headerCell3.setCellValue('Price');
        
        // 设置表头样式
        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        headerCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        headerCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        headerCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        headerCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        headerCell1.setCellStyle(headerCellStyle);
        headerCell2.setCellStyle(headerCellStyle);
        headerCell3.setCellStyle(headerCellStyle);
        
        // 填充数据
        int rowIndex = 1;
        for (Product product : products) {
            Row row = sheet.createRow(rowIndex++);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(product.getName());
            Cell cell2 = row.createCell(1);
            cell2.setCellValue(product.getCategory());
            Cell cell3 = row.createCell(2);
            cell3.setCellValue(product.getPrice());
        }
        
        // 创建小计行
        Row totalRow = sheet.createRow(rowIndex++);
        Cell totalCell1 = totalRow.createCell(0);
        totalCell1.setCellValue('Total');
        Cell totalCell2 = totalRow.createCell(1);
        Cell totalCell3 = totalRow.createCell(2);
        for (String category : categoryTotalMap.keySet()) {
            int total = categoryTotalMap.get(category);
            int categoryRowIndex = rowIndex++;
            Row categoryRow = sheet.createRow(categoryRowIndex);
            Cell categoryCell1 = categoryRow.createCell(0);
            categoryCell1.setCellValue(category);
            Cell categoryCell2 = categoryRow.createCell(1);
            Cell categoryCell3 = categoryRow.createCell(2);
            categoryCell3.setCellValue(total);
            totalCell2.setCellValue(totalCell2.getStringCellValue() + category + '\n');
            totalCell3.setCellValue(totalCell3.getNumericCellValue() + total);
        }
        
        // 设置小计行样式
        CellStyle totalCellStyle = workbook.createCellStyle();
        totalCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        totalCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        totalCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        totalCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        totalCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        totalCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        totalCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        totalCell1.setCellStyle(totalCellStyle);
        totalCell2.setCellStyle(totalCellStyle);
        totalCell3.setCellStyle(totalCellStyle);
        
        // 自动调整列宽
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);
        sheet.autoSizeColumn(2);
        
        // 保存Excel文档
        try (FileOutputStream outputStream = new FileOutputStream('products.xlsx')) {
            workbook.write(outputStream);
        }
    }
    
    static class Product {
        private String name;
        private String category;
        private int price;
        
        public Product(String name, String category, int price) {
            this.name = name;
            this.category = category;
            this.price = price;
        }
        
        public String getName() {
            return name;
        }
        
        public String getCategory() {
            return category;
        }
        
        public int getPrice() {
            return price;
        }
    }
}

这个示例代码使用了 Apache POI 库来操作 Excel 文档。首先,它创建了一个 Product 类来表示商品数据。然后,它按商品分类进行小计,并创建了一个 Excel 文档。在 Excel 文档中,它创建了一个名为 Products 的工作表,并在第一行创建了表头。然后,它填充了商品数据,并在最后创建了小计行。小计行包括总计和每个商品分类的小计。最后,它保存 Excel 文档到文件系统中。

本代码利用 Java 和 Apache POI 库实现 Excel 文件的创建、数据填充和格式设置,并通过分类汇总功能将商品数据以更加直观的表格形式展示。该代码示例可以帮助理解如何使用 Java 操作 Excel 文件,并进行简单的报表生成。

代码说明:

  1. 使用 XSSFWorkbook 类创建 Excel 文档。
  2. 使用 createSheet 方法创建名为 'Products' 的工作表。
  3. 使用 createRow 方法创建表头和数据行。
  4. 使用 createCell 方法创建单元格,并使用 setCellValue 方法设置单元格值。
  5. 使用 createCellStyle 方法创建样式,并使用 setFillForegroundColorsetFillPatternsetAlignment 等方法设置样式属性。
  6. 使用 setCellStyle 方法将样式应用于单元格。
  7. 使用 autoSizeColumn 方法自动调整列宽。
  8. 使用 FileOutputStream 类将 Excel 文档保存到文件系统中。
Java 使用 Apache POI 将商品数据导出到 Excel 并按分类进行小计

原文地址: https://www.cveoy.top/t/topic/kSLh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录