将datatable转换为excel 2007(.xlsx) [英] Convert datatable to excel 2007(.xlsx)

查看:307
本文介绍了将datatable转换为excel 2007(.xlsx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataTable 我需要放入Excel 2007格式并将其另存为Excel文件(.xlsx)2007。



任何人都可以帮助我实现这一点吗?

解决方案

可以使用OLEDB数据提供者,作为另一个ADO.NET数据源,以循环遍历您的DataTable行并将其插入到Excel电子表格中。这是一个Microsoft知识库文章,引导您完成许多细节。



http://support.microsoft.com/kb/316934/en-us



要保留的大事请注意,您可以在工作簿中创建工作簿和工作表,您可以通过在名称末尾附加一个$来引用现有工作表。如果您在表单名称末尾省略'$',OLEDB提供商将会认为它是一个新的工作表,并将尝试创建它。



工作表名称之后的美元符号表示
表存在。如果您正在创建
a新表,如本文中
创建新工作簿和表
部分所述,请勿使用
美元符号。


您可以在2003(.xls)或2007格式(xlsx)中创建和电子表格,并在连接字符串上定义 - 您指定的文件要写入,只需指定扩展名。确保使用正确的OLEDB提供商版本。



如果要创建2003(.xls)版本,请使用此连接字符串:

  Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Book1.xls;扩展属性=Excel 8.0; HDR = YES 

如果要创建2007(.xlsx)版本,请使用此连接字符串:

  Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Book1.xlsx;扩展属性=Excel 12.0; HDR = YES 

您可能必须从Microsoft下载ACE提供商才能创建XLSX文件。你可以找到它这里



我通常使用XLS提供商,所以我没有和XLSX提供商一样多。



希望这有帮助。如果您有其他问题,请告知我。


I have an DataTable I need to put into Excel 2007 format and save it as an excel file(.xlsx) 2007.

Can anyone help me to achieve this?

解决方案

You can use an OLEDB data provider and just treat Excel as another ADO.NET data source in order to loop through your DataTable rows and insert them into the Excel spreadsheet. Here's a Microsoft KB article that walks you through a lot of the details.

http://support.microsoft.com/kb/316934/en-us

The big thing to keep in mind is that you can create workbooks and sheets within the workbook, and you can reference existing sheets by appending a '$' at the end of the name. If you omit the '$' at the end of the sheet name, the OLEDB provider will assume that it's a new sheet and will try to create it.

The dollar sign following the worksheet name is an indication that the table exists. If you are creating a new table, as discussed in the Create New Workbooks and Tables section of this article, do not use the dollar sign.

You can create and spreadsheet in 2003 (.xls) or 2007 format (xlsx), and that's defined on your connection string -- you specify the file that you're going to write to, and just specify the extension. Make sure you use the right OLEDB provider version.

If you want to create a 2003 (.xls) version, you use this connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES

If you want to create a 2007 (.xlsx) version, you use this connection string:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Book1.xlsx;Extended Properties="Excel 12.0;HDR=YES

You may have to download the ACE provider from Microsoft in order to create XLSX files. You can find it here.

I usually use the XLS provider, so I haven't worked with the XLSX provider as much.

Hope this helps. Let me know if you have other questions.

这篇关于将datatable转换为excel 2007(.xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