首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教,DataTable导出到EXCEL 2010

2012-10-21 
请问,DataTable导出到EXCEL 2010DataTable导出到EXCEL 2003还能正常如何导入到EXCEL 2010[解决办法]用 xml

请问,DataTable导出到EXCEL 2010
DataTable导出到EXCEL 2003还能正常


如何导入到EXCEL 2010

[解决办法]
用 xml 格式,xml是通用格式,哪个版本都可以用。



XML code
<?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"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">  <Author>User</Author>  <LastAuthor>User</LastAuthor>  <Created>2012-10-10T11:18:43Z</Created>  <Company>微软中国</Company>  <Version>11.5606</Version> </DocumentProperties> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">  <WindowHeight>9225</WindowHeight>  <WindowWidth>18180</WindowWidth>  <WindowTopX>240</WindowTopX>  <WindowTopY>120</WindowTopY>  <ProtectStructure>False</ProtectStructure>  <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles>  <Style ss:ID="Default" ss:Name="Normal">   <Alignment ss:Vertical="Center"/>   <Borders/>   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>   <Interior/>   <NumberFormat/>   <Protection/>  </Style> </Styles> <Worksheet ss:Name="Sheet1">  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">   <Row>    <Cell><Data ss:Type="String">fdfd</Data></Cell>   </Row>  </Table>  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">   <Selected/>   <ProtectObjects>False</ProtectObjects>   <ProtectScenarios>False</ProtectScenarios>  </WorksheetOptions> </Worksheet> <Worksheet ss:Name="Sheet2">  <Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">   <ProtectObjects>False</ProtectObjects>   <ProtectScenarios>False</ProtectScenarios>  </WorksheetOptions> </Worksheet> <Worksheet ss:Name="Sheet3">  <Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">   <ProtectObjects>False</ProtectObjects>   <ProtectScenarios>False</ProtectScenarios>  </WorksheetOptions> </Worksheet></Workbook>
[解决办法]
连接的字符串有错。。2003和2010的连接字符串是不一样的。。
C# code
 if(aLastName =="xls" )                    strCon = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties = \'Excel 8.0\'";                 if(aLastName =="xlsx" )                    strCon = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", FilePath);
------解决方案--------------------


连接字符串命令不一样.
2003的

C# code
 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
[解决办法]
csdn中有篇博客,专门介绍C#和Excel之间相互操作的,有兴趣可以看看,可能会对你有帮助。
http://blog.csdn.net/jokeesloat/article/details/7164017

热点排行