c# - 如何在不打开另一个工作簿/excel 应用程序的情况下将外部数据(.csv 文件)导入为新工作表
我的目标是打开许多 .csv 文件,每个文件都位于同一 Excel 工作簿的新工作表中,而不需要为每个文件打开一个工作簿。
我注意到 Open() 或 OpenText() 方法属于 workBook 类,使用它们意味着打开一个新的工作簿。
更清楚地说,普通用户只需在 Excel 2003 中选择以下选项即可执行此操作: 数据>导入外部数据>导入数据...>> (然后选择 .csv 文件并按照向导操作)。 此方案将授予用户在新工作表中打开 .csv 文件的能力。 此外,还可以选择分隔符、文本限定符和数据格式,其方式与使用 workBook.OpenText() 方法类似。
我设法通过在单独的工作簿中打开每个 .csv 文件,然后将其复制到目标 Excel 工作簿中的新工作表来完成此操作。这似乎是一种笨拙的方法,我认为它需要更多时间(我在这里处理许多 .csv 文件)。
我试着到处寻找。难道这件事不能通过“Microsoft Excel 11.0 对象库”实现吗?
谢谢
My target is to open many .csv files, each of them in a new workSheet in the same excel workBook, without opening a workBook for each.
I noticed that the Open() or OpenText() methods belong to the workBook class and using them means opening a new workBook.
To be more clear, a regular user would simply do this in Excel 2003 by selecting:
Data > Import External Data > Import Data... > (then selecting a .csv file and go along with the wizard).
This scenario will grant the user an ability to open the .csv file to a new workSheet.
Also, it is possible to choose the Delimiters, Text Qualifier and Data Format, in a similar manner as using the workBook.OpenText() method.
I managed to do it by opening each .csv file in a seperate workBook and then copying it to a new workSheet in the target excel workBook. It seems kind of a clumsy way to do and i think it requires more time (im dealing with many .csv files here).
I tried searching everywhere. could it be that this thing is not possible via "Microsoft Excel 11.0 Object Library" ??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须使用工作簿来一一打开它们,但你可以改进它:
只需使用一个工作簿对象打开每个 CSV 文件,并将打开的工作表复制到另一个工作簿对象(作为您的基簿),以将它们存储在单独的工作表中。
You have to use workbook to open them one by one, but you can improve it:
Just use one workbook object to open each CSV file and copy the opened worksheet over to another workbook object (as your basebook) to store them in separate workssheets.