Java导出Excel(自定义格式)

Java导出Excel(自定义格式,非纯导出表格)

最近业务有个需求需要用到导出Excel表,按模板格式导出报关单(比较复杂,需要一格一格画),这里记录一下Excel导出怎么用
如果只是导出纯表列数据,可以不用我这种方法

这里需要引入Apache POI依赖库,
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
 //准备报表用参数
        CVVoyage cvVoyage = this.cvVoyageDao.getByVoyageId(voyageId);
        List<Order> bookingList = this.csBookingDao.getByVoyageId(cvVoyage.getId());

 // Excel表开始
        String sheetname = "申报单";
        //创建一个HSSFWorkbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetname);

        //设置宽度
        int[] width = {20, 10, 30, 30, 30, 20, 30};
        for (int columnIndex = 0; columnIndex < 7; columnIndex++) {
            sheet.setColumnWidth(columnIndex, width[columnIndex] * 256);
        }
        //设置样式集合
        Map<String, HSSFCellStyle> styles = addStyle(wb);

int rowIndex;
        int colIndex;
        //添加标题行
        HSSFRow row0 = sheet.createRow(0);
        row0.setHeight((short) (40 * 20));
        HSSFCell cell0 = row0.createCell(0);
        cell0.setCellStyle(styles.get("title"));
        String cellValues0 = "中华人民共和国海关运输工具起卸/添加物料申报单";
        cell0.setCellValue(cellValues0);
        CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 6);
        sheet.addMergedRegion(cra);

        HSSFRow row1 = sheet.createRow(1);
        row0.setHeight((short) (30 * 20));
        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(styles.get("title"));
        String cellValues1 = "DECALRATION OF OFFLAND/SUPPLY PROVISIONS/STORES TO CUSTOMS\n";
        cell1.setCellValue(cellValues1);
        CellRangeAddress cra1 = new CellRangeAddress(1, 1, 0, 6);
        sheet.addMergedRegion(cra1);

        HSSFRow row2 = sheet.createRow(2);
        for (colIndex = 0; colIndex < 7; colIndex++) {
            row2.createCell(colIndex);
            row2.getCell(colIndex).setCellStyle(styles.get("header_center"));
        }
        row2.getCell(0).setCellValue("录入编号(SERIES NUMBER)");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 3));
        row2.getCell(4).setCellValue("□起卸(OFFLAND)");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 5));
        row2.getCell(6).setCellValue("□ 添加(SUPPLY)");

// 后面的数据可以根据自己的需求写....................


 //生成文件
        String fileName = "申报单.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        OutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
  1. 添加表格样式
private Map<String, HSSFCellStyle> addStyle(HSSFWorkbook wb) {
        @SuppressWarnings({"unchecked", "rawtypes"})
        Map<String, HSSFCellStyle> styles = new HashMap();

        //设置字体
        HSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setFontHeightInPoints((short) 16);
        headFont.setBold(true);
        HSSFFont bodyFont = wb.createFont();
        bodyFont.setFontName("微软雅黑");
        bodyFont.setFontHeightInPoints((short) 10);

        //标题行样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(headFont);
        style.setWrapText(true);
        style.setFillForegroundColor((short) 27);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styles.put("title", style);

        //数据头居中样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        style.setFillForegroundColor((short) 27);
        styles.put("header_center", style);

        //数据行居中样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        styles.put("data_center", style);

        //数据行居中底色样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        style.setFillForegroundColor((short) 27);
        styles.put("data_center_color", style);

        //数据行居中底色样式2
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        style.setFillForegroundColor((short) 27);
        styles.put("data_center_color1", style);

        //数据行居左样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        style.setFillForegroundColor((short) 27);
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styles.put("data_left", style);

        //数据行居右样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styles.put("data_right", style);
        //无边框样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        styles.put("data_noborder", style);
        //无底边框样式
        style = wb.createCellStyle();
        style.setFont(bodyFont);
        style.setWrapText(true);
        styles.put("data_bottom", style);

        return styles;
    }

最终效果图
在这里插入图片描述
具体用法可以参考POI官方文档
https://www.cnblogs.com/fqfanqi/p/6172223.html

  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Apache POI 库来实现在 Java导出带有自定义表头的 Excel。 下面是一个示例代码,可以按照自己的需求进行修改: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; 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 CustomHeaderExcelExporter { public static void main(String[] args) { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头行 Row headerRow = sheet.createRow(0); // 添加表头单元格 Cell headerCell1 = headerRow.createCell(0); headerCell1.setCellValue("列1"); Cell headerCell2 = headerRow.createCell(1); headerCell2.setCellValue("列2"); Cell headerCell3 = headerRow.createCell(2); headerCell3.setCellValue("列3"); // 添加数据行和单元格 Row dataRow = sheet.createRow(1); Cell dataCell1 = dataRow.createCell(0); dataCell1.setCellValue("A"); Cell dataCell2 = dataRow.createCell(1); dataCell2.setCellValue("B"); Cell dataCell3 = dataRow.createCell(2); dataCell3.setCellValue("C"); // 调整列宽 sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.autoSizeColumn(2); // 将工作簿写入文件 try (FileOutputStream outputStream = new FileOutputStream("CustomHeaderExcel.xlsx")) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码可以将一个包含自定义表头的 Excel 导出到当前工作目录下。您可以修改代码中的表头和数据,以及导出的文件名和路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值