Solved

Creating Excel Files with PHP

Posted on 2004-08-25
11
336 Views
Last Modified: 2008-02-01
I want to be able to create excel files with my php code and have the excel file saved to a location on the server.
I won't want it opening into excelon the users side I just want to create the excel file on the server side and have it stored somewhere.

Does anyone know of a tutorial on how to do this?

Kelly

0
Comment
Question by:unreal400
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Expert Comment

by:pjargon
ID: 11894983
I looked into doing that for a little while, it didn't really work
If you're just handling data, why not just make a  csv?  Excel can read csv just fine (comma separated values)
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11895076
It's possible, but as pjargon says, seems prone to errors.  If you want to try it anyway, here's a couple places to look:

http://jaxn.org/cataBlog.php?articleID=290
http://www.devshed.com/c/a/PHP/PHP-and-COM/1/
0
 
LVL 2

Author Comment

by:unreal400
ID: 11895095
Whats the easiest way to write out the files to the server.  I've never worked with creating files. just creating the webpages with php.  Can you point me to an example.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11895197
0
 
LVL 1

Expert Comment

by:pjargon
ID: 11895204
Should be pretty simple file output, yes?
thx google:  http://www.softwareforeducation.com/php/13file-io.htm

You may have to mess with permissions
0
 
LVL 11

Expert Comment

by:neester
ID: 11898067
I have found the easiest is a TAB delimted file.
Since sometimes values need to contain a ','
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11899826
if you save everything into arrays, you can then write them to a file using serialize

http://www.php.net/serialize
http://www.php.net/unserialize

and read them using unserialize
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11900615
About writing to a file - directly from php.net help on fwrite():
http://www.php.net/manual/en/function.fwrite.php

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
        print "Cannot open file ($filename)";
        exit;
   }

   // Write $somecontent to our opened file.
   if (!fwrite($handle, $somecontent)) {
       print "Cannot write to file ($filename)";
       exit;
   }
   
   print "Success, wrote ($somecontent) to file ($filename)";
   
   fclose($handle);
                   
} else {
   print "The file $filename is not writable";
}
?>
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11903017
Commas inside fields is not a problem with CSV, because field content is usually quoted, thus escaping the commas. In some instances it will even allow line breaks inside fields. CSV for excel works very nicely as long as you're not doing anything that's excel specific, like drawing graphs or running macros, and is very simple to do. I generate Excel-tagged CSV for downloads - the important thing is to give it the correct MIME type for it to be picked up by Excel, which is application/msexcel.
0
 
LVL 1

Expert Comment

by:njava
ID: 11918888
0
 
LVL 1

Accepted Solution

by:
laurenty earned 50 total points
ID: 11927710
I generate Excel files from PHP all the time and use the PEAR  Spreadsheet_Excel_Writer. It does a very nice job and has some very useful functions that simplifying the whole process of generating/writing/sending the excel file.

http://pear.php.net/package/Spreadsheet_Excel_Writer



0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question