Excel2ANY转换工具

Excel2ANY.vbs

Option Explicit

Excel2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.xlsx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.pdf","PDF"
Excel2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.xlsx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.xps","XPS"
Excel2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.xlsx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.csv","CSV"

Private Sub Excel2ANY(inFile, outFile, outFormat)
	Dim objFSO, objExcel, objWorkbook, objSheet, xlFormat, isSaveAs

	Const xlAddIn                           =18
	Const xlAddIn8                          =18
	Const xlCSV                             =6
	Const xlCSVMac                          =22
	Const xlCSVMSDOS                        =24
	Const xlCSVWindows                      =23
	Const xlCurrentPlatformText             =-4158
	Const xlDBF2                            =7
	Const xlDBF3                            =8
	Const xlDBF4                            =11
	Const xlDIF                             =9
	Const xlExcel12                         =50
	Const xlExcel2                          =16
	Const xlExcel2FarEast                   =27
	Const xlExcel3                          =29
	Const xlExcel4                          =33
	Const xlExcel4Workbook                  =35
	Const xlExcel5                          =39
	Const xlExcel7                          =39
	Const xlExcel8                          =56
	Const xlExcel9795                       =43
	Const xlHtml                            =44
	Const xlIntlAddIn                       =26
	Const xlIntlMacro                       =25
	Const xlOpenDocumentSpreadsheet         =60
	Const xlOpenXMLAddIn                    =55
	Const xlOpenXMLStrictWorkbook           =61 
	Const xlOpenXMLTemplate                 =54
	Const xlOpenXMLTemplateMacroEnabled     =53
	Const xlOpenXMLWorkbook                 =51
	Const xlOpenXMLWorkbookMacroEnabled     =52
	Const xlSYLK                            =2
	Const xlTemplate                        =17
	Const xlTemplate8                       =17
	Const xlTextMac                         =19
	Const xlTextMSDOS                       =21
	Const xlTextPrinter                     =36
	Const xlTextWindows                     =20
	Const xlUnicodeText                     =42
	Const xlWebArchive                      =45
	Const xlWJ2WD1                          =14
	Const xlWJ3                             =40
	Const xlWJ3FJ3                          =41
	Const xlWK1                             =5
	Const xlWK1ALL                          =31
	Const xlWK1FMT                          =30
	Const xlWK3                             =15
	Const xlWK3FM3                          =32
	Const xlWK4                             =38
	Const xlWKS                             =4
	Const xlWorkbookDefault                 =51
	Const xlWorkbookNormal                  =-4143
	Const xlWorks2FarEast                   =28
	Const xlWQ1                             =34
	Const xlXMLSpreadsheet                  =46
	Const XlFixedFormatType_xlTypePDF       =0
	Const XlFixedFormatType_xlTypeXPS       =1

	' Create a File System object
	Set objFSO = CreateObject( "Scripting.FileSystemObject" )

	' Create a Excell object
	Set objExcel = CreateObject("Excel.Application")

	With objExcel
		' True: make Excell visible; False: invisible
		.Visible = True
 
		' Check if the Excell document exists
		If not( objFSO.FileExists( inFile ) ) Then
			WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
			' Close Excell
			.Quit
			Exit Sub
		End If
 
		' Open the Excell document
		.Workbooks.Open inFile
 
		' Make the opened file the active document
		Set objWorkbook = .ActiveWorkbook
		Set objSheet = .ActiveSheet

 		isSaveAs = True
		If StrComp(Ucase( outFormat ),"PDF") = 0 then
			isSaveAs = False
		ElseIf StrComp(Ucase( outFormat ),"XPS") = 0 then
			isSaveAs = False
		ElseIf StrComp(Ucase( outFormat ),"CSV") = 0 then
			xlFormat= xlCSV
		ElseIf StrComp(Ucase( outFormat ),"HTML") = 0 then
			xlFormat= xlHtml
		ElseIf StrComp(Ucase( outFormat ),"XML") = 0 then
			xlFormat= xlXMLSpreadsheet
		ElseIf StrComp(Ucase( outFormat ),"TXT") = 0 then
			xlFormat= xlTextWindows
		Else
			WScript.Echo "FILE FORTMART ERROR: Unknown file format" & vbCrLf
			' Close Excell
			.Quit
			Exit Sub
		End If

		' Save in PDF/XPS format
		If isSaveAs then
			objSheet.SaveAs outFile, xlFormat
		ElseIf StrComp(Ucase( outFormat ),"PDF") = 0 then
			objSheet.ExportAsFixedFormat XlFixedFormatType_xlTypePDF, outFile
		ElseIf StrComp(Ucase( outFormat ),"XPS") = 0 then
			objSheet.ExportAsFixedFormat XlFixedFormatType_xlTypeXPS, outFile
		End If
 
		' Close the active document
		objWorkbook.Close
 
		' Close Excell
		.Quit
	End With
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

*