943,718 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3943
  • PHP RSS
Jul 27th, 2008
0

Generating an excel file.

Expand Post »
Hello guys!

I am working with an PHP page that will generate an excel file. The flow is all transaction made should be generated in an excel file. Also if there is another transation, the new information will be appended in the same excel file.

I have work on this and it generates an excel file, but it only shows in one cell of the excel file. Then when I try to append the new transaction made, it appends but the new information is appended in the same cell.(I used fopen for the option in writing to the file).

If anyone has an idea of how to place the information in different cells, would be appreciated. Below is the code I have taken from the internet and the output generated:

Page used to locate the directory where to place the generated excel file and calling a class to generate the excel file:

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. require_once "excel.php";
  4. $filename = "theFile.xls";
  5. ?>
  6. <p>
  7. </p>
  8. <?php $export_file = "xlsfile: \\xampp\\htdocs\\".$filename;
  9. $fp = fopen($export_file, "w+");
  10. if (!is_resource($fp))
  11. {
  12. die("Cannot open $export_file");
  13. }
  14. // typically this will be generated/read from a database table<br />
  15. $assoc = array(
  16. array("First name" => "Mattias", "IQ" => 250),
  17. array("First name" => "Tony", "IQ" => 100),
  18. array("First name" => "Peter", "IQ" => 100),
  19. array("First name" => "Edvard", "IQ" => 100)); ?> <br /> <?php
  20. fwrite($fp, serialize($assoc));
  21. fclose($fp);
  22.  
  23. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  24. header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
  25. header ("Cache-Control: no-cache, must-revalidate");
  26. header ("Pragma: no-cache");
  27. header ("Content-type: application/x-msexcel");
  28. header ("Content-Disposition: attachment; filename=\"" . $filename . "\"" );
  29. header ("Content-Description: PHP/INTERBASE Generated Data" );
  30. readfile($export_file);
  31. exit;
  32.  
  33.  
  34. ?>


Class that is called to generate an excel file:

