使用不同的模式将数据库数据到xml

时间:2016-11-04 07:32:18

标签: c# asp.net xml datatable ado.net

我需要将我的数据表数据转换为此模式(在MS Excel中打开)。

Datatable Schema

ReferId    Name    Salary
2457165     ABC    10000  

输出架构示例

<Worksheet ss:Name="Sheet1">
  <ss:Table>
   <ss:Row>
    <ss:Cell  ss:StyleID="s27"><Data ss:Type="String">Refer_ID</Data></ss:Cell>
    <ss:Cell  ss:StyleID="s27"><Data ss:Type="String">Name</Data></ss:Cell>
    <ss:Cell  ss:StyleID="s27"><Data ss:Type="String">Salary</Data></ss:Cell>
   </ss:Row>
   <ss:Row>
    <ss:Cell><Data ss:Type="String">2457165</Data></ss:Cell>
    <ss:Cell><Data ss:Type="String">ABC</Data></ss:Cell>
    <ss:Cell><Data ss:Type="String">10000</Data></ss:Cell>
   </ss:Row>
</ss:Table>

我曾尝试过什么

我尝试用DataTable.WriteXml()

编写简单的xml
System.IO.StringWriter writer = new System.IO.StringWriter();
dt.WriteXml(writer, XmlWriteMode.WriteSchema, true);

但架构不同。我尝试了一些循环遍历数据表的行和列。但坚持下去,任何帮助都会受到关注

更新1

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

<Styles>
 <Style ss:ID="Default" ss:Name="Normal">
  <Alignment ss:Vertical="Bottom"/>
  <Borders/>
  <Font/>
  <Interior/>
  <NumberFormat/>
  <Protection/>
 </Style>
 <Style ss:ID="s27">
  <Font x:Family="Swiss" ss:Color="#0000FF" ss:Bold="1"/>
 </Style>
 <Style ss:ID="s21">
  <NumberFormat ss:Format="yyyy\-mm\-dd"/>
 </Style>
 <Style ss:ID="s22">
  <NumberFormat ss:Format="yyyy\-mm\-dd\ hh:mm:ss"/>
 </Style>
 <Style ss:ID="s23">
  <NumberFormat ss:Format="hh:mm:ss"/>
 </Style>
</Styles>

1 个答案:

答案 0 :(得分:1)

试试xml linq:

User