无法从DataTable对象导出XML,然后使用File->将其导入Excel中。主要问题是如果我写它,Excel拒绝识别模式。如果我不编写架构,Excel就不会“发明”正确的架构。首先,一些代码:
DataTable dt;
DataRow dr;
dt = new DataTable("Employee", "xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance");
dt.Columns.Add("First Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
dt.Columns.Add("Job Title", typeof(string));
dt.Columns.Add("Annual Salary", typeof(int));
dt.Columns.Add("Hourly Wage", typeof(int));
dt.Columns.Add("Termination Date", typeof(DateTime));
dt.Columns["Termination Date"].DateTimeMode = DataSetDateTime.Unspecified;
dt.Columns.Add("Hire Date", typeof(DateTime));
dt.Columns["Hire Date"].DateTimeMode = DataSetDateTime.Unspecified;
dr = dt.NewRow();
dr[0] = "Joe";
dr[1] = "Blow";
dr[2] = "Grunt";
dr[4] = 15;
dr[6] = new DateTime(1998, 3, 17);
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Jane";
dr[1] = "Doe";
dr[2] = "Manager";
dr[3] = 75000;
dr[5] = new DateTime(2015, 6, 26);
dr[6] = new DateTime(2014, 2, 12);
dt.Rows.Add(dr);
dt.WriteXml(@"C:\Users\Public\DataTable1.xml", XmlWriteMode.IgnoreSchema);
dt.WriteXml(@"C:\Users\Public\DataTable2.xml", XmlWriteMode.WriteSchema);
dt.WriteXmlSchema(@"C:\Users\Public\DataTable.xsd");
注意我添加到表中的列的顺序,并注意我的数据是专门构造的,在某些单元格中以非常特定的顺序具有空值。现在有些输出:
DataTable1.xml
<?xml version="1.0" standalone="yes"?>
<DocumentElement xmlns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">
<Employee>
<First_x0020_Name>Joe</First_x0020_Name>
<Last_x0020_Name>Blow</Last_x0020_Name>
<Job_x0020_Title>Grunt</Job_x0020_Title>
<Hourly_x0020_Wage>15</Hourly_x0020_Wage>
<Hire_x0020_Date>1998-03-17T00:00:00</Hire_x0020_Date>
</Employee>
<Employee>
<First_x0020_Name>Jane</First_x0020_Name>
<Last_x0020_Name>Doe</Last_x0020_Name>
<Job_x0020_Title>Manager</Job_x0020_Title>
<Annual_x0020_Salary>75000</Annual_x0020_Salary>
<Termination_x0020_Date>2015-06-26T00:00:00</Termination_x0020_Date>
<Hire_x0020_Date>2014-02-12T00:00:00</Hire_x0020_Date>
</Employee>
</DocumentElement>
DataTable2.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet xmlns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">
<xs:schema id="NewDataSet" targetNamespace="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns:mstns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="xmlns_x003A_xsi_x003D_http_x003A__x002F__x002F_www.w3.org_x002F_2001_x002F_XMLSchema-instance_x003A_Employee" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="First_x0020_Name" type="xs:string" minOccurs="0" />
<xs:element name="Last_x0020_Name" type="xs:string" minOccurs="0" />
<xs:element name="Job_x0020_Title" type="xs:string" minOccurs="0" />
<xs:element name="Annual_x0020_Salary" type="xs:int" minOccurs="0" />
<xs:element name="Hourly_x0020_Wage" type="xs:int" minOccurs="0" />
<xs:element name="Termination_x0020_Date" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
<xs:element name="Hire_x0020_Date" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Employee>
<First_x0020_Name>Joe</First_x0020_Name>
<Last_x0020_Name>Blow</Last_x0020_Name>
<Job_x0020_Title>Grunt</Job_x0020_Title>
<Hourly_x0020_Wage>15</Hourly_x0020_Wage>
<Hire_x0020_Date>1998-03-17T00:00:00</Hire_x0020_Date>
</Employee>
<Employee>
<First_x0020_Name>Jane</First_x0020_Name>
<Last_x0020_Name>Doe</Last_x0020_Name>
<Job_x0020_Title>Manager</Job_x0020_Title>
<Annual_x0020_Salary>75000</Annual_x0020_Salary>
<Termination_x0020_Date>2015-06-26T00:00:00</Termination_x0020_Date>
<Hire_x0020_Date>2014-02-12T00:00:00</Hire_x0020_Date>
</Employee>
</NewDataSet>
DataTable.xsd
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" targetNamespace="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns:mstns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns="xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="xmlns_x003A_xsi_x003D_http_x003A__x002F__x002F_www.w3.org_x002F_2001_x002F_XMLSchema-instance_x003A_Employee" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="First_x0020_Name" type="xs:string" minOccurs="0" />
<xs:element name="Last_x0020_Name" type="xs:string" minOccurs="0" />
<xs:element name="Job_x0020_Title" type="xs:string" minOccurs="0" />
<xs:element name="Annual_x0020_Salary" type="xs:int" minOccurs="0" />
<xs:element name="Hourly_x0020_Wage" type="xs:int" minOccurs="0" />
<xs:element name="Termination_x0020_Date" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
<xs:element name="Hire_x0020_Date" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
WriteXML函数显然没有输出格式正确的架构代码(或者Excel太笨了而无法识别它)。所以Excel声称DataTable2.xml没有架构,但是不要害怕我 Excel,所有强大的人都会为你创建一个。然后,继续执行此操作,导入模式,就像它是数据一样,然后将实际数据导入到任何列所需的列的右侧,以将模式伪造为数据。这种问题让我想到了第二个问题。 WriteXML函数'出色地'省略了任何空值成员,因此Excel(全部强大,记住)'发明'一个模式它只是开始读取元素并创建它需要的任何列。如果它们已经存在,它将重用相同名称的列,否则在右侧添加列。由于我的第一个元素包含索引3和5处的列的空值,因此在第二个记录添加它们之前,这些值将被忽略。这有效地交换了列顺序。如果我在DataTable构造函数调用中有一个名称空间值,那么更不用说附加到列名的哑'ns1:'前缀。顺便说一句,“DataSetDateTime.Unspecified”位用于防止XML日期时间以“2007-10-22T09:55:45-05:00”格式出现,Excel无法从文本解析回日期时间。 ..它被迫做...没有架构......它无法读取。我尝试为列添加默认值(new DateTime()),但这不起作用,因为我正在使用另一个表中的ImportRow而我不想要高飞'0001-1-1:00:00 :00'默认日期时间默认因为,猜猜看,Excel也无法将其解释为日期时间。虽然,我认为它能够将'1900-1-1:00:00:00'解释为日期时间,这是T-SQL的默认值。任何帮助将不胜感激,因为我想摆脱我目前的解决方法,即填充所有列中的默认值的虚拟第一行,以强制Excel构建正确的架构,然后告诉用户删除它。 / p>