本文概述
- 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 excel字体
- apache poi excel单元格颜色
- apache poi excel单元格边界
- apache poi excel单元格
- apache poi excel对齐单元格
- apache poi excel日期单元格
- apache poi excel工作簿
- apache poi excel文档处理
- apache poi安装