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

view plain copy to clipboard print ?
  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. }  

import java.io.File; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.Region; public class test{ public static void main(String[] args) { // 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(); } } }

你可能感兴趣的:(oracle,数据库,session,String,Excel,header)