如何在Silverlight中将ms office电子表格xml内容转换为xlsx
本文关键字:xml 转换 xlsx 电子表格 office Silverlight 中将 ms | 更新日期: 2023-09-27 18:08:33
我有这样的电子表格XML
<?xml version="1.0"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:Styles>
<ss:Style ss:ID="1">
<ss:Font ss:Bold="1"/>
</ss:Style>
</ss:Styles>
<ss:Worksheet ss:Name="Sheet1">
<ss:Table>
<ss:Column ss:Width="80"/>
<ss:Column ss:Width="80"/>
<ss:Column ss:Width="80"/>
<ss:Row ss:StyleID="1">
<ss:Cell>
<ss:Data ss:Type="String">First Name</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Last Name</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Phone Number</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">Kevin</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Boske</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">(425) 123-4567</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</ss:Worksheet>
</ss:Workbook>
我想把这个xml转换成xlsx.
我尝试的是
private void btnExport_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog Savedlg = new SaveFileDialog();
Savedlg.Filter = "XLS (.xls)|*.xls|xlsx (.xlsx)|*.xlsx";
byte[] xlfile = this.byts;
if ((bool)Savedlg.ShowDialog())
{
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
mStream.Write(xlfile, 0, xlfile.Length);
Stream fileStream = Savedlg.OpenFile();
mStream.WriteTo(fileStream);
fileStream.Close();
}
}
byte[] byts = null;
private void import_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.ShowDialog();
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
this.byts= ReadFully(fileStream);
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
另存为XLS工作正常,当在EXCEL中打开该文件时,它会显示像
这样的消息"您要打开的文件的格式与文件扩展名指定的格式不同"。
保存为XLSX无法工作
我建议阅读xml文件,使用XmlReader
或XDocument
或XmlDocument
类。然后使用Open XML SDK,可以创建excel文件。
Open XML SDK有很高的学习曲线。因此,您可能还想看看Closed XML,它是Open XML SDK的包装,学习曲线更平缓。