1
votes

Objective: Use PHP to call a vbs that converts an xls/xlsx file to a csv.

Question: How can I pass a source file path and a destination file path to a vbs that converts xls/xlsx to csv and run that vbs in a PHP web application?

Details: I have a working vbs that takes a source file path and a destination file path and converts the xls/xlsx at source file path into a csv. I can execute it from the Windows cmd line and it does exactly what I want it to do. I can also put the execution command into a bat file and run the bat file to achieve the same results. However, when I use exec()/shell_exec()/system() in PHP to execute the same command no csv is created. (If I try to run the bat from PHP using system() the contents of the bat file show up on the page, in fact, echo Conversion complete! prints "echo Conversion complete! Conversion complete.") I haven't seen any errors yet.

Note: I know about PHPExcel, I'd prefer not to use it.

excelToCsv.vbs

On Error Resume Next
if WScript.Arguments.Count < 2 Then WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
Wscript.Quit
End If
csv_format = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.SaveAs dest_file, csv_format
oBook.Close False
oExcel.Quit

batConverter.bat

excelToCsv.vbs conversionTestSourceMS2003.xls batTest.csv
echo Conversion Complete!

index.phtml

<?php
system("cmd /c batConvert.bat")
?>

Note: All of the above files (along with conversionTestSourceMS2003.xls) are in the same directory. I have not implemented any way to pass the parameters (since I can't get it to work even if it's all hard coded...)

Set Up: PHP5, Zend Framework, WAMP, Windows 7 (localhost).

1
Can you show us an example of the PHP you were using to execute the VBScript? I'd like to see how you are specifying the path.jveazey
@jveazey Yep, that's index.phtml above, for now all that page has is a call to my bat file via system(). All of the files are in the same folder so the path should be correct. I've also tried full paths (from C:) to no avail...lostphilosopher
Is it necessary to use the batch file?jveazey
@jveazey Nope, I originally didn't have one, but tried it as a possible solution.lostphilosopher

1 Answers

0
votes

For the sake of simplicity, I merged everything into a single ASP page. This will allow me to hopefully see a similar problem in IIS, and since it is in a single ASP script, I will be able to see the error more directly. My test machine is running on Windows Vista SP2 on IIS7 with Excel 2007 SP3.

excelToCsv.asp

<%

Option Explicit

Dim csv_format, src_file, dest_file, strPath, objFSO

csv_format = 6
src_file = "conversionTestSourceMS2003.xls"
dest_file = "testbat.csv"

strPath = "[HARDCODED PATH HERE]\"
src_file = strPath & src_file
dest_file = strPath & dest_file

Dim objExcel, objBook
Set objExcel = CreateObject("Excel.Application")

Set objBook = objExcel.Workbooks.Open(src_file)
objBook.SaveAs dest_file, csv_format
objBook.Close False

Response.Write "Conversion Complete!"
objExcel.Quit

%>

When running this code, I got a generic ASP error. So, I enabled detailed error messages in ASP and I get this following error...

Microsoft Office Excel error '800a03ec'

Microsoft Office Excel cannot access the file '[HARDCODED PATH HERE]\conversionTestSourceMS2003.xls'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook.

/temp/ExcelToCsv.asp, line 18

Now, this is not Apache, but I do believe the problem is related to yours. This error implies there is a security/permission problem where Excel cannot do what it needs to do to access or read the file. In fact, I encountered similar errors when I was executing the VBScript (and passing the error up the chain) from PHP (in IIS).

I believe it can be resolved by changing the Windows User being used to create the process. This can be configured in Services.msc by editing the Apache service and changing the Log On tab to an actual Windows user instead of a Service Account. I have not tested it yet, though, since setting up Apache is not something I can do right now.