Jump to content

[SOLVED] Export Data into Excel Spreadsheet


dennismonsewicz

Recommended Posts

Sure here is the code I am using:

 

The following is my code in my switch

 

case "export":	

			 include "../includes/db_login.php";
			  include "excelWriter.inc.php";

			  $excel=new ExcelWriter();

			  $myArr=array("Id","Product","Company","Description","Web Address","Last Used","Where product was last used","Is product active or inactive?");
			  $excel->writeLine($myArr);

			   $query = "SELECT * FROM table_name";
			  $result = mysql_query($query);
			  if(!$result){
				die("Could not query the database: <br/>" . mysql_error());
			  }
			  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
				$id = $row['rowid'];
				$company = $row['company'];
				$product = $row['product'];
				$description = $row['description'];
				$web = $row['web'];
				$last = $row['last'];
				$used = $row['used'];
				$active = $row['active'];
				$myArr = array($id,$product,$company,$description,$web,$last,$used,$active);

				$excel->writeLine($myArr);
			  }
			  $excel->send('hr');
			  exit;
		} 
		/* END SWITCH */

 

Here is the code I am using for my include:

 

<?php
  class ExcelWriter{
    var $data = null;
    var $filename = null;
    var $newRow = false;

    function ExcelWriter(){
      
    }
    
    function writeLine($line_arr){
      if(!is_array($line_arr)){
        trigger_error("Error : Argument is not valid. Supply an valid Array.",E_USER_WARNING);
        return false;
      }
      $this->data .= "  <tr>\n";
      foreach($line_arr as $value)
        $this->data .= "    <td class=xl24 width=64 >{$value}</td>\n";
      $this->data .= "  </tr>\n";
    }

    function send($filename = null){
      if($filename){
        header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
        header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
        header ( "Pragma: no-cache" );
        header ( "Content-type: application/x-msexcel" );
        header ( "Content-Disposition: attachment; filename={$filename}.xls" );
      }
      print $this->GetHeader();
      print $this->data;
      print $this->GetFooter();
      if($filename)
        exit;
    }

    function GetHeader(){
      ob_start();
?>
        <html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns="http://www.w3.org/TR/REC-html40">

        <head>
        <meta http-equiv=Content-Type content="text/html; charset=us-ascii">
        <meta name=ProgId content=Excel.Sheet>
        <!--[if gte mso 9]><xml>
         <o:DocumentProperties>
          <o:LastAuthor>Sriram</o:LastAuthor>
          <o:LastSaved>2005-01-02T07:46:23Z</o:LastSaved>
          <o:Version>10.2625</o:Version>
         </o:DocumentProperties>
         <o:OfficeDocumentSettings>
          <o:DownloadComponents/>
         </o:OfficeDocumentSettings>
        </xml><![endif]-->
        <style>
        <!--table
          {mso-displayed-decimal-separator:"\.";
          mso-displayed-thousand-separator:"\,";}
        @page
          {margin:1.0in .75in 1.0in .75in;
          mso-header-margin:.5in;
          mso-footer-margin:.5in;}
        tr
          {mso-height-source:auto;}
        col
          {mso-width-source:auto;}
        br
          {mso-data-placement:same-cell;}
        .style0
          {mso-number-format:General;
          text-align:general;
          vertical-align:bottom;
          white-space:nowrap;
          mso-rotate:0;
          mso-background-source:auto;
          mso-pattern:auto;
          color:windowtext;
          font-size:10.0pt;
          font-weight:400;
          font-style:normal;
          text-decoration:none;
          font-family:Arial;
          mso-generic-font-family:auto;
          mso-font-charset:0;
          border:none;
          mso-protection:locked visible;
          mso-style-name:Normal;
          mso-style-id:0;}
        td
          {mso-style-parent:style0;
          padding-top:1px;
          padding-right:1px;
          padding-left:1px;
          mso-ignore:padding;
          color:windowtext;
          font-size:10.0pt;
          font-weight:400;
          font-style:normal;
          text-decoration:none;
          font-family:Arial;
          mso-generic-font-family:auto;
          mso-font-charset:0;
          mso-number-format:General;
          text-align:general;
          vertical-align:bottom;
          border:none;
          mso-background-source:auto;
          mso-pattern:auto;
          mso-protection:locked visible;
          white-space:nowrap;
          mso-rotate:0;}
        .xl24
          {mso-style-parent:style0;
          white-space:normal;}
        -->
        </style>
        <!--[if gte mso 9]><xml>
         <x:ExcelWorkbook>
          <x:ExcelWorksheets>
           <x:ExcelWorksheet>
          <x:Name>srirmam</x:Name>
          <x:WorksheetOptions>
           <x:Selected/>
           <x:ProtectContents>False</x:ProtectContents>
           <x:ProtectObjects>False</x:ProtectObjects>
           <x:ProtectScenarios>False</x:ProtectScenarios>
          </x:WorksheetOptions>
           </x:ExcelWorksheet>
          </x:ExcelWorksheets>
          <x:WindowHeight>10005</x:WindowHeight>
          <x:WindowWidth>10005</x:WindowWidth>
          <x:WindowTopX>120</x:WindowTopX>
          <x:WindowTopY>135</x:WindowTopY>
          <x:ProtectStructure>False</x:ProtectStructure>
          <x:ProtectWindows>False</x:ProtectWindows>
         </x:ExcelWorkbook>
        </xml><![endif]-->
        </head>

        <body link=blue vlink=purple>
        <table x:str border=0 cellpadding=0 cellspacing=0 style='border-collapse: collapse;table-layout:fixed;'>
<?php
      return ob_get_clean();
    }

    function GetFooter(){
      return "</table></body></html>";
    }
  }
?>

Link to comment
Share on other sites

But my concern is that something is being printed before this code.

 

In the ExcelWriter class, update these lines:

 

    function send($filename = null){
      if(headers_sent()) die("Headers already sent"); //Add this line
      if($filename){
        header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );

Link to comment
Share on other sites

Ok...

 

I clicked your link and was able to open it.

 

I updated the PHP script, and still the same problem.

 

I did notice this though:

 

When I click on my link and try to open it, Excel displays this message:

 

23k6jpz.jpg

 

But when I click on your link it works and does not display the above message :(

Link to comment
Share on other sites

  • 7 years later...

Im using excelwriter.inc.php library to write data in XLS. I have a system where I select column names like "id" , "name", "description" etc. This columns can be chosen from list. The column count can vary from choosing 1 or 2 or 40 columns to appear in XLS.

As I chose more columns to appear in XLS. The data is not shown in proper format. Some data goes missing in between or with large number of chosen column the count is reduced to 8000-9000 rows.

Suppose if column count is around at max 2 . Suppose id , name , the total rows would be perfectly shown having 10k records. But as I increase the column say adding a description . The row count would reduce to suppose 8-9k. Now If I keep increasing more column than The record count keep on decreasing and also some columns data goes missing.

There is some problem with the excelwriter library to write data in XLS.

LOGIC : One sheet will have maximum 10k rows. If count exceeds 10k . It will generate a new sheet.

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.