我正在寻求一些建议,开始编写代码来解析excel文件并使用重建的工作表创建一个新文件,如果有人可以很友好地回复建议,我将不胜感激。我不是在寻找任何人来编写我的代码,但我确实想知道如何做到这一点。似乎嵌套循环可能是技巧或者数组?
我的目录中的文件都具有相同的记录结构,但如果记录有多个变量,则源会生成一个新行,而不是将该变量水平附加到记录的第一行。我希望我的数据像普通的Excel电子表格一样水平运行。
我希望L列定义工作表。 L列中只有五个不同的值。
在源文件中,列G,K和L是确定记录的关键列。如果行和行+ 1具有相同的G,K和L值,则行+ 1,列N应转到行,列O.然后,如果行和行+ 2具有相同的G,K和L值,则行+ 2,列N应该到行,列P.等等。
如果G,K和L不匹配,则行完成,行+ n成为新行。
这是我的数据逻辑
Open c:\xml\vac\values00.xls
First_Row = 3
Last_Row = Last_Row
For First_Row to Last_Row
Read First_Row
Get Column_A
Get Column_B
Get Column_C
Get Column_D
Get Column_E
Get Column_F
Get Column_G 'critical
Get Column_H
Get Column_I
Get Column_J
Get Column_K 'critical
Get Column_L 'critical
Get Column_M
Get Column_N
'build row
New_First_Row = Column_A, Column_B, Column_C, Column_D, Column_E, Column_F, Column_G, Column_H, Column_I, Column_J, Column_K, Column_L, Column_M, Column_N
'write row to worksheet defined by value in Column L
Write New_First_Row to Column_L_worksheet
'read next row and check L, K, and G, if they match previous L, K, and G then same record, move N to New_First_Row O
Next_Row = First_Row + 1
Next_Column = Column N
For Next_Row to Last_Row
Next_Column = Next_Column + 1
Read Next_Row
Get Column_Ln
Get Column_Kn
Get Column_Gn
If (Column_L = Column_Ln) AND (Column_K = Column_Kn) AND (Column_G = Column_Gn)
Then 'grow horizontally
Get Column_N
Write Column_N to Next_Column
End IF
Next 'new row
Next First_Row
答案 0 :(得分:0)
使用VBSCRIPT,您实际上可以获得更好的成功。听起来你需要遍历目录中的一些文件,然后循环遍历该目录中的excel文件的行。
我在博客上写过如何在我的网站上执行每项操作:
VBSCRIPT - Loop through all files in a directory
VBSCRIPT - Loop through all rows in an excel sheet
这两个脚本可以组合如下:
'Declare variables to be used
Dim oFSO
Dim myFolder
Dim myDirectoryName
Dim myConnectionString
Dim myConnection
Dim myRS
'Define variables
'Access a Scripting.FileSystemObject
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Get the current directory of the script file
myDirectoryName = get_Current_Directory
'Set the current directory to a folder object
Set myFolder = oFSO.GetFolder( myDirectoryName )
'Loop through each of the files in the folder
For each myFile in myFolder.Files
'Check if its an excel file
If LCase(oFSO.GetExtensionName( myFile.Path )) = "xlsx" Then
Set myConnection = CreateObject("ADODB.Connection")
Set myRS = CreateObject("ADODB.Recordset")
'Open the excel file
myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myFile.Path & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
myConnection.Open myConnectionString
myRS.Open "SELECT * FROM [Sheet1$]", myConnection
Do While Not myRS.eof
Set myFields = myRS.Fields
For each myField in myFields
wscript.echo myField.Name & ": " & myField.Value
Next
myRS.MoveNext
Loop
'Cleanup
Set myRS = Nothing
Set myConnection = Nothing
End If
Next
Function get_Current_Directory()
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
get_Current_Directory = oFSO.GetAbsolutePathName(".")
End Function
将此脚本保存为.VBS文件,位于您拥有这些文件的同一目录中,然后进入命令提示符。将目录更改为保存此vbs文件的位置,然后在命令提示符下执行cscript myscript.vbs
,脚本将遍历目录中的所有excel文件并将内容输出到控制台。这将至少向您展示如何遍历文件中的所有文件和所有数据。其余的由您决定如何处理它们。
谢谢,
Sean W。