2008-10-06 152 views

回答

8

這裏是將所有文件轉換成常規一個擴展名爲.xls的單個目錄。

它需要一個直接的方法。工作簿中的任何VBA代碼都被刪除,工作簿不會以.xlsm擴展名保存。任何不兼容性警告都不會顯示,而是會自動接受更改。

Sub Convert_xls_Files() 

Dim strFile As String 
Dim strPath As String 

    With Application 
     .EnableEvents = False 
     .DisplayAlerts = False 
     .ScreenUpdating = False 
    End With 
'Turn off events, alerts & screen updating 

     strPath = "C:\temp\excel\" 
     strFile = Dir(strPath & "*.xls") 
'Change the path as required 

    Do While strFile <> "" 
     Workbooks.Open (strPath & strFile) 
     strFile = Mid(strFile, 1, Len(strFile) - 4) & ".xlsx" 
     ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat:=xlOpenXMLWorkbook 
     ActiveWorkbook.Close True 
     strFile = Dir 
    Loop 
'Opens the Workbook, set the file name, save in new format and close workbook 

    With Application 
     .EnableEvents = True 
     .DisplayAlerts = True 
     .ScreenUpdating = True 
    End With 
'Turn on events, alerts & screen updating 

End Sub 
+1

感謝羅伯特,它工作得很好。我改變的唯一的事情是FileFormat:= XlFileFormat.xlXMLSpreadsheet(我正在使用Excel 2003) – kristof 2008-10-07 10:06:29

3

你能適應我張貼在這裏的代碼:

http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/01/loading-office-documents-in-net.aspx

它顯示瞭如何保存爲PDF(字顯示在博客,但如果你下載的解決方案,它具有代碼Excel和PPT)。

您需要找到保存爲新格式而不是導出的功能(可能最簡單的方法是記錄您在Excel中執行此操作的宏,然後查看代碼)。

+0

謝謝樓主的兩個答案,並在糾正問題文件擴展名。當我需要更全面的解決方案時,我可能會考慮您的建議。至於現在我用VBA OK – kristof 2008-10-07 09:59:00

0

最簡單的方法是爲一個文件錄製宏,然後手動編輯宏以使用循環對文件夾中的文件執行此類操作。在宏中,您可以使用標準的VB函數來獲取目錄中的所有文件並對其進行過濾。你可以看看http://www.xtremevbtalk.com/archive/index.php/t-247211.html瞭解更多信息。

3

打開它們加起來,然後按ALT + F11去宏編輯器,然後輸入類似:

Sub SaveAllAsXml() 
    Dim wbk As Workbook 
    For Each wbk In Application.Workbooks 
     wbk.SaveAs FileFormat:=XlFileFormat.xlXMLSpreadsheet 
    Next 
End Sub 

然後按F5鍵來運行它。可能需要一些調整,因爲我沒有測試過它。

+0

謝謝,這很有幫助 – kristof 2008-10-07 09:55:26

1

聽起來像一個工作,爲我所有時間最受我最低估的語言:VBScript!

將這個文本文件,並延長「.VBS」:

set xlapp = CreateObject("Excel.Application") 
set fso = CreateObject("scripting.filesystemobject") 
set myfolder = fso.GetFolder("YOURFOLDERPATHHERE") 
set myfiles = myfolder.Files 
for each f in myfiles 
    set mybook = xlapp.Workbooks.Open(f.Path) 
    mybook.SaveAs f.Name & ".xml", 47 
    mybook.Close 
next 

我沒有測試過這一點,但它應該工作

0
Const xlXLSX = 51 

REM 51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx) 
REM 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm) 
REM 50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb) 
REM 56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls) 

dim args 
dim file 
dim sFile 
set args=wscript.arguments 

dim wshell 
Set wshell = CreateObject("WScript.Shell") 

Set objExcel = CreateObject("Excel.Application") 

Set objWorkbook = objExcel.Workbooks.Open(wshell.CurrentDirectory&"\"&args(0)) 

objExcel.DisplayAlerts = FALSE 

objExcel.Visible = FALSE 

objWorkbook.SaveAs wshell.CurrentDirectory&"\"&args(1), xlXLSX 

objExcel.Quit 

Wscript.Quit 
相關問題