简体   繁体   English

使用VB将多个Excel文件合并为一个

[英]Combine several Excel files into one using VB

I have a folder with several excel files that have a date field, ie 08-24-2010-123320564.xls. 我有一个文件夹,其中包含几个具有日期字段的Excel文件,即08-24-2010-123320564.xls。 I want to be able to have some VB scripting that will simply take the files that start with todays date and merge them into one file. 我希望能够拥有一些VB脚本,这些脚本将简单地获取以今天的日期开头的文件并将它们合并为一个文件。

08-24-2010-123320564.xls 08-24-2010-123440735.xls 08-24-2010-131450342.xls 08-24-2010-123320564.xls 08-24-2010-123440735.xls 08-24-2010-131450342.xls

into 进入

08-24-2010.xls 08-24-2010.xls

Can someone please help? 有人可以帮忙吗?

Thanks 谢谢

GabrielVA 加布里埃尔

Aspose使在.NET http://www.aspose.com中使用excel文件变得非常容易

assuming you just want to append rows in simple spreadsheets, follow this logic: 假设您只想在简单的电子表格中添加行,请遵循以下逻辑:

  • Psuedocode 伪代码
    • use an excel macro 使用Excel宏
      • (you could just as well automate excel from vb but why vbscript alone since you need excel anyway?) (您也可以从vb中自动执行excel,但是为什么还是要单独使用vbscript,因为无论如何您都需要excel?)
    • have it to a dir listing (dir function) 将其保存到目录清单(目录功能)
    • dim a date_start variable init to "new" 将date_start变量init变暗为“新”
    • dim a merged_spreadsheet as new doc default to nothing 将merged_spreadsheet变暗为新文档,默认为无
    • loop thru result of dir 通过目录结果循环
      • if date_start <> start of filename 如果date_start <>文件名的开始
        • if merged_spreadsheet is not nothing 如果merged_spreadsheet不为空
          • save it 保存
          • set it to nothing 设置为空
        • store start of date (left mid function) in date_start 在date_start中存储日期的开始(左中间函数)
      • if merged_spreadsheet is nothing 如果merged_spreadsheet什么都不是
        • make a new one 换一个新的
      • open the file from the dir command's loop 从dir命令的循环中打开文件
      • select all the data 选择所有数据
      • copy it 复制它
      • go to first empty row in merged_spreadsheet 转到merged_spreadsheet中的第一个空行
      • paste it 贴上
    • loop files 循环文件
    • if merged_spreadsheet is not nothing 如果merged_spreadsheet不为空
      • save it 保存

If you're not happy with all those 'nothings', you can set a separate flag to keep track of whether you have a merged_spreadsheet or not. 如果您对所有这些“不满意”都不满意,则可以设置一个单独的标志来跟踪您是否有merged_spreadsheet。 Think about what happens for just one file in a date, no files at all, etc. Of course you will tear out your hair finding out how to automate those excel functions. 想一想日期中仅一个文件会发生什么,根本没有文件等。当然,您会发现问题,找出如何自动执行这些excel函数。 The secret to turning 'hard' into 'pretty darn easy' is this: 将“艰苦”变成“相当简单”的秘密是:

macro recorder will reveal the automation commands 宏记录器将显示自动化命令

They are not intuitive. 它们不直观。 So record a macro. 因此,记录一个宏。 Then do things that you'll need to do in your code. 然后执行您在代码中需要做的事情。 Stop recording and look at the result. 停止记录并查看结果。

For example: 例如:
* load/save files * select only entered fields * Select all * copy / switch files / paste * create new sheet * click in various single cells and type (how to examine/set a cell's contents) *加载/保存文件*仅选择输入的字段*选择所有*复制/切换文件/粘贴*创建新工作表*单击各种单个单元格并键入(如何检查/设置单元格的内容)

In summary- 综上所述-
(1) Know exactly the steps to what you're doing (1) 确切了解您正在做的步骤
(2) Use macro recorder to give away the secrets of the excel object model. (2)使用宏记录器泄露excel对象模型的秘密。 Steal its secrets. 窃取其秘密。

Really this won't be all that hard if you marry these two concepts cleverly. 确实,如果您巧妙地将这两个概念结合在一起,这将不会很困难。 Since the macro will be vbscript (at least if you use office 97 ;-) you can probably run it from vbs or vb6 if you want. 由于该宏将是vbscript(至少如果使用office 97,则为-),因此您可以根据需要从vbs或vb6运行它。 A hop to vb.net shouldn't be that hard either. 跳到v​​b.net也不应该那么难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM