2008-10-06 250 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 
相关问题