使用VisualStudio将excel转换为csv
我正在尝试使用visual studio将excel文件转换为csv,但转换时遇到问题。我循环我的代码,通过文件夹中的.xls或.xlsx文件,将它们转换为csv。然而,我没有任何结果:( Textbox1.Text是所选文件夹,Textbox2.Text是目标文件夹。 有人能帮我吗 这是我的密码:使用VisualStudio将excel转换为csv,excel,vba,visual-studio,csv,Excel,Vba,Visual Studio,Csv,我正在尝试使用visual studio将excel文件转换为csv,但转换时遇到问题。我循环我的代码,通过文件夹中的.xls或.xlsx文件,将它们转换为csv。然而,我没有任何结果:( Textbox1.Text是所选文件夹,Textbox2.Text是目标文件夹。 有人能帮我吗 这是我的密码: Dim xls As Excel.Application Dim strFile As String, strPath As String xls = New Excel.Application
Dim xls As Excel.Application
Dim strFile As String, strPath As String
xls = New Excel.Application
strPath = TextBox1.Text
strFile = Dir(strPath & "*.xls")
While strFile <> ""
xls.Workbooks.Open(strPath & strFile)
xls.ActiveWorkbook.SaveAs(Filename:=Replace(TextBox2.Text & strFile, ".xls", ".csv"), FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlTextMSDOS)
xls.Workbooks.Application.ActiveWorkbook.Close(SaveChanges:=False)
strFile = Dir()
End While
xls.Quit()
Dim xls作为Excel.Application
Dim strFile作为字符串,strPath作为字符串
xls=新的Excel.Application
strPath=TextBox1.Text
strFile=Dir(strPath&“*.xls”)
而strFile“”
xls.工作簿.打开(strPath和strFile)
xls.ActiveWorkbook.SaveAs(文件名:=Replace(TextBox2.Text&strFile,“.xls”、“.csv”),文件格式:=Microsoft.Office.Interop.Excel.XlFileFormat.xlTextMSDOS)
xls.Workbooks.Application.ActiveWorkbook.Close(SaveChanges:=False)
strFile=Dir()
结束时
xls.Quit()
将其放入文本文件中,并将其另存为Excel2Csv.vbs
。将其保存在包含所有excel文件的文件夹中。然后只需将excel文件拖动到此.vbs文件上即可
'* Usage: Drop .xl* files on me to export each sheet as CSV
'* Global Settings and Variables
Dim gSkip
Set args = Wscript.Arguments
For Each sFilename In args
iErr = ExportExcelFileToCSV(sFilename)
' 0 for normal success
' 404 for file not found
' 10 for file skipped (or user abort if script returns 10)
Next
WScript.Quit(0)
Function ExportExcelFileToCSV(sFilename)
'* Settings
Dim oExcel, oFSO, oExcelFile
Set oExcel = CreateObject("Excel.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")
iCSV_Format = 6
'* Set Up
sExtension = oFSO.GetExtensionName(sFilename)
if sExtension = "" then
ExportExcelFileToCSV = 404
Exit Function
end if
sTest = Mid(sExtension,1,2) '* first 2 letters of the extension, vb's missing a Like operator
if not (sTest = "xl") then
if (PromptForSkip(sFilename,oExcel)) then
ExportExcelFileToCSV = 10
Exit Function
end if
End If
sAbsoluteSource = oFSO.GetAbsolutePathName(sFilename)
sAbsoluteDestination = Replace(sAbsoluteSource,sExtension,"{sheet}.csv")
'* Do Work
Set oExcelFile = oExcel.Workbooks.Open(sAbsoluteSource)
For Each oSheet in oExcelFile.Sheets
sThisDestination = Replace(sAbsoluteDestination,"{sheet}",oSheet.Name)
oExcelFile.Sheets(oSheet.Name).Select
oExcelFile.SaveAs sThisDestination, iCSV_Format
Next
'* Take Down
oExcelFile.Close False
oExcel.Quit
ExportExcelFileToCSV = 0
Exit Function
End Function
Function PromptForSkip(sFilename,oExcel)
if not (VarType(gSkip) = vbEmpty) then
PromptForSkip = gSkip
Exit Function
end if
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sPrompt = vbCRLF & _
"A filename was received that doesn't appear to be an Excel Document." & vbCRLF & _
"Do you want to skip this and all other unrecognized files? (Will only prompt this once)" & vbCRLF & _
"" & vbCRLF & _
"Yes - Will skip all further files that don't have a .xl* extension" & vbCRLF & _
"No - Will pass the file to excel regardless of extension" & vbCRLF & _
"Cancel - Abort any further conversions and exit this script" & vbCRLF & _
"" & vbCRLF & _
"The unrecognized file was:" & vbCRLF & _
sFilename & vbCRLF & _
"" & vbCRLF & _
"The path returned by the system was:" & vbCRLF & _
oFSO.GetAbsolutePathName(sFilename) & vbCRLF
sTitle = "Unrecognized File Type Encountered"
sResponse = MsgBox (sPrompt,vbYesNoCancel,sTitle)
Select Case sResponse
Case vbYes
gSkip = True
Case vbNo
gSkip = False
Case vbCancel
oExcel.Quit
WScript.Quit(10) '* 10 Is the error code I use to indicate there was a user abort (1 because wasn't successful, + 0 because the user chose to exit)
End Select
PromptForSkip = gSkip
Exit Function
End Function
当你运行它时会发生什么?它会崩溃吗?或者没有文件?没有文件…可能会中断并检查strPath和strFile的值?在While中中断以查看它是否输入。或者尝试使用硬编码路径保存以查看它是否工作。旁注
xls.Workbooks.Application
与xls