Apache POI 实现分栏以及去表格边框实现

通过POI实现分栏操作

目前没有直接的POI分栏Word API,只能是用表格来模拟实现,创建一个1行2列的表格,然后将表格的边框去掉。

      //创建一个表格
        XWPFTable table = doc.createTable(1, 2);
        XWPFTableCell firstCell = table.getRow(0).getCell(0);
        firstCell.addParagraph().createRun().addPicture(
                new FileInputStream("D:\\Users\\lvhb\\Desktop\\right.png")
                , XWPFDocument.PICTURE_TYPE_PNG, "Generated",
                //设置图片宽高
                Units.toEMU(140), Units.toEMU(140)
        );

        XWPFTableCell secondCell = table.getRow(0).getCell(1);
        secondCell.addParagraph().createRun().addPicture(
                new FileInputStream("D:\\Users\\lvhb\\Desktop\\left.png")
                , XWPFDocument.PICTURE_TYPE_PNG, "Generated",
                //设置图片宽高
                Units.toEMU(140), Units.toEMU(140)
        );

通过表格来间接模拟实现分栏。

去掉表格的边框

首先需要获取到表格的单元格,根据获取到的单元格来具体实现去掉哪边对应的边框,分别有top、bottom、left、right。

       CTTc ctTc1 = firstCell.getCTTc();
        CTTcPr ctTcPr = ctTc1.addNewTcPr();
        ctTc1.getTcPr().addNewTcBorders().addNewLeft().setVal(STBorder.NIL);//设置无边框
        ctTc1.getTcPr().addNewTcBorders().addNewRight().setVal(STBorder.NIL);//设置无边框
        ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER); //设置文字居中

注意,新的单元格调用getCTTc()方法时返回的是null,所以此时要先为此单元格添加一个Tc,调用addNewTcPr()方法。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Controller 中使用 Apache POI 操作 Excel 表格可以按照以下步骤进行: 1. 导入 Apache POI 的依赖包。可以通过 Maven 或 Gradle 等工具进行导入。 2. 创建一个 Java 类,该类中定义一个方法用于读取 Excel 表格中的数据。 ```java import java.io.File; import java.io.FileInputStream; 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.ss.usermodel.WorkbookFactory; public class ExcelReader { public void readExcel(String filePath) throws IOException { // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(filePath)); // 创建工作簿 Workbook workbook = WorkbookFactory.create(inputStream); // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 遍历工作表中的每一行和每一列 for (Row row : sheet) { for (Cell cell : row) { // 获取单元格的值 String cellValue = cell.getStringCellValue(); // 输出单元格的值 System.out.print(cellValue + "\t"); } System.out.println(); } // 关闭工作簿和输入流 workbook.close(); inputStream.close(); } } ``` 3. 在 Controller 中调用该方法,传入 Excel 表格的文件路径即可实现操作。 ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/excel") public class ExcelController { @GetMapping("/read") @ResponseBody public void readExcel() throws IOException { String filePath = "path/to/excel/file.xlsx"; ExcelReader reader = new ExcelReader(); reader.readExcel(filePath); } } ``` 需要注意的是,在使用 Apache POI 操作 Excel 表格时,需要根据 Excel 文件的格式选择不同的 API,如 HSSF(适用于 Excel 2003 及以前的版本)和 XSSF(适用于 Excel 2007 及以后的版本)。同时,还需要注意关闭工作簿和输入流,避免资源泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值