Apache POI允许无涯教程将单元合并到一个单元中。为此,它提供了一些以单元格索引为参数并将单元格合并为单个大单元格的方法。
在下面的示例中,将创建两个单元格并将它们合并为一个单元格。
链接:https://www.learnfk.comhttps://www.learnfk.com/apache-poi-word/apache-poi-merging-cells.html
来源:LearnFk无涯教程网
package poiexample; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.util.CellRangeAddress; public class MergingCellExample { public static void main(String[] args) { try (OutputStream fileOut = new FileOutputStream("Learnfk.xls")) { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet"); Row row = sheet.createRow(1); Cell cell = row.createCell(1); cell.setCellValue("Two cells have merged"); //通过提供单元格索引来合并单元格 sheet.addMergedRegion(new CellRangeAddress(1,1,1,2)); wb.write(fileOut); }catch(Exception e) { System.out.println(e.getMessage()); } } }
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)