poi 读取excel(xlsx) 并保存到xml
2016-04-30 00:00
246 查看
摘要: poi版本3.14 jdom2
excel 格式
![](http://static.oschina.net/uploads/space/2016/0430/145959_g5aJ_1388758.png)
![](http://static.oschina.net/uploads/space/2016/0430/150519_FcLv_1388758.png)
显示不全,只能截这么多
excel 格式
![](http://static.oschina.net/uploads/space/2016/0430/145959_g5aJ_1388758.png)
public static void xlsxToXml(String path) throws FileNotFoundException, IOException{ XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); //创建根节点 Element sheet = new Element("sheet"); Document doc = new Document(sheet); Comment ttComment = new Comment("常数配置表");//创建注释 sheet.addContent(ttComment); Attribute ab1 = new Attribute("id","constantConfig");//根节点属性 sheet.setAttribute(ab1); // 循环工作表Sheet for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // 循环行Row for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { // System.out.println("正运行到"+rowNum+"行了"); Element subTree1 = new Element("subTree");// 子节点 // 获得行 XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow == null) { continue; } // int cells = xssfRow.getPhysicalNumberOfCells(); Element object1 = new Element("id");//子节点信息 Element object2 = new Element("param"); Element object3 = new Element("describe"); object1.setText(String.valueOf(xssfRow.getCell(0).getNumericCellValue())); object2.setText(String.valueOf(xssfRow.getCell(1).getNumericCellValue())); object3.setText(xssfRow.getCell(2).getStringCellValue()); sheet.addContent(subTree1);//与上级节点(顶级节点)关系 subTree1.addContent(object1); subTree1.addContent(object2); subTree1.addContent(object3); } } //将上述内容写入XML XMLOutputter out = new XMLOutputter(); out.setFormat(out.getFormat().setEncoding("UTF-8")); try { out.output(doc, new FileOutputStream(new File("F:/jdom_constant_config.xml"))); } catch (Exception e) { e.printStackTrace(); } }
![](http://static.oschina.net/uploads/space/2016/0430/150519_FcLv_1388758.png)
显示不全,只能截这么多
相关文章推荐
- Android SDK 百度地图通过poi城市内检索简介接口的使用
- Android百度地图poi范围搜索
- Android 百度地图POI搜索功能实例代码
- Java 使用poi把数据库中数据导入Excel的解决方法
- java使用POI读取properties文件并写到Excel的方法
- java使用poi读取ppt文件和poi读取excel、word示例
- Java中使用Apache POI读取word文件简单示例
- POI WORD
- 利用POI实现Word和 Html互转(可以实现在线预览)
- POI封装二:导出 Export
- java生成word文档【一】
- java生成word文档【二】
- 使用POI修改Excel后进行保存
- Java 实现导出excel表
- java POI实现excle 强制换行
- poi解析office文档
- poi 一个单元格不同字体