/**
* 合并单元格的方法
*
* @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();
}
}