.net 将 Excel 文件合二为一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/830841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge Excel Files Into One
提问by Juliet
I have a few hundred Excel files, where each file has some data on the first sheet. I've been asked to write a console application in C# which merges all of the Excel files together into a single document while retaining the formatting.
我有几百个 Excel 文件,每个文件在第一张纸上都有一些数据。我被要求用 C# 编写一个控制台应用程序,它将所有 Excel 文件合并到一个文档中,同时保留格式。
The consolidated file is a single workbook containing a sheet for each file merged into the document. Unfortunately, the Excel files are in binary rather than XML format, so I can't perform an XSL transformation.
合并文件是单个工作簿,其中包含合并到文档中的每个文件的工作表。不幸的是,Excel 文件是二进制格式而不是 XML 格式,因此我无法执行 XSL 转换。
Is there are free library or sample code which demonstrates how to combine a sheets from multiple Excel documents into a single file?
是否有免费的库或示例代码来演示如何将多个 Excel 文档中的工作表合并到一个文件中?
采纳答案by Joshua Belden
You'll need to use the Microsoft Excel Interop library. http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx
您需要使用 Microsoft Excel 互操作库。http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx
You should be able to add a reference, in the com tab, to Microsoft Excel 11 Object Library.
您应该能够在 com 选项卡中添加对 Microsoft Excel 11 对象库的引用。
http://msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx
回答by Vem234
This is an easy task for a neat little program called Bulk File Merger (they have a Mac version called Excel File Merger). I managed to merge over 300 files in a matter of minutes. You can also merge csv files, which takes even less time. We banged out over 1300 csv files in seconds. Scary.
对于名为 Bulk File Merger 的简洁小程序(他们有一个名为 Excel File Merger 的 Mac 版本)来说,这是一项简单的任务。我设法在几分钟内合并了 300 多个文件。您还可以合并 csv 文件,这将花费更少的时间。我们在几秒钟内处理了 1300 多个 csv 文件。害怕。
回答by Fredrik M?rk
It's not free (a developer license is $ 425) but GemBoxhas a very good library that is way faster than using the office objects. We use it in my current project since a year and a half, and it has consistently performed very well for us.
它不是免费的(开发者许可证是 425 美元)但是GemBox有一个非常好的库,它比使用 office 对象要快得多。一年半以来,我们在我当前的项目中使用它,它对我们来说一直表现得非常好。
回答by Drew McGhie
Look into Visual Studio Tools for Office (VSTO).
You can open workbooks in code and use a lot of the excel functionality, as well as copying worksheets like you've said you need to do.
查看 Visual Studio Tools for Office (VSTO)。
您可以在代码中打开工作簿并使用许多 excel 功能,以及像您所说的那样复制工作表。
For resources, this MSDN forum postdemonstrates opening excel files and copying worksheets. Hopefully this will get you on the right direction.
对于资源,此 MSDN 论坛帖子演示了打开 excel 文件和复制工作表。希望这会让你走上正确的方向。
回答by Roger
Of course you can do Tool->Compare->Merge Workbooks. And within Excel as Joshua mentioned there is the merge method in the COM Interop libraries. If you need some help let me know I work heavily in Excel and may be able to help you out on a small fee basis.
当然你可以做 Tool->Compare->Merge Workbooks。正如 Joshua 提到的,在 Excel 中,COM Interop 库中有合并方法。如果您需要一些帮助,请告诉我我在 Excel 中的工作量很大,也许可以在收取少量费用的情况下为您提供帮助。

