java 怎样解析 excel生成的xml文件
2016-07-26 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
java解析excel生成的xml文件的方法是使用dom4j实现的。
dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。
1、excel生成的xml样例文件:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2006-09-16T00:00:00Z</Created>
<LastSaved>2016-07-25T03:26:50Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7956</WindowHeight>
<WindowWidth>14808</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>168</WindowTopY>
<ActiveSheet>2</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s16" ss:Name="好">
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#006100"/>
<Interior ss:Color="#C6EFCE" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s17">
<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"
ss:WrapText="1"/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="8" ss:Color="#686868"/>
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s18" ss:Parent="s16">
<Alignment ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s19">
<NumberFormat ss:Format="yyyy/m/d\ h:mm:ss"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="14.4">
<Row>
<Cell><Data ss:Type="String">工号</Data></Cell>
<Cell><Data ss:Type="String">姓名 </Data></Cell>
<Cell ss:Index="5"><Data ss:Type="String">工号</Data></Cell>
<Cell><Data ss:Type="String">姓名</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">111</Data></Cell>
<Cell><Data ss:Type="String">张三</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">111</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">张三</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">112</Data></Cell>
<Cell><Data ss:Type="String">李四</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">112</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">李四</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>7</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
2、java解析代码:
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("person.xml"));
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();
//未知属性名称情况下
/*Iterator attrIt = element.attributeIterator();
while (attrIt.hasNext()) {
Attribute a = (Attribute) attrIt.next();
System.out.println(a.getValue());
}*/
//已知属性名称情况下
System.out.println("id: " + element.attributeValue("id"));
//未知元素名情况下
/*Iterator eleIt = element.elementIterator();
while (eleIt.hasNext()) {
Element e = (Element) eleIt.next();
System.out.println(e.getName() + ": " + e.getText());
}
System.out.println();*/
//已知元素名情况下
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}
dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。
1、excel生成的xml样例文件:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2006-09-16T00:00:00Z</Created>
<LastSaved>2016-07-25T03:26:50Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7956</WindowHeight>
<WindowWidth>14808</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>168</WindowTopY>
<ActiveSheet>2</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s16" ss:Name="好">
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#006100"/>
<Interior ss:Color="#C6EFCE" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s17">
<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"
ss:WrapText="1"/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="8" ss:Color="#686868"/>
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s18" ss:Parent="s16">
<Alignment ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s19">
<NumberFormat ss:Format="yyyy/m/d\ h:mm:ss"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="14.4">
<Row>
<Cell><Data ss:Type="String">工号</Data></Cell>
<Cell><Data ss:Type="String">姓名 </Data></Cell>
<Cell ss:Index="5"><Data ss:Type="String">工号</Data></Cell>
<Cell><Data ss:Type="String">姓名</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">111</Data></Cell>
<Cell><Data ss:Type="String">张三</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">111</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">张三</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">112</Data></Cell>
<Cell><Data ss:Type="String">李四</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">112</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">李四</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>7</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
2、java解析代码:
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("person.xml"));
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();
//未知属性名称情况下
/*Iterator attrIt = element.attributeIterator();
while (attrIt.hasNext()) {
Attribute a = (Attribute) attrIt.next();
System.out.println(a.getValue());
}*/
//已知属性名称情况下
System.out.println("id: " + element.attributeValue("id"));
//未知元素名情况下
/*Iterator eleIt = element.elementIterator();
while (eleIt.hasNext()) {
Element e = (Element) eleIt.next();
System.out.println(e.getName() + ": " + e.getText());
}
System.out.println();*/
//已知元素名情况下
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询