poi 三层表头



// TODO Auto-generated method stub
FileOutputStream fileOut = null;
File file = new File("c:/1.xls");
String[] header = { "服务器名", "IP地址", "库cache命中率_最大值", "库cache命中率_平均值",
"连接数_最大值", "连接数_平均值", "游标数_最大值", "游标数_平均值", "session数_最大值",
"session数_平均值", "事务数_最大值", "事务数_平均值", "数据库锁数_最大值", "数据库锁数_平均值",
"缓冲池命中数_最大值", "缓冲池命中数_平均值", "表空间_使用率_最大值", "表空间_使用率_平均值",
"表空间_已用空间_最大值", "表空间_已用空间_平均值", "表空间_剩余率_最大值", "表空间_剩余率_平均值",
"表空间_剩余空间_最大值", "表空间_剩余空间_平均值", "表空间_总容量_最大值", "表空间_总容量_平均值",
"连接状态" };
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFFont fontinfo = wb.createFont();
fontinfo.setFontHeightInPoints((short) 8); // 字体大小
HSSFFont fonthead = wb.createFont();
fonthead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
HSSFCellStyle cellStylename = wb.createCellStyle();// 表名样式
cellStylename.setFont(fonthead);
HSSFCellStyle cellStyleinfo = wb.createCellStyle();// 表信息样式
cellStyleinfo.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 对齐
cellStyleinfo.setFont(fontinfo);
HSSFCellStyle cellStylehead = wb.createCellStyle();// 表头样式
cellStylehead.setFont(fonthead);
cellStylehead.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStylehead.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 边框
cellStylehead.setBottomBorderColor(HSSFColor.BLACK.index);
cellStylehead.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStylehead.setLeftBorderColor(HSSFColor.BLACK.index);
cellStylehead.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStylehead.setRightBorderColor(HSSFColor.BLACK.index);
cellStylehead.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStylehead.setTopBorderColor(HSSFColor.BLACK.index);
cellStylehead.setFillForegroundColor(HSSFColor.ORANGE.index);
cellStylehead.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle cellStyle = wb.createCellStyle();// 数据单元样式
cellStyle.setWrapText(true);// 自动换行
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

int col = header.length;
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStylename);
cell.setCellValue("oracle");// 表名
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));

cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyleinfo);
cell.setCellValue("oracle"); // 信息
sheet.addMergedRegion(new Region(0, (short) 2, 0, (short) col));

int rows_max = 0;
for (int i = 0; i < header.length; i++) {
String h = header[i];
if (h.split("_").length > rows_max) {
rows_max = h.split("_").length;
}
}
Map map = new HashMap();
for (int k = 0; k < rows_max; k++) {
row = sheet.createRow((short) k + 1);
if (k == 0) {
cell = row.createCell((short) (0));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStylehead);
cell.setCellValue("序号");
sheet.addMergedRegion(new Region(k + 1, (short) 0, k
+ rows_max, (short) 0));
}
for (int i = 0; i < header.length; i++) {
String headerTemp = header[i];
String[] s = headerTemp.split("_");
String sk = "";
int num = i + 1;
if (s.length == 1) {
cell = row.createCell((short) (num));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStylehead);
sheet.addMergedRegion(new Region(1, (short) (num),
rows_max, (short) (num)));
sk = headerTemp;
cell.setCellValue(sk);
} else {
System.out.println(sk);

cell = row.createCell((short) (num));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStylehead);
int cols = 0;
if (map.containsKey(headerTemp)) {
continue;
}
for (int d = 0; d <= k; d++) {
if (d != k) {
sk += s[d] + "_";
} else{
sk += s[d];
}
}
if(map.containsKey(sk)){
continue;
}
for (int j = 0; j < header.length; j++) {
if (header[j].indexOf(sk) != -1) {
cols++;
}
}
cell.setCellValue(s[k]);
sheet
.addMergedRegion(new Region(k + 1,
(short) num, k + 1,
(short) (num + cols-1)));
if (sk.equals(headerTemp)) {
sheet.addMergedRegion(new Region(k + 1,
(short) num, k + 1 + rows_max
- s.length, (short) num));
}
}
if (s.length > k) {
if (!map.containsKey(sk)) {
String key = "";
if (k > 0) {
key = sk;
} else {
key = s[k];
}
map.put(key, null);
}
}
}
}
fileOut = new FileOutputStream(file);
// modify by xywang(2007-11-19) end
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用POI实现多级表头时,我们需要注意以下几点: 1.表头需要合并单元格:多级表头通常需要将多个单元格合并成一个单元格,以显示表头层级结构。 2.表头需要设置样式:表头的样式通常需要与正文区域有所区别,我们可以通过POI提供的样式设置方法来设置表头的样式,比如字体、颜色、边框等。 3.表头需要动态生成:如果表头层级结构比较复杂,我们需要动态生成表头,以适应不同的数据源。 下面是一个示例代码,演示了如何使用POI实现两级表头: ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头行 Row headerRow1 = sheet.createRow(0); Row headerRow2 = sheet.createRow(1); // 设置表头样式 CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerCellStyle.setAlignment(HorizontalAlignment.CENTER); headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); headerCellStyle.setBorderTop(BorderStyle.THIN); headerCellStyle.setBorderBottom(BorderStyle.THIN); headerCellStyle.setBorderLeft(BorderStyle.THIN); headerCellStyle.setBorderRight(BorderStyle.THIN); // 创建表头单元格 Cell headerCell11 = headerRow1.createCell(0); headerCell11.setCellValue("第一级表头"); headerCell11.setCellStyle(headerCellStyle); Cell headerCell12 = headerRow1.createCell(1); headerCell12.setCellValue(""); headerCell12.setCellStyle(headerCellStyle); Cell headerCell13 = headerRow1.createCell(2); headerCell13.setCellValue(""); headerCell13.setCellStyle(headerCellStyle); Cell headerCell21 = headerRow2.createCell(1); headerCell21.setCellValue("第二级表头1"); headerCell21.setCellStyle(headerCellStyle); Cell headerCell22 = headerRow2.createCell(2); headerCell22.setCellValue("第二级表头2"); headerCell22.setCellStyle(headerCellStyle); // 合并表头单元格 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); // 填充正文区域 // ... // 输出Excel文件 FileOutputStream outputStream = new FileOutputStream("example.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); ``` 在上面的代码中,我们首先创建了两行表头,分别表示两级表头。然后,我们通过设置样式,创建单元格,并合并单元格,完成了表头的设置。最后,我们可以填充正文区域,并将Excel文件输出到磁盘上。 需要注意的是,这只是一个简单的示例代码,实际应用中可能需要更加复杂的表头设置,但基本思路是类似的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值