POI EXCEL多表头的处理代码收藏

本文介绍了一个Java程序示例,该程序使用Apache POI库创建并填充一个包含多种数据类型的Excel文件。示例展示了如何设置单元格样式、合并单元格以及如何处理复杂的表头结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import java.io.File;   
  2. import java.io.FileOutputStream;   
  3. import java.util.HashMap;   
  4. import java.util.Map;   
  5.   
  6. import org.apache.poi.hssf.usermodel.HSSFCell;   
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  8. import org.apache.poi.hssf.usermodel.HSSFFont;   
  9. import org.apache.poi.hssf.usermodel.HSSFRow;   
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  12. import org.apache.poi.hssf.util.HSSFColor;   
  13. import org.apache.poi.hssf.util.Region;   
  14.   
  15. public class test{   
  16.        
  17. public static void main(String[] args) {   
  18. // TODO Auto-generated method stub      
  19. FileOutputStream fileOut = null;      
  20. File file = new File("c:/1.xls");      
  21. String[] header = { "服务器名""IP地址""库cache命中率_最大值""库cache命中率_平均值",      
  22.                     "连接数_最大值""连接数_平均值""游标数_最大值""游标数_平均值""session数_最大值",      
  23.                     "session数_平均值""事务数_最大值""事务数_平均值""数据库锁数_最大值""数据库锁数_平均值",      
  24.                     "缓冲池命中数_最大值""缓冲池命中数_平均值""表空间_使用率_最大值""表空间_使用率_平均值",      
  25.                     "表空间_已用空间_最大值""表空间_已用空间_平均值""表空间_剩余率_最大值""表空间_剩余率_平均值",      
  26.                     "表空间_剩余空间_最大值""表空间_剩余空间_平均值""表空间_总容量_最大值""表空间_总容量_平均值",      
  27.                     "连接状态" };   
  28. try {      
  29.     HSSFWorkbook wb = new HSSFWorkbook();      
  30.     HSSFSheet sheet = wb.createSheet("new sheet");      
  31.     HSSFFont fontinfo = wb.createFont();      
  32.     fontinfo.setFontHeightInPoints((short8); // 字体大小      
  33.     HSSFFont fonthead = wb.createFont();      
  34.     fonthead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗      
  35.     HSSFCellStyle cellStylename = wb.createCellStyle();// 表名样式      
  36.     cellStylename.setFont(fonthead);      
  37.     HSSFCellStyle cellStyleinfo = wb.createCellStyle();// 表信息样式      
  38.     cellStyleinfo.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 对齐      
  39.     cellStyleinfo.setFont(fontinfo);      
  40.     HSSFCellStyle cellStylehead = wb.createCellStyle();// 表头样式      
  41.     cellStylehead.setFont(fonthead);      
  42.     cellStylehead.setAlignment(HSSFCellStyle.ALIGN_CENTER);      
  43.     cellStylehead.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 边框      
  44.     cellStylehead.setBottomBorderColor(HSSFColor.BLACK.index);      
  45.     cellStylehead.setBorderLeft(HSSFCellStyle.BORDER_THIN);      
  46.     cellStylehead.setLeftBorderColor(HSSFColor.BLACK.index);      
  47.     cellStylehead.setBorderRight(HSSFCellStyle.BORDER_THIN);      
  48.     cellStylehead.setRightBorderColor(HSSFColor.BLACK.index);      
  49.     cellStylehead.setBorderTop(HSSFCellStyle.BORDER_THIN);      
  50.     cellStylehead.setTopBorderColor(HSSFColor.BLACK.index);      
  51.     cellStylehead.setFillForegroundColor(HSSFColor.ORANGE.index);      
  52.     cellStylehead.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);      
  53.     HSSFCellStyle cellStyle = wb.createCellStyle();// 数据单元样式      
  54.     cellStyle.setWrapText(true);// 自动换行      
  55.     cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);      
  56.     cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);      
  57.     cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);      
  58.     cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);      
  59.     cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);      
  60.     cellStyle.setRightBorderColor(HSSFColor.BLACK.index);      
  61.     cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);      
  62.     cellStyle.setTopBorderColor(HSSFColor.BLACK.index);      
  63.           
  64.     int col = header.length;      
  65.     HSSFRow row = sheet.createRow((short0);      
  66.     HSSFCell cell = row.createCell((short0);      
  67.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);      
  68.     cell.setCellStyle(cellStylename);      
  69.     cell.setCellValue("oracle");// 表名      
  70.     sheet.addMergedRegion(new Region(0, (short00, (short1));      
  71.      
  72.     cell = row.createCell((short2);      
  73.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);      
  74.     cell.setCellStyle(cellStyleinfo);      
  75.     cell.setCellValue("oracle"); // 信息      
  76.     sheet.addMergedRegion(new Region(0, (short20, (short) col));      
  77.      
  78.     int rows_max = 0;      
  79.     for (int i = 0; i < header.length; i++) {      
  80.         String h = header[i];      
  81.         if (h.split("_").length > rows_max) {      
  82.             rows_max = h.split("_").length;      
  83.         }      
  84.     }      
  85.     Map map = new HashMap();      
  86.     for (int k = 0; k < rows_max; k++) {      
  87.         row = sheet.createRow((short) k + 1);      
  88.         if (k == 0) {      
  89.             cell = row.createCell((short) (0));      
  90.             cell.setEncoding(HSSFCell.ENCODING_UTF_16);      
  91.             cell.setCellStyle(cellStylehead);      
  92.             cell.setCellValue("序号");      
  93.             sheet.addMergedRegion(new Region(k + 1, (short0, k      
  94.                     + rows_max, (short0));      
  95.         }      
  96.         for (int i = 0; i < header.length; i++) {      
  97.             String headerTemp = header[i];      
  98.             String[] s = headerTemp.split("_");      
  99.             String sk = "";      
  100.             int num = i + 1;      
  101.             if (s.length == 1) {      
  102.                 cell = row.createCell((short) (num));      
  103.                 cell.setEncoding(HSSFCell.ENCODING_UTF_16);      
  104.                 cell.setCellStyle(cellStylehead);      
  105.                 sheet.addMergedRegion(new Region(1, (short) (num),      
  106.                         rows_max, (short) (num)));      
  107.                 sk = headerTemp;      
  108.                 cell.setCellValue(sk);      
  109.             } else {      
  110.                 System.out.println(sk);      
  111.                       
  112.                 cell = row.createCell((short) (num));      
  113.                 cell.setEncoding(HSSFCell.ENCODING_UTF_16);      
  114.                 cell.setCellStyle(cellStylehead);      
  115.                 int cols = 0;      
  116.                 if (map.containsKey(headerTemp)) {      
  117.                     continue;      
  118.                 }      
  119.                 for (int d = 0; d <= k; d++) {      
  120.                     if (d != k) {      
  121.                         sk += s[d] + "_";      
  122.                     } else{      
  123.                         sk += s[d];      
  124.                     }      
  125.                 }      
  126.                 if(map.containsKey(sk)){      
  127.                     continue;      
  128.                 }      
  129.                 for (int j = 0; j < header.length; j++) {      
  130.                     if (header[j].indexOf(sk) != -1) {      
  131.                         cols++;      
  132.                     }      
  133.                 }      
  134.                 cell.setCellValue(s[k]);      
  135.                 sheet      
  136.                         .addMergedRegion(new Region(k + 1,      
  137.                                 (short) num, k + 1,      
  138.                                 (short) (num + cols-1)));      
  139.                 if (sk.equals(headerTemp)) {      
  140.                     sheet.addMergedRegion(new Region(k + 1,      
  141.                             (short) num, k + 1 + rows_max      
  142.                                     - s.length, (short) num));      
  143.                 }      
  144.             }      
  145.             if (s.length > k) {      
  146.                 if (!map.containsKey(sk)) {      
  147.                     String key = "";      
  148.                     if (k > 0) {      
  149.                         key = sk;      
  150.                     } else {      
  151.                         key = s[k];      
  152.                     }      
  153.                     map.put(key, null);      
  154.                 }      
  155.             }      
  156.         }      
  157.     }      
  158.     fileOut = new FileOutputStream(file);      
  159.     // modify by xywang(2007-11-19) end      
  160.     wb.write(fileOut);      
  161. catch (Exception e) {      
  162.     e.printStackTrace();      
  163. }      
  164.   
  165.   
  166. }   
  167. }  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值