xls(excel)到图表
我有一个 excel 文件 (xls),我想在网页上的图表中显示该数据。
我可以在 Excel 中将文件另存为 xml 文档,但我现在不知道如何解析它。
$(document).ready(function() {
$.ajax({
url: "data.xml",
type: "GET",
dataType: "xml",
success: function(xml){
parseXml(xml);
}
});
});
function parseXml(xml)
{
var xml = $(xml);
var data = xml.find("Worksheet").children('table').children('row').children('Cell');
console.log(data);
}
这“有点”有效,但获得正确的值很困难。
例如,如果我想获取 xml 文档中如下所示的净销售额:
<Row>
<Cell ss:StyleID="s44">
<Data ss:Type="String">Net sales</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11779.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10996.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10222.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11636.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10918.0</Data>
</Cell>
<Cell ss:StyleID="s42"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10231.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">9649.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10871.0</Data>
</Cell>
</Row>
I have a excel file (xls) and I want to show that data in charts on a web page.
I can save the file as a xml document in Excel but i don't now how to parse it.
$(document).ready(function() {
$.ajax({
url: "data.xml",
type: "GET",
dataType: "xml",
success: function(xml){
parseXml(xml);
}
});
});
function parseXml(xml)
{
var xml = $(xml);
var data = xml.find("Worksheet").children('table').children('row').children('Cell');
console.log(data);
}
This works "kind of" but to get the right values is hard.
For example, if I want to get the Net Sales which looks like this in the xml document:
<Row>
<Cell ss:StyleID="s44">
<Data ss:Type="String">Net sales</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11779.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10996.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10222.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11636.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10918.0</Data>
</Cell>
<Cell ss:StyleID="s42"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10231.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">9649.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10871.0</Data>
</Cell>
</Row>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 jQuery 1.5 开始,内置了对将 XML 字符串解析为 jQuery 对象的支持。只需使用
jQuery.parseXML
即可查询生成的 jQuery对象就像 DOM 一样。由于同源策略(请参阅MDC 上的 HTTP 访问控制),我无法进行交叉-browser GET,所以我必须从 XML 文件中复制源代码并将其放入文本区域中。
可能是因为我收到解析错误,所以您在那里进行的任何转义都丢失了。属性
ss:Format
包含无效值:我删除了该值,它的效果非常好!请参阅我的 jsFiddle 测试用例。
Since jQuery 1.5, there's built-in support for parsing an XML string to a jQuery object. Just use
jQuery.parseXML
and then you can query the resulting jQuery object just as if it were a DOM.Due to the same origin policy (see HTTP access control on MDC) I was prevented from doing a cross-browser GET, so I had to copy the the source code from your XML file and put it in a textarea.
Could be be that any escaping you had in there was lost because I am getting a parse error. The attribute
ss:Format
contains an invalid value:I removed this value, and it worked like a charm! See my test case on jsFiddle.