poi的合并单元格和冻结行列

时间:2024-06-19 00:04:02

poi的合并单元格和冻结行列

         //创建工作薄(excel)
Workbook wb = new HSSFWorkbook();
//创建sheet
Sheet createSheet = wb.createSheet("sheet1"); //设置标题字体
Font fontTitle = wb.createFont();
fontTitle.setFontHeightInPoints((short) 18); //字体大小
fontTitle.setColor(HSSFColor.BLACK.index); //字体颜色
fontTitle.setFontName("宋体"); //字体
fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置标题单元格类型
CellStyle cellStyleTitle = wb.createCellStyle();
cellStyleTitle.setFont(fontTitle);
cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyleTitle.setWrapText(true);//设置自动换行 cellStyleTitle.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleTitle.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleTitle.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleTitle.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex()); //创建合并单元格 ---begin
CellRangeAddress region = new CellRangeAddress(0, 0, 1, 3);// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region2 = new CellRangeAddress(1, 2, 0, 0);// 起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region3 = new CellRangeAddress(1, 1, 1, 3);// 起始行号,终止行号, 起始列号,终止列号
//在sheet里增加合并单元格
createSheet.addMergedRegion(region);
createSheet.addMergedRegion(region2);
createSheet.addMergedRegion(region3); // -----------填充第一行数据-------------
Row rowTitle = createSheet.createRow(0);
Cell cellTitle = rowTitle.createCell(0);
cellTitle.setCellStyle(cellStyleTitle);
cellTitle.setCellValue("");// 设置标题内容
Cell cellTitle1_1 = rowTitle.createCell(1);
cellTitle1_1.setCellStyle(cellStyleTitle);
cellTitle1_1.setCellValue(dateStr);// 设置标题内容
// -----------填充第二行数据-------------
Row rowTitle1 = createSheet.createRow(1);
Cell cellTitle1 = rowTitle1.createCell(0);
cellTitle1.setCellStyle(cellStyleTitle);
cellTitle1.setCellValue("店名");// 设置内容
Cell cellTitle11 = rowTitle1.createCell(1);
cellTitle11.setCellStyle(cellStyleTitle);
cellTitle11.setCellValue("王总");//
Cell cellTitle111 = rowTitle1.createCell(2);
cellTitle111.setCellStyle(cellStyleTitle);
cellTitle111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
Cell cellTitle1111 = rowTitle1.createCell(3);
cellTitle1111.setCellStyle(cellStyleTitle);
cellTitle1111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
// -----------填充第三行数据-------------
Row rowTitle2 = createSheet.createRow(2);
Cell cellTitle2 = rowTitle2.createCell(1);
cellTitle2.setCellStyle(cellStyleTitle);
cellTitle2.setCellValue("装修费");// 设置内容
Cell cellTitle22 = rowTitle2.createCell(2);
cellTitle22.setCellStyle(cellStyleTitle);
cellTitle22.setCellValue("加盟费");// 设置内容
Cell cellTitle222 = rowTitle2.createCell(3);
cellTitle222.setCellStyle(cellStyleTitle);
cellTitle222.setCellValue("人员培训费");// 设置内容
// 合并单元格 ----end
// 第四行数据留着下面写 //设置表头字体
Font fontHead = wb.createFont();
fontHead.setFontHeightInPoints((short) 14); //字体大小
fontHead.setColor(Font.COLOR_NORMAL); //字体颜色
fontHead.setFontName("Microsoft Sans Serif"); //字体
fontHead.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置表头单元格类型
CellStyle cellStyleHead = wb.createCellStyle();
cellStyleHead.setFont(fontHead);
cellStyleHead.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleHead.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleHead.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyleHead.setWrapText(true);//设置自动换行 cellStyleHead.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleHead.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleHead.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleHead.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleHead.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setRightBorderColor(IndexedColors.BLACK.getIndex()); //创建第一行,标题
Row row = createSheet.createRow(3);
String[] cellHead = {"", "","", ""};
// 设置列宽
double[] titleWidth = {10, 24, 24, 24};
for (int i = 0; i < cellHead.length; i++) {
Cell createCell = row.createCell(i);
createCell.setCellValue(cellHead[i]);
createCell.setCellStyle(cellStyleHead);
} //设置内容字体
Font fontData = wb.createFont();
fontData.setFontHeightInPoints((short) 14); //字体大小
fontData.setColor(Font.COLOR_NORMAL); //字体颜色
fontData.setFontName("Microsoft Sans Serif"); //字体
//font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置内容单元格类型
CellStyle cellStyleDataOdd = wb.createCellStyle();
cellStyleDataOdd.setFont(fontData);
cellStyleDataOdd.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyleDataOdd.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleDataOdd.setWrapText(true); cellStyleDataOdd.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleDataOdd.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleDataOdd.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleDataOdd.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleDataOdd.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setRightBorderColor(IndexedColors.BLACK.getIndex()); try {
int no = 1;
for (int i = 0; i < datas.length(); i++) {
JSONObject dayData = datas.getJSONObject(i);
String date = dayData.optString("date");
JSONArray detail = dayData.getJSONArray("detail"); for (int k = 0; k < detail.length(); k++) {
JSONObject ss = detail.getJSONObject(k); row = createSheet.createRow(k + 4);
int j = 0;
// 店名
Cell cell1 = row.createCell(j++);
cell1.setCellValue(ss.optString("xxxxx"));
cell1.setCellStyle(cellStyleDataOdd);
// 装修费
Cell cell2 = row.createCell(j++);
cell2.setCellValue(ss.optString("xxxxx"));
cell2.setCellStyle(cellStyleDataOdd);
// 加盟费
Cell cell7 = row.createCell(j++);
cell7.setCellValue(ss.optString("xxxxxx"));
cell7.setCellStyle(cellStyleDataOdd);
// 人员培训费
Cell cellH = row.createCell(j++);
cellH.setCellValue(ss.optString("xxxxxx"));
cellH.setCellStyle(cellStyleDataOdd);
}
} } catch (JSONException e) {
e.printStackTrace();
} // 设置列宽
for (int i = 0; i < titleWidth.length; i++) {
createSheet.setColumnWidth((short) i, (short) titleWidth[i] * 256);
} // 设置上面四行冻结
createSheet.createFreezePane( 0, 4, 1, 4); // 前两个参数是你要用来拆分的列数和行数。后两个参数是下面窗口的可见象限,其中第三个参数是右边区域可见的左边列数,第四个参数是下面区域可见的首行