Solved

PEAR - Spreadsheet Excel Writer - File error

Posted on 2008-10-25
2
1,012 Views
Last Modified: 2013-11-10
I query the database and retrieve the following array:

Array
(
      [0] => Array
      (
            [id] => 5689
            [item] => YUIOPD
            [size] => 8
            [cost] => $99.99
      )

      [1] => Array
      (
            [id] => 7664
            [item] => TYJKIP
            [size] => 12
            [cost] => $199.99
      )

      [2] => Array
      (
            [id] => 2113
            [item] => HYTIUP
            [size] => 4
            [cost] => $79.99
      )
)

I'm trying to enter that data into an excel spreadsheet using the Spreadsheet Excel Writer PEAR pkg. But, I keep getting: File error: data may be lost, and only the ids are in the spreadsheet, each on its own row.  I'm trying to figure out how to put each element into its own row for each one.

Right now, the code looks like this:
$excel = new Spreadsheet_Excel_Writer('textexcel.xls');
$sheet =& $excel->addWorksheet('worksheet 1');
$i = 0;
  foreach($res as $data) {
		foreach($data as $k => $v) {
				$sheet->write($i, $k, $v);
		}
		$i++;
  }
$excel->close();

Open in new window

0
Comment
Question by:n00b0101
[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
2 Comments
 
LVL 11

Accepted Solution

by:
MacAnthony earned 500 total points
ID: 22804254
I think the issue is you are using the associative array name for the column. Try this.
$excel = new Spreadsheet_Excel_Writer('textexcel.xls');
$sheet =& $excel->addWorksheet('worksheet 1');
$row = 0;
  foreach($res as $data) {
                $col = 0;
                foreach($data as $v) {
                                $sheet->write($row, $col, $v);
                                $col++;
                }
                $row++;
  }
$excel->close();

Open in new window

0
 

Author Closing Comment

by:n00b0101
ID: 31509971
Thanks so much!  
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP $_POST vs asp request 4 56
How to close my php post page. 3 41
Php variable to be sent back 3 34
PHP and accessing Array Elements 3 34
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

732 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