package com.hwy.test;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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.util.CellRangeAddress;
import java.io.FileOutputStream;
/**
* Created by Ay on 2016/4/16.
*/
public class PoiCellRangeAddress {
public static void main(String[] args) {
//新建excel报表
HSSFWorkbook excel = new HSSFWorkbook();
//添加一个sheet,名字叫"我的POI之旅"
HSSFSheet hssfSheet = excel.createSheet("我的POI之旅");
//单元格范围 参数(int firstRow, int lastRow, int firstCol, int lastCol)
//单元格范围 参数(开始行, 结束行, 开始列, 结束列)
CellRangeAddress cellRangeAddress =new CellRangeAddress(0, 3, 3, 9);
//在sheet里增加合并单元格
hssfSheet.addMergedRegion(cellRangeAddress);
//生成第一行
Row row = hssfSheet.createRow(0);
Cell first = row.createCell(3);
first.setCellValue("合并单元格");
//cell 位置3-9被合并成一个单元格,
//不管你怎样创建第4个cell还是第5个cell…然后在写数据,都是无法写入的。
Cell secondCell = row.createCell(10);
secondCell.setCellValue("不在合并单元格范围");
FileOutputStream fout = null;
try{
fout = new FileOutputStream("D:/students.xls");
excel.write(fout);
fout.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
结果