次の宣伝文句を使用して、XElement を使用して Excel XML ファイルを読み込んで保存しています。
Dim root As XElement = XElement.Load(inFile)
'code to change elements goes here
root.Save(outFile)
問題は、保存ルーチンが名前空間タグを追加しているだけでなく、誰が何を知っているかを知っているため、Excel と Windows がこれを Excel XML ファイルとして認識しなくなることです。私の例では、要素を操作していません。ファイルを読み込んで保存しているだけです。私は基本的にlinqを使用してXML内の特定の要素を見つけ、それらを変更してからファイル全体を保存したいと考えています。私はこれを難しくしすぎていますか?
inFile 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">
<Worksheet ss:Name="Datagrid">
<Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row ss:Index="3" ss:AutoFitHeight="0">
<Cell ecProperty="email_address">
<Data ss:Type="String">email address</Data>
</Cell>
</Row>
<Row ss:Index="4" ss:AutoFitHeight="0">
<Cell ecProperty="synthesis_mode">
<Data ss:Type="String">Ideal Mode</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
outFile 結果
<?xml version="1.0" encoding="utf-8"?>
<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">
<ss:Worksheet ss:Name="Datagrid">
<ss:Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
<ss:Row ss:Index="3" ss:AutoFitHeight="0">
<ss:Cell ecProperty="email_address">
<ss:Data ss:Type="String">email address</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row ss:Index="4" ss:AutoFitHeight="0">
<ss:Cell ecProperty="synthesis_mode">
<ss:Data ss:Type="String">Ideal Mode</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</ss:Worksheet>
</ss:Workbook>