php Syntax (Toggle Plain Text)
  1. <?php
  2. /**
  3.  * MS-Excel stream handler
  4.  * This class read/writes a data stream directly
  5.  * from/to a Microsoft Excel spreadsheet
  6.  * opened with the xlsfile:// protocol
  7.  * This is used to export associative array data directly to MS-Excel
  8.  * @requires PHP 4 >= 4.3.2
  9.  * @author Ignatius Teo <ignatius@act28.com>
  10.  * @copyright (C)2004 act28.com <http://act28.com>
  11.  * @version 0.3
  12.  * @date 20 Jan 2005
  13.  * $Id: excel.php,v 1.3 2005/01/20 09:58:58 Owner Exp $
  14.  */
  15. class xlsStream
  16. {
  17. /* private */
  18. var $position = 0; // stream pointer
  19. var $mode = "rb"; // default stream open mode
  20. var $xlsfilename = null; // stream name
  21. var $fp = null; // internal stream pointer to physical file
  22. var $buffer = null; // internal write buffer
  23. var $endian = "unknown"; // little | unknown | big endian mode
  24. var $bin = array(
  25. "big" => "v",
  26. "little" => "s",
  27. "unknown" => "s",
  28. );
  29.  
  30. /**
  31. * detect server endian mode
  32. * thanks to Charles Turner for picking this one up
  33. * @access private
  34. * @params void
  35. * @returns void
  36. * @see [url]http://www.phpdig.net/ref/rn45re877.html[/url]
  37. */
  38. function _detect()
  39. {
  40. // A hex number that may represent 'abyz'
  41. $abyz = 0x6162797A;
  42.  
  43. // Convert $abyz to a binary string containing 32 bits
  44. // Do the conversion the way that the system architecture wants to
  45. switch (pack ('L', $abyz))
  46. {
  47. // Compare the value to the same value converted in a Little-Endian fashion
  48. case pack ('V', $abyz):
  49. $this->endian = "little";
  50. break;
  51.  
  52. // Compare the value to the same value converted in a Big-Endian fashion
  53. case pack ('N', $abyz):
  54. $this->endian = "big";
  55. break;
  56.  
  57. default:
  58. $this->endian = "unknown";
  59. break;
  60. }
  61. }
  62.  
  63. /**
  64.   * called by fopen() to the stream
  65.   * @param (string) $path file path
  66.   * @param (string) $mode stream open mode
  67.   * @param (int) $options stream options (STREAM_USE_PATH |
  68.   * STREAM_REPORT_ERRORS)
  69.   * @param (string) $opened_path stream opened path
  70.   */
  71. function stream_open($path, $mode, $options, &$opened_path)
  72. {
  73. $url = parse_url($path);
  74. $this->xlsfilename = '/' . $url['host'] . $url['path'];
  75. $this->position = 0;
  76. $this->mode = $mode;
  77.  
  78. $this->_detect(); // detect endian mode
  79.  
  80. //@TODO: test for invalid mode and trigger error if required
  81.  
  82. // open underlying resource
  83. $this->fp = @fopen($this->xlsfilename, $this->mode);
  84. if (is_resource($this->fp))
  85. {
  86. // empty the buffer
  87. $this->buffer = "";
  88.  
  89. if (preg_match("/^w|x/", $this->mode))
  90. {
  91. // write an Excel stream header
  92. $str = pack(str_repeat($this->bin[$this->endian], 6), 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  93. fwrite($this->fp, $str);
  94. $opened_path = $this->xlsfilename;
  95. $this->position = strlen($str);
  96. }
  97. }
  98. return is_resource($this->fp);
  99. }
  100.  
  101. /**
  102.   * read the underlying stream resource (automatically called by fread/fgets)
  103.   * @todo modify this to convert an excel stream to an array
  104.   * @param (int) $byte_count number of bytes to read (in 8192 byte blocks)
  105.   */
  106. function stream_read($byte_count)
  107. {
  108. if (is_resource($this->fp) && !feof($this->fp))
  109. {
  110. $data .= fread($this->fp, $byte_count);
  111. $this->position = strlen($data);
  112. }
  113. return $data;
  114. }
  115.  
  116. /**
  117.   * called automatically by an fwrite() to the stream
  118.   * @param (string) $data serialized array data string
  119.   * representing a tabular worksheet
  120.   */
  121. function stream_write($data)
  122. {
  123. // buffer the data
  124. $this->buffer .= $data;
  125. $bufsize = strlen($data);
  126. return $bufsize;
  127. }
  128.  
  129. /**
  130.   * pseudo write function to manipulate the data
  131.   * stream before writing it
  132.   * modify this to suit your data array
  133.   * @access private
  134.   * @param (array) $data associative array representing
  135.   * a tabular worksheet
  136.   */
  137. function _xls_stream_write($data)
  138. {
  139. if (is_array($data) && !empty($data))
  140. {
  141. $row = 0;
  142. foreach (array_values($data) as $_data)
  143. {
  144. if (is_array($_data) && !empty($_data))
  145. {
  146. if ($row == 0)
  147. {
  148. // write the column headers
  149. foreach (array_keys($_data) as $col => $val)
  150. {
  151. // next line intentionally commented out
  152. // since we don't want a warning about the
  153. // extra bytes written
  154. // $size += $this->write($row, $col, $val);
  155. $this->_xlsWriteCell($row, $col, $val);
  156. }
  157. $row++;
  158. }
  159.  
  160. foreach (array_values($_data) as $col => $val)
  161. {
  162. $size += $this->_xlsWriteCell($row, $col, $val);
  163. }
  164. $row++;
  165. }
  166. }
  167. }
  168. return $size;
  169. }
  170.  
  171. /**
  172.   * Excel worksheet cell insertion
  173.   * (single-worksheet supported only)
  174.   * @access private
  175.   * @param (int) $row worksheet row number (0...65536)
  176.   * @param (int) $col worksheet column number (0..255)
  177.   * @param (mixed) $val worksheet row number
  178.   */
  179. function _xlsWriteCell($row, $col, $val)
  180. {
  181. if (is_float($val) || is_int($val))
  182. {
  183. // doubles, floats, integers
  184. $str = pack(str_repeat($this->bin[$this->endian], 5), 0x203, 14, $row, $col, 0x0);
  185. $str .= pack("d", $val);
  186. }
  187. else
  188. {
  189. // everything else is treated as a string
  190. $l = strlen($val);
  191. $str = pack(str_repeat($this->bin[$this->endian], 6), 0x204, 8 + $l, $row, $col, 0x0, $l);
  192. $str .= $val;
  193. }
  194. fwrite($this->fp, $str);
  195. $this->position += strlen($str);
  196. return strlen($str);
  197. }
  198.  
  199. /**
  200.   * called by an fclose() on the stream
  201.   */
  202. function stream_close()
  203. {
  204. if (preg_match("/^w|x/", $this->mode))
  205. {
  206. // flush the buffer
  207. $bufsize = $this->_xls_stream_write(unserialize($this->buffer));
  208.  
  209. // ...and empty it
  210. $this->buffer = null;
  211.  
  212. // write the xls EOF
  213. $str = pack(str_repeat($this->bin[$this->endian], 2), 0x0A, 0x00);
  214. $this->position += strlen($str);
  215. fwrite($this->fp, $str);
  216. }
  217.  
  218. // ...and close the internal stream
  219. return fclose($this->fp);
  220. }
  221.  
  222. function stream_eof()
  223. {
  224. $eof = true;
  225. if (is_resource($this->fp))
  226. {
  227. $eof = feof($this->fp);
  228. }
  229. return $eof;
  230. }
  231. }
  232.  
  233. stream_wrapper_register("xlsfile", "xlsStream")
  234. or die("Failed to register protocol: xlsfile");
  235. ?>


Output that is shown in one cell only:

a:4:{i:0;a:2:{s:10:"First name";s:7:"Mattias";s:2:"IQ";i:250;}i:1;a:2:{s:10:"First name";s:4:"Tony";s:2:"IQ";i:100;}i:2;a:2:{s:10:"First name";s:5:"Peter";s:2:"IQ";i:100;}i:3;a:2:{s:10:"First name";s:6:"Edvard";s:2:"IQ";i:100;}}


please, please help... Thanks a lot
Last edited by peter_budo; Jul 28th, 2008 at 5:41 am. Reason: Keep It Organized - please use [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dudegio is offline Offline
66 posts
since May 2007
Jul 28th, 2008
0

Re: Generating an excel file.

Excel is basically tab delimited. Basically, in between columns add a "\t" to insert a tab to let excel know that the first column is finished and the second column should start. To start a new row, add a "\n" to force excel to put the next data in a separate row.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jul 28th, 2008
0

Re: Generating an excel file.

I tried the tab, its works but the unnecessary characters are still showing as well as the \n is not working. The unnecessary characters are those in the output such as :, ;, {, } etc.
this value should be removed. please help...

Output:
a:4:{i:0;a:2:{s:10:"First name";s:7:"Mattias";s:2:"IQ";i:250;}i:1;a:2:{s:10:"First name";s:4:"Tony";s:2:"IQ";i:100;}i:2;a:2:{s:10:"First name";s:5:"Peter";s:2:"IQ";i:100;}i:3;a:2:{s:10:"First name";s:6:"Edvard";s:2:"IQ";i:100;}}

Below is the block of code I have modified for the \n and \t suggestion:

php Syntax (Toggle Plain Text)
  1. $assoc = array(
  2. array("First name\t" => "Mattias\t", "IQ\t" => 250 + "\n"),
  3. array("First name\t" => "Tony\t", "IQ\t" => 100 + "\n"),
  4. array("First name" => "Peter", "IQ" => 100),
  5. array("First name" => "Edvard", "IQ" => 100)); ?> <br /> <?php



help please.. thanks...
Last edited by peter_budo; Jul 28th, 2008 at 5:41 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dudegio is offline Offline
66 posts
since May 2007
Jul 28th, 2008
0

Re: Generating an excel file.

Hello Guys!

Please help on this if you have an idea.

Thanks..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dudegio is offline Offline
66 posts
since May 2007
Jul 28th, 2008
0

Re: Generating an excel file.

You could use a regular expression to evaluate the output to get rid of :, ;, {, }
php Syntax (Toggle Plain Text)
  1. <?php
  2. $string='a:4:{i:0;a:2:{s:10:"First name";s:7:"Mattias";s:2:"IQ";i:250;}i:1;a:2:{s:10:"First name";s:4:"Tony";s:2:"IQ";i:100;}i:2;a:2:{s:10:"First name";s:5:"Peter";s:2:"IQ";i:100;}i:3;a:2:{s:10:"First name";s:6:"Edvard";s:2:"IQ";i:100;}} ';
  3. preg_match_all ("/[^0-9^a-z^_^A-Z^.:]/", $string, $matches);
  4. // Loop through the matches with foreach
  5. foreach ($matches[0] as $value) {
  6. $string = str_replace($value, "", $string);
  7. }
  8. echo $string;
  9. ?>
This will return
a:4:i:0a:2:s:10:Firstnames:7:Mattiass:2:IQi:250i:1a:2:s:10:Firstnames:4:Tonys:2:IQi:100i:2a:
2:s:10:Firstnames:5:Peters:2:IQi:100i:3a:2:s:10:Firstnames:6:Edvards:2:IQi:100
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: POP3 help
Next Thread in PHP Forum Timeline: Problem passing PHP variable from one page to another





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC