我正在使用LINQ to XML來創建Excel XML文件。我想在單元格中包含換行符。 Excel使用
文字表示新行。如果我嘗試添加該使用XTEXT:單元格中的Excel XML換行符
XText newlineElement = new XText("Foo Bar");
我得到:
Foo
Bar
在Excel其中顯示爲:
Foo Bar
有沒有一種辦法將

寫入XML文件而不做
String.Replace("
", " ")
生成的文件文本?
編輯這裏是一段代碼片段,展示瞭如何爲Excel文檔創建一行。由於它有點混亂,我避免了它。 :)讓我知道如果更多的代碼會有所幫助,或者如果這隻會增加更多的困惑。
在此示例中,上述newlineElement
由代碼示例中唯一的XText表示。
const string CELL = "{urn:schemas-microsoft-com:office:spreadsheet}Cell";
const string DATA = "{urn:schemas-microsoft-com:office:spreadsheet}Data";
const string STYLE = "{urn:schemas-microsoft-com:office:spreadsheet}StyleID";
const string TYPE= "{urn:schemas-microsoft-com:office:spreadsheet}Type";
const string HEIGHT = "{urn:schemas-microsoft-com:office:spreadsheet}Height";
const string ROW = "{urn:schemas-microsoft-com:office:spreadsheet}Row";
...
XElement rowElement = new XElement(Row,
new XElement(CELL,
new XAttribute(STYLE, "s0")),
new XElement(CELL,
new XElement(DATA,
new XText(description.Replace("\n", " ")),
new XAttribute(TYPE, "String")),
new XAttribute(STYLE, "sWrap")),
new XElement(CELL,
new XAttribute(STYLE, "s0")),
new XAttribute(HEIGHT, height));
這將產生:
<ss:Row ss:Height="45">
<ss:Cell ss:StyleID="s0" />
<ss:Cell ss:StyleID="sWrap">
<ss:Data ss:Type="String">Foo&#10;Bar</ss:Data>
</ss:Cell>
<ss:Cell ss:StyleID="s0" />
</ss:Row>
感謝您的幫助迄今!
相信與否,這似乎不可能。 – SLaks 2010-05-26 20:20:20
您可以發佈一些示例代碼,瞭解它如何進入Excel文件? – CoderDennis 2010-05-26 23:34:50
基本上,看到一些代碼在創建後顯示如何使用newlineElement對象會很好。 – CoderDennis 2010-05-26 23:44:49