java 利用poi根据excel模板导出数据(三)

3 篇文章 0 订阅

本文是  java 利用poi根据excel模板导出数据(一) 的优化版本

主要针对于  fillBodyData 方法这块,具体如下

源代码:

private static void fillBodyData(Sheet sheet, int startRow, List<NutMap> bodyList, int[] colWidth) {
 
        // 碰到 时间、金额等需要转换的  在setCellValue 里面操作
        for (int rowNum = 0; rowNum < bodyList.size(); rowNum++) {
            NutMap map = bodyList.get(rowNum);
            Row row = sheet.createRow(rowNum + startRow);
            int colNum = 0;
            // comm
            Cell accountNumberCell = row.createCell(colNum++, CellType.STRING);
            String accountNumber = map.getString("comm");
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(accountNumber));
            accountNumberCell.setCellValue(accountNumber);
            // a
            Cell accountNameCell = row.createCell(colNum++, CellType.STRING);
            String accountName = map.getString("a");
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(accountName));
            accountNameCell.setCellValue(accountName);
            // b
            Cell mobileCell = row.createCell(colNum++, CellType.STRING);
            String mobile = map.getString("b");
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(mobile));
            mobileCell.setCellValue(mobile);
 
            // c
            Cell addressCell = row.createCell(colNum++, CellType.STRING);
            String address = map.getString("c");
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(address));
            addressCell.setCellValue(address);
 
            // d
            Cell meterNoCell = row.createCell(colNum++, CellType.STRING);
            String meterNo = map.getString("d");
            meterNoCell.setCellValue(meterNo);
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(meterNo));
 
            // e
            Cell uncollectedNumberCell = row.createCell(colNum++, CellType.STRING);
            String uncollectedNumber = map.getString("e");
            uncollectedNumberCell.setCellValue(uncollectedNumber);
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(uncollectedNumber));
 
            // f
            Cell uncollectedDosageCell = row.createCell(colNum++, CellType.STRING);
            String uncollectedDosage = map.getString("f");
            uncollectedDosageCell.setCellValue(uncollectedDosage);
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(uncollectedDosage));
 
            // g
            Cell uncollectedDosageCell1 = row.createCell(colNum++, CellType.STRING);
            String uncollectedDosage1 = map.getString("g");
            uncollectedDosageCell1.setCellValue(uncollectedDosage1);
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(uncollectedDosage1));
 
            // h
            Cell uncollectedDosageCell2 = row.createCell(colNum++, CellType.STRING);
            String uncollectedDosage2 = map.getString("h");
            uncollectedDosageCell2.setCellValue(uncollectedDosage2);
            colWidth[colNum - 1] = Math.max(colWidth[colNum - 1], Strings.charLength(uncollectedDosage2));
        }
    }

现在的


    private void fillBodyData(XSSFWorkbook workbook, Sheet sheet, int startRow, int startCol, List<List<String>> bodyList, int[] colWidth) {

        // 单元格样式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        // 设置单元格的水平对齐类型
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格的垂直对齐类型
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置样式对象,这里仅设置了边框属性
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框

        for (int rowNum = 0; rowNum < bodyList.size(); rowNum++) {
            List<String> rowData = bodyList.get(rowNum);
            Row row = sheet.getRow(rowNum + startRow);
            if (null == row) {
                row = sheet.createRow(rowNum + startRow);
                row.setHeight((short) 440);
            }
            int colNum = startCol;

            for (String cellValue : rowData) {
                Cell rowCell = row.getCell(colNum);
                if (null == rowCell) {
                    rowCell = row.createCell(colNum, CellType.STRING);
                    rowCell.setCellStyle(cellStyle);
                }
                colWidth[colNum - startCol] = Math.max(colWidth[colNum - startCol], Strings.charLength(cellValue));
                rowCell.setCellValue(cellValue);
                colNum++;
            }
        }
    }

相当于不用一个一个列去渲染了直接循环解决,

两个参数

1. startRow 起始行号  从0开始

2.startCol  起始列号  从0开始

当然具体的要根据需求来,也建议大家自己去去优化

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!您可以使用JavaPOI库来根据现有的模板导出Excel文件。下面是一个简单的示例代码,演示了如何使用POI来实现这个需求: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; public class ExcelExportExample { public static void main(String[] args) { String templatePath = "path/to/template.xlsx"; String outputPath = "path/to/output.xlsx"; try { FileInputStream fis = new FileInputStream(templatePath); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); // 在模板中找到需要填充数据的位置,假设要填充的单元格是A1 Row row = sheet.getRow(0); Cell cell = row.getCell(0); // 填充数据到单元格中 cell.setCellValue("Hello, World!"); // 将修改后的Excel文件写入输出流 FileOutputStream fos = new FileOutputStream(outputPath); workbook.write(fos); // 关闭资源 fos.close(); workbook.close(); System.out.println("Excel导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,您需要将`templatePath`替换为实际的模板文件路径,`outputPath`替换为导出Excel文件保存路径。然后,您可以通过`Workbook`对象获取要填充数据的单元格,并使用`setCellValue`方法将数据填充到单元格中。最后,通过`FileOutputStream`将修改后的Excel写入到输出流中。 希望这个示例对您有帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值