poi创建excle

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
相关功能:
  HSSF - 提供读写Microsoft Excel格式档案的功能。
  XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
  HWPF - 提供读写Microsoft Word格式档案的功能。
  HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
  HDGF - 提供读写Microsoft Visio格式档案的功能


实例:将上篇解析出xml文件节点的数据写入excle
导入poi的jar包

创建excle文件及工作表

 1 private static HSSFWorkbook workbook = null;
 2 private static HSSFSheet sheets[] = null;
 3 public void createExcel(String fileDir, List<File> files) throws Exception {
 4         // 创建读写excle文档的workbook
 5         workbook = new HSSFWorkbook();
 6         // 新建文件
 7         FileOutputStream out = null;
 8 
 9         sheets = new HSSFSheet[files.size()]; //有多少个xml创建多少个工作表
10         // 一个xml文件创建对应的sheet
11         for (int i = 0; i < files.size(); i++) {
12             String name[] = files.get(i).toString().split("\\\\");
13             // 添加Worksheet,工作表名称与xml文件相对应
14             sheets[i] = workbook.createSheet(name[name.length - 2]);
15         }
16         insertDataToExcle(files); //向excle中导入数据
17         try {
18             out = new FileOutputStream(fileDir);
19             workbook.write(out);
20         } catch (Exception e) {
21             throw e;
22         } finally {
23             out.close();
24         }
25 }

excle写入数据

  1 public static void insertDataToExcle(List<File> files) {
  2         String topPub[] = { "服务中文名", "接口名", "服务名", "数据格式", "调用方式", "功能描述", "处理逻辑", "START", "input", "head(公共信息)" };
  3         String cellName[] = { "字段名称", "", "类型/结构元素", "最大长度", "默认值", "必输", "描述", "说明" };
  4         XmlElements xmlele = new XmlElements();
  5 
  6         // 循环excle中已创建的工作表并插入数据
  7         for (int i = 0; i < sheets.length; i++) {
  8             List<Element> inEleNode = xmlele.getPartEleNode(xmlele.getStartElement(xmlele.getRootElement(files.get(i))),"input");
  9             Map<String, String> inallNode = xmlele.getPartAllNode(xmlele.getStartElement(xmlele.getRootElement(files.get(i))), "input");
 10             List<Element> outEleNode = xmlele.getPartEleNode(xmlele.getStartElement(xmlele.getRootElement(files.get(i))), "output");
 11             Map<String, String> outallNode = xmlele.getPartAllNode(xmlele.getStartElement(xmlele.getRootElement(files.get(i))), "output");
 12             // 合并单元格
 13             for (int j = 0; j < 15; j++) {
 14                 if (j < 7) {
 15                 //new CellRangeAddress(a, b, c, d)获取单元格地址a,b从第几行到第几行,c,d是从第几列到第几列
 16                     sheets[i].addMergedRegion(new CellRangeAddress(j, j, 0, 1));  //将前7行的强两列合并
 17                     sheets[i].addMergedRegion(new CellRangeAddress(j, j, 2, 7)); //将前7行的三到八列合并
 18                 } else if (j < 10 || j == 13) {
 19                     sheets[i].addMergedRegion(new CellRangeAddress(j, j, 0, 7));
 20                 }
 21             }
 22             int indataNum;
 23             if (inEleNode == null) {
 24                 indataNum = 0;
 25             } else {
 26                 indataNum = inEleNode.size();
 27             }
 28             int outdataNum;
 29             if (outEleNode == null) {
 30                 outdataNum = 0;
 31             } else {
 32                 outdataNum = outEleNode.size();
 33             }
 34 
 35             // 向excle中插入数据
 36             for (int r = 0; r < (15 + indataNum + 7 + outdataNum); r++) {
 37                 HSSFRow row = sheets[i].createRow(r); //创建行
 38                 for (int c = 0; c < 8; c++) {   //设置当前行前八列的值
 39                     if (c == 0 && r < 10) {
 40                         HSSFCell cell = row.createCell(c);  //创建列
 41                         cell.setCellValue(topPub[r]);       //将该单元格设置值
 42                     }
 43 
 44                     if (r == 0 && c == 2) {
 45                         HSSFCell cell = row.createCell(c);
 46                         String des = null;
 47                         if (xmlele.getRootElement(files.get(i)).element("description") == null) {
 48                             des = "";
 49                         } else {
 50                             des = (String) xmlele.getRootElement(files.get(i)).element("description").getData();
 51                         }
 52                         cell.setCellValue(des);
 53                     }
 54 
 55                     if (r == 1 && c == 2) {
 56                         HSSFCell cell = row.createCell(c);
 57                         String name = xmlele.getRootElement(files.get(i)).attributeValue("name");
 58                         cell.setCellValue(name);
 59                     }
 60                     // input
 61                     if (r == 10 || r == 14 || r == (15 + indataNum + 3) || r == (15 + indataNum + 6)) {
 62                         HSSFCell cell = row.createCell(c);
 63                         cell.setCellValue(cellName[c]);
 64                     }
 65 
 66                     if (r == 13 && c == 0) {
 67                         HSSFCell cell = row.createCell(c);
 68                         cell.setCellValue("content(业务数据)");
 69                     }
 70                     // input数据导入
 71                     if (r > 14 && r < (15 + indataNum)) {
 72                         if (c == 0) {
 73                             HSSFCell cell = row.createCell(c);
 74                             cell.setCellValue(inEleNode.get(r - 15).attributeValue("name"));
 75                         }
 76                         if (c == 2) {
 77                             HSSFCell cell = row.createCell(c);
 78                             cell.setCellValue(inEleNode.get(r - 15).attributeValue("type"));
 79                         }
 80                         if (c == 6) {
 81                             HSSFCell cell = row.createCell(c);
 82                             cell.setCellValue(inallNode.get(inEleNode.get(r - 15).attributeValue("name")));
 83                         }
 84                     }
 85 
 86                     // output
 87                     if (r == (15 + indataNum + 1) && c == 0) {
 88                         HSSFCell cell = row.createCell(c);
 89                         cell.setCellValue("output");
 90                         sheets[i].addMergedRegion(new CellRangeAddress(r, r, 0, 7));
 91                     }
 92 
 93                     if (r == (15 + indataNum + 2) && c == 0) {
 94                         HSSFCell cell = row.createCell(c);
 95                         cell.setCellValue("head(公共信息)");
 96                         sheets[i].addMergedRegion(new CellRangeAddress(r, r, 0, 7));
 97                     }
 98 
 99                     if (r == (15 + indataNum + 5) && c == 0) {
100                         HSSFCell cell = row.createCell(c);
101                         cell.setCellValue("content(业务数据)");
102                         sheets[i].addMergedRegion(new CellRangeAddress(r, r, 0, 7));
103                     }
104 
105                     if (r > 15 + indataNum + 6 && r < (15 + indataNum + 7 + outdataNum)) {
106                         if (c == 0) {
107                             HSSFCell cell = row.createCell(c);
108                             cell.setCellValue(outEleNode.get(r - (15 + indataNum + 7)).attributeValue("name"));
109                         }
110                         if (c == 2) {
111                             HSSFCell cell = row.createCell(c);
112                             cell.setCellValue(outEleNode.get(r - (15 + indataNum + 7)).attributeValue("type"));
113                         }
114                         if (c == 6) {
115                             HSSFCell cell = row.createCell(c);
116                             cell.setCellValue(
117                                     outallNode.get(outEleNode.get(r - (15 + indataNum + 7)).attributeValue("name")));
118                         }
119                     }
120                 }
121             }
122         }
123     }

posted @ 2019-01-14 15:29  c_x_t  阅读(226)  评论(0)    收藏  举报