从 WinForm 将数据导入 Excel
好的,所以我有一个表格,其中填写了客户地址、购买信息等。现在,我想将这些数据导入 Excel,其中有一列用于显示购买的每种产品的数量、人员姓名、他们的姓名电子邮件等。每个客户都有自己的行。我的程序是用C#编写的,它是一个winform。现在,我是一个初学者,之前我只将数据放入列表框中。将 ListBox 转换为 Excel 是否容易,或者是否有更好的方法来解决此问题?
Ok, So I have a form, that is filled in with a customers address, purchase info etc. Now, I want to get that data into Excel, having a column for the quantities of each product purchased, the name of the person, their email, etc. Each customer would have their own row. My program is coded in C#, it is a winform. Now, I am a bit of a beginner and I have only put data into a ListBox before. Would it be easy to do ListBox to Excel or is there a better way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议引用 Microsoft.Office.Interop.Excel 。有关如何执行此操作的更多信息,请参阅本指南。
http://msdn.microsoft.com/en-us/library/dd264733.aspx
I'd recommend referencing Microsoft.Office.Interop.Excel . For more information on how to do this follow this guide.
http://msdn.microsoft.com/en-us/library/dd264733.aspx
我在 Codeplex 的应用程序上使用它。
Excel 数据阅读器 - 在 .NET 中读取 Excel 文件
I am using this on my Apps from Codeplex.
Excel Data Reader - Read Excel files in .NET
Regards
您还可以使用 Excel 作为数据库通过 ADO 来完成此操作。这是一篇较旧的文章,涉及 Visual Basic 或 VBA,但您应该能够轻松地将概念转换为 C#。它不需要 Excel 在您的计算机上。
http://support.microsoft.com/kb/257819
如果您尚未使用ADO,这应该为您指明正确的方向,尽管这里处理的是 Access,因此您需要使用上面文章中的连接字符串:
http://www.java2s.com/Code/ASP/ADO.net -数据库/使用SQLC将数据插入数据库.htm
You can also do this through ADO using Excel as a database. This is an older article and deals with Visual Basic or VBA, but you should be able to convert the concept to C# easily enough. It doesn't require Excel to be on your machine.
http://support.microsoft.com/kb/257819
If you haven't inserted data using ADO, this should point you in the right direction, though it is dealing with Access here, so you want to use the connection string from the article above:
http://www.java2s.com/Code/ASP/ADO.net-Database/InsertdatatodatabaseusingSQLC.htm
可能是最简单的解决方案:您可以创建一个带有列标题的逗号分隔 (csv) 文件,并为其指定扩展名 .xls。 Excel 将打开它们并至少了解列结构。
“xls”文件将是一个 csv 文件,但对于只读结果,在许多情况下它是完全足够的。
使用 Interop 是一项更大的任务,例如,仅仅删除您创建的 Excel 实例就是一项高雅艺术(或黑暗魔法)。
Possible the easiest solution: You could create a comma-separated (csv) file with column headers, and give them the extension .xls. Excel will open them and at least understand the column structure.
The "xls" file will be a csv file, but for readonly results, it is totally sufficient in astoundingly many cases.
Using Interop is a bigger task, for example just getting rid of the Excel instance you created is a piece of high arts (or dark magic).
对于创建/修改/读取/写入 Office 文件(包括 Excel、Word 等),您可以使用 MS 提供的免费 SDK,名为 OpenXML SDK 2.0 - 您可以下载它此处。
如果功能不够,还有其他免费和商业 SDK - 推荐一些可以帮助您更多地了解您的需求。
For creating/modiyfing/reading/writing Office files (including Excel, Word etc.) there is a free SDK from MS you can use called OpenXML SDK 2.0 - you can download it here.
IF the features are not enough there are other free and commercial SDKs out there - to recommend something it would help to know more about your requirements.