Jump to content

Exporting Mysql Data


arpit_gadle

Recommended Posts

The simplest method would be to create a csv file and simply import that with Excel. Otherwise you will have to find an extension/set of classes for working with Excel in PHP such as PHPExcel. If you have a PHP version newer that 5.1.0 you can use...

 

$file = fopen('path.csv', 'w');

while($row = mysql_fetch_assoc($result)) {
   fputcsv($file, $row);
}

fclose($file);

Link to comment
Share on other sites

Dear Cags,

  Thanks for the solution. After creating CSV of the data returned by query what i have to is this?

Psuedo Code

 

1. String filename="test.xls";

2. open test.xls using fopen;

3. read data from CSV just created

4. write data to test.xls using some fwrite function;

 

 

Am I right;

 

 

Regards,

 

Arpit

Link to comment
Share on other sites

No. The point of creating a CSV file is that you don't have to mess around finding a decent library for working with the xls format, you completely sidestep it by using Excel manually. You would simply take the csv file, open Excel and click 'Import from CSV', then click save to give you an xls file. If you have to create the xls file with PHP then you will be looking at an awful lot more code than I posted in my last post.

Link to comment
Share on other sites

No the pseudo code doesn't make sense. The whole point of using the csv was to avoid the complexities of writing to the xls format. If you are going to write to xls using PHP there is no point in creating a csv file first, you might aswell simply take the data direct from the database. You will need to use a set of classes designed to manipulate xls files though, such as PHPExcel as I mentioned in my very first post.

Link to comment
Share on other sites

but you agree with the logic given in the psuedo code? I agree what you are saying but processing time taken by psuedo code logic is less than the generating and excel file using PHP Excel package and formatting is not my requirement only data is to be inserted to excel file from database and the rest of the things will be handle by data entry department viz formattingm filtering of excel etc....

Link to comment
Share on other sites

Because the specification shows the REQUIRED format of an xls file. How complex you think your problem is doesn't matter, a valid xls file must follow a set of specifications. Hence the fact I recommended using csv files which Excel can read and has a very simple file format.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.