1、这里举例处理第一行和第二行,效果如下
1、先对每个单元格设置框线。
HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("sheet" + x);// 建立新的sheet对象
// 第一行
HSSFRow row0 = sheet.createRow((short)0);// 建立提示行
row0.setHeight((short)(40 * 20));
// 第一行样式
HSSFCellStyle cellStyle = wb.createCellStyle();
// 设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)12);
font.setColor(HSSFColor.BLACK.index);
cellStyle.setFont(font);
// 水平居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 垂直居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 设置边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 第一行第一列
HSSFCell cell0 = row0.createCell((short)0);// 建立新cell
cell0.setCellValue(“品类分布数据”);
cell0.setCellStyle(cellStyle);
// 第一行第六列(**处理框线**)
cell0 = row0.createCell((short)6);// 建立新cell
cell0.setCellValue("");
cell0.setCellStyle(cellStyle);
// 第二行
HSSFRow row1 = sheet.createRow((short)1);// 建立新行
row1.setHeight((short)(30 * 20));
// 第二行样式
cellStyle = wb.createCellStyle();
// 设置字体
font = wb.createFont();
font.setFontHeightInPoints((short)12);
font.setColor(HSSFColor.BLACK.index);
// 第二行第一列
cell0 = row1.createCell((short)0);// 建立新cell
cell0.setCellValue("栏目名称");
cell0.setCellStyle(cellStyle);
// 第二行第二列(**处理框线**)
cell0 = row1.createCell((short)1);// 建立新cell
cell0.setCellValue("");
cell0.setCellStyle(cellStyle);
// 第二行第三列(**处理框线**)
cell0 = row1.createCell((short)2);// 建立新cell
cell0.setCellValue("");
cell0.setCellStyle(cellStyle);
// 第二行第四列
cell0 = row1.createCell((short)3);// 建立新cell
cell0.setCellValue("商品数量");
cell0.setCellStyle(cellStyle);
// 第二行第五列
cell0 = row1.createCell((short)4);// 建立新cell
cell0.setCellValue("售出数量");
cell0.setCellStyle(cellStyle);
// 第二行第六列
cell0 = row1.createCell((short)5);// 建立新cell
cell0.setCellValue("销售总额");
cell0.setCellStyle(cellStyle);
// 第二行第七列
cell0 = row1.createCell((short)6);// 建立新cell
cell0.setCellValue("销售利润");
cell0.setCellStyle(cellStyle);
2、再进行单元格合并操作(顺序颠倒,会有部分单元格缺失框线)
// 合并单元格
sheet.addMergedRegion(new Region(0, (short)0, 0, (short)6));
sheet.addMergedRegion(new Region(1, (short)0, 1, (short)2));