micmania1 Posted September 8, 2009 Share Posted September 8, 2009 I've pulled data from a database and want to save it to an excel file. So far my COm section of my script looks like this: $excel = new COM('excel.application') or die("Excel could not be started."); $excel->Visible = 1; $excel->DisplayAlerts = 0; // add a workbook $excel->Workbooks->Add(); // save $Workbook = $excel->Workbooks[1]; $Workbook->SaveAs("C:\\xampp\\htdocs\\foo\\excel\\test.xlsx"); $Workbook->Worksheets->Add(); $Worksheet = $Workbook->Worksheets[1]; $cell = $Worksheet->Cells(1, 2); $excel->Quit(); How do I write a value to a cell? I've tried this: $cell->Value = 'Hello, World!'; ... without success. Also, another problem is, the script it will be generated with is running on a new version of excel and by default creates it with the new *.xlsx format. Thus meaning when I try to save as *.xls and open the file in excel, it tells me that it isn't written in the correct format and asks if I want to still open the file. This is because the format is actually *.xlsx but the filename says *.xls. Does anybody have links to any informative sites which has more info about using excel with COM? Thanks Link to comment https://forums.phpfreaks.com/topic/173516-com-excelapplication/ Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 I have not used PHPExcel a lot, but I tested it once and it supports also .xlsx files. This could be probably the best tool to work with excel sheets & php. Maybe give it a try ? http://phpexcel.codeplex.com/ Also I've seen one of the developers posting in these forums, a guy called Mark Baker, so it could be easy to get guidance also. Link to comment https://forums.phpfreaks.com/topic/173516-com-excelapplication/#findComment-914660 Share on other sites More sharing options...
micmania1 Posted September 8, 2009 Author Share Posted September 8, 2009 Stupidity got the better of me here I don't save after I make the change. My new script for anybodies future reference: $excel = new COM('excel.application') or die("Excel could not be started."); $excel->Visible = 1; $excel->DisplayAlerts = 0; // add a workbook $excel->Workbooks->Add(); // save $Workbook = $excel->Workbooks[1]; $Workbook->Worksheets->Add(); $Worksheet = $Workbook->Worksheets[1]; $cell = $Worksheet->Cells(1, 2)->Value = 'Hello, World'; $Workbook->SaveAs("C:\\excel\\test.xlsx"); $excel->Quit(); Link to comment https://forums.phpfreaks.com/topic/173516-com-excelapplication/#findComment-914663 Share on other sites More sharing options...
micmania1 Posted September 8, 2009 Author Share Posted September 8, 2009 I have found out how to save the file as .xls and open fine. $Workbook->SaveAs("C:\\excel\\test.xlsx", 1); The 1 seems to work fine. To save to the latest version, do not enter a 2nd parameter. Link to comment https://forums.phpfreaks.com/topic/173516-com-excelapplication/#findComment-914693 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.