您的位置:首页 > 其它

poi 读取excel(xlsx) 并保存到xml

2016-04-30 00:00 246 查看
摘要: poi版本3.14 jdom2

excel 格式



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();
}
}




显示不全,只能截这么多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息