【发布时间】:2021-12-07 17:16:22
【问题描述】:
我正在尝试生成这样的 XML Excel 文档
<?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">
<Styles>
<Style ss:ID="Default" /></Style>
</Styles>
<Worksheet ss:Name="Worksheet Name">
<Table>
<Row ss:AutoFitHeight="0">
<Cell>
<Data ss:Type="String">Test</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
我目前的结果是这样的:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<ss: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">
<Styles xmlns="">
<Style ss:ID="Default" />
</Styles>
<Worksheet ss:Name="Worksheet Name" xmlns="">
<Table>
<Row ss:AutoFitHeight="0">
<Cell>
<Data ss:Type="String">Test</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</ss:Workbook>
我尝试了很多不同的解决方案,但没有任何运气。我需要用 Workbook 替换 ss:Workbook 并去掉所有的 xmlns=""。
我当前的代码如下所示(LinqPad):
void Main()
{
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
var workbook = new XElement(ns + "Workbook");
workbook.Add(new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"));
workbook.Add(new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"));
workbook.Add(new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"));
workbook.Add(new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"));
workbook.Add(new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"));
var styles = new XElement("Styles");
workbook.Add(styles);
var style = new XElement("Style");
style.Add(new XAttribute(ss + "ID", "Default"));
styles.Add(style);
var worksheet = new XElement("Worksheet");
worksheet.Add(new XAttribute(ss + "Name", "Worksheet Name"));
workbook.Add(worksheet);
var table = new XElement("Table");
worksheet.Add(table);
var row = new XElement("Row");
row.Add(new XAttribute(ss + "AutoFitHeight", 0));
table.Add(row);
var cell = new XElement("Cell");
var data = new XElement("Data");
data.Add(new XAttribute(ss + "Type", "String"));
data.Value = "Test";
cell.Add(data);
row.Add(cell);
var document = new XDocument(new XDeclaration("1.0", "", null));
document.Add(new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""));
document.Add(workbook);
var sw = new CustomStringWriter();
document.Save(sw);
sw.ToString().Dump();
//sw.ToString().Replace("ss:Workbook", "Workbook").Replace("xmlns=\"\"", "").Dump();
}
// Define other methods and classes here
class CustomStringWriter : StringWriter
{
public override Encoding Encoding
{
get
{
return null;
}
}
}
有什么建议吗?
【问题讨论】:
-
目前我最好的 hack 是在添加 Workbook 元素而不添加 xmlns 命名空间时省略命名空间。将文档转换为字符串后,我手动将 xmlns 命名空间添加到 Workbook 元素。它不漂亮,但它有效。
-
这是 stackoverflow.com/questions/12075157/… 的副本,建议的解决方案是使用 Open XML。不过,这不是我的首选解决方案...