apache poi合并单元

本文概述

  • Apache POI合并单元格示例
Apache POI允许我们将单元合并为一个单元。为此,它提供了一些以单元格索引为参数并将单元格合并为单个大单元格的方法。
在下面的示例中,我们将创建两个单元格并将它们合并为一个单元格。参见示例。
Apache POI合并单元格示例
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("srcmini.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"); //Merging cells by providing cell index sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); wb.write(fileOut); }catch(Exception e) { System.out.println(e.getMessage()); } } }

【apache poi合并单元】输出:
apache poi合并单元

文章图片

    推荐阅读