Apache POI Excel 合并单元格
Apache POI 允许我们将单元格合并为一个单元格。为此,它提供了将单元格索引作为参数并将单元格合并为单个大单元格的方法。
在以下示例中,我们将创建两个单元格并将它们合并为一个单元格。请参阅示例。
Apache POI 合并单元格示例
package com.yiidian;
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;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class MergingCellExample {
public static void main(String[] args) {
try (OutputStream fileOut = new FileOutputStream("yiidian.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());
}
}
}
输出结果为:
热门文章
优秀文章