poi 如何实现Excel合并单元格和设置内容

/**
* 合并单元格的方法
*
* @param sheet一个HSSFSheet对象
* @param row一个行对象
* @param rowindex行索引
* @param name单元格的值
* @param a1从第几行开始
* @param a2从第几个单元格开始
* @param a3到第几行结束
* @param a4到第几个单元格结束

*
*/
public void createCell(int indexRow, int rowindex, String name, int a1,int a2, int a3, int a4,
HSSFCellStyle cellStyle, HSSFFont font,
int flag) {
HSSFRow row = this.sheet.createRow((short) indexRow);
HSSFCell cel0 = row.createCell((short) rowindex);
if (flag == 0) {
cellStyle.setBorderBottom((short) 1);
cellStyle.setBorderLeft((short) 1);
cellStyle.setBorderRight((short) 1);
cellStyle.setBorderTop((short) 1);
cellStyle.setBottomBorderColor((short) 64);
cellStyle.setAlignment((short) 1);
cellStyle.setVerticalAlignment((short) 1);
cellStyle.setDataFormat((short) 49);
}
cel0.setCellStyle(cellStyle);
cel0.setEncoding(HSSFCell.ENCODING_UTF_16);
cel0.setCellValue(name);
sheet.addMergedRegion(new Region(a1, (short) a2, a3, (short) a4));
if (cellStyle != null) {
if (font != null) {
cellStyle.setFont(font);
}
cel0.setCellStyle(cellStyle);
}
}
合并后的单元格内写入文字

public class MergCell {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“new sheet”);

HSSFRow row = sheet.createRow((short) 1);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");

for(int i = 0; i < 10; i++){
sheet.addMergedRegion(new Region(i,(short)1,i,(short)2));
}

HSSFSheet sheet1 = wb.createSheet("row sheet");
sheet1.setSelected(true);


// Write the output to a file
try {

FileOutputStream fileOut = new FileOutputStream(“workbook.xls”);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值