POI如何合并单元格
生活随笔
收集整理的這篇文章主要介紹了
POI如何合并单元格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author longrong.lang
* @version 1.0
* @description
* @date 2020/9/15 9:32
*/
public class MergeCellDemo {
public static void main(String[] args) throws IOException {
//新建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//新建工作表
XSSFSheet sheet = xssfWorkbook.createSheet("工作表1");
//指定合并開始行、合并結束行 合并開始列、合并結束列
CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, 1);
//添加要合并地址到表格
sheet.addMergedRegion(rangeAddress);
//創建行,指定起始行號,從0開始
XSSFRow row = sheet.createRow(0);
//創建單元格,指定起始列號,從0開始
XSSFCell cell = row.createCell(0);
//設置單元格內容
cell.setCellValue("我是合并后的單元格");
//創建樣式對象
CellStyle style = xssfWorkbook.createCellStyle();
//設置樣式對齊方式:水平垂直居中
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
//設定填充單色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//設定背景顏色
style.setFillForegroundColor(IndexedColors.PINK.getIndex());
//為指定單元格設定樣式
cell.setCellStyle(style);
FileOutputStream fileOutputStream = new FileOutputStream("d:\MergeCellDemo.xlsx");
xssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
}
}
總結
以上是生活随笔為你收集整理的POI如何合并单元格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#中输入文本文字,将输入的文字生成图片
- 下一篇: 利用Kotlin扩展函数实现任意View