How does PHP export an Excel file after the query ends?

Source: Internet
Author: User
How does PHP export the query end Excel PHPexcel export

 
 ";} Sqlsrv_free_stmt ($ query); sqlsrv_close ($ conn);?> 
  
User ID User name
$ Row [0] $ Row [1]


Reply to discussion (solution)

Phpexcel
Http://www.jz123.cn/text/2319267.html

Fputcsv ()

Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename =" aaa.xls ");

Phpexcel
Http://www.jz123.cn/text/2319267.html
+ 1

Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename =" aaa.xls ");
+ 1

In fact, excel has powerful functions ..

$ Str = "A1 \ tA2 \ tA3 \ n"; $ str. = "B1 \ tB2 \ b2\ n". you can write these tokens to the file and change the suffix to .xls. excel can read them directly ..

In fact, excel has powerful functions ..

$ Str = "A1 \ tA2 \ tA3 \ n"; $ str. = "B1 \ tB2 \ b2\ n". you can write these tokens to the file and change the suffix to .xls. excel can read them directly ..


But it is not actually a real xls file.


In fact, excel has powerful functions ..

$ Str = "A1 \ tA2 \ tA3 \ n"; $ str. = "B1 \ tB2 \ b2\ n". you can write these tokens to the file and change the suffix to .xls. excel can read them directly ..


But it is not actually a real xls file.
$query = sqlsrv_query($conn,$sql);if( $query === false) {    die( print_r( sqlsrv_errors(), true) );}while($row = sqlsrv_fetch_array($query)) { echo$objPHPExcel->getActiveSheet()->setCellValue('A2', convertUTF8($row[0]))->setCellValue('B2', convertUTF8($row[1]))->setCellValue('C2', convertUTF8($row[2]))->setCellValue('D2', convertUTF8($row[3]))->setCellValue('E2', convertUTF8($row[4]))->setCellValue('F2', convertUTF8($row[5]))->setCellValue('G2', convertUTF8($row[6]))->setCellValue('H2', convertUTF8($row[7]))->setCellValue('I2', convertUTF8($row[8]))->setCellValue('J2', convertUTF8($row[9]))->setCellValue('K2', convertUTF8($row[10]))->setCellValue('L2', convertUTF8($row[11]));}
Is there a problem with this code? Why is the following error reported during execution:

Catchable fatal error: Object of class PHPExcel_Worksheet cocould not be converted to string in D: \ MarsdenWeb \ Report \ output_002.php on line 19

$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.

$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.


$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.

-> SetCellValue ('A2 ', convertUTF8 ($ row [0])
-> SetCellValue ('B2', convertUTF8 ($ row [1])
-> SetCellValue ('C2 ', convertUTF8 ($ row [2])
-> SetCellValue ('D2 ', convertUTF8 ($ row [3])
-> SetCellValue ('E2', convertUTF8 ($ row [4])
-> SetCellValue ('F2', convertUTF8 ($ row [5])
-> SetCellValue ('G2 ', convertUTF8 ($ row [6])
-> SetCellValue ('h2 ', convertUTF8 ($ row [7])
-> SetCellValue ('i2 ', convertUTF8 ($ row [8])
-> SetCellValue ('j2 ', convertUTF8 ($ row [9])
-> SetCellValue ('K2', convertUTF8 ($ row [10])
-> SetCellValue ('l2', convertUTF8 ($ row [11]);

Some cycles marked with red are not changed, so the values of the previous cells are overwritten.



$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.

-> SetCellValue ('A2 ', convertUTF8 ($ row [0])
-> SetCellValue ('B2', convertUTF8 ($ row [1])
-> SetCellValue ('C2 ', convertUTF8 ($ row [2])
-> SetCellValue ('D2 ', convertUTF8 ($ row [3])
-> SetCellValue ('E2', convertUTF8 ($ row [4])
-> SetCellValue ('F2', convertUTF8 ($ row [5])
-> SetCellValue ('G2 ', convertUTF8 ($ row [6])
-> SetCellValue ('h2 ', convertUTF8 ($ row [7])
-> SetCellValue ('i2 ', convertUTF8 ($ row [8])
-> SetCellValue ('j2 ', convertUTF8 ($ row [9])
-> SetCellValue ('K2', convertUTF8 ($ row [10])
-> SetCellValue ('l2', convertUTF8 ($ row [11]);

Some cycles marked with red are not changed, so the values of the previous cells are overwritten.
I don't quite understand what you mean. how should I change it?




$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.

-> SetCellValue ('A2 ', convertUTF8 ($ row [0])
-> SetCellValue ('B2', convertUTF8 ($ row [1])
-> SetCellValue ('C2 ', convertUTF8 ($ row [2])
-> SetCellValue ('D2 ', convertUTF8 ($ row [3])
-> SetCellValue ('E2', convertUTF8 ($ row [4])
-> SetCellValue ('F2', convertUTF8 ($ row [5])
-> SetCellValue ('G2 ', convertUTF8 ($ row [6])
-> SetCellValue ('h2 ', convertUTF8 ($ row [7])
-> SetCellValue ('i2 ', convertUTF8 ($ row [8])
-> SetCellValue ('j2 ', convertUTF8 ($ row [9])
-> SetCellValue ('K2', convertUTF8 ($ row [10])
-> SetCellValue ('l2', convertUTF8 ($ row [11]);

Some cycles marked with red are not changed, so the values of the previous cells are overwritten.
I don't quite understand what you mean. how should I change it?

$i = 2;while($row = sqlsrv_fetch_array($query)) {$objPHPExcel->getActiveSheet()        ->setCellValue('A'.$i, convertUTF8($row[0]))        ->setCellValue('B'.$i, convertUTF8($row[1]))        ->setCellValue('C'.$i, convertUTF8($row[2]))        ->setCellValue('D'.$i, convertUTF8($row[3]))        ->setCellValue('E'.$i, convertUTF8($row[4]))        ->setCellValue('F'.$i, convertUTF8($row[5]))        ->setCellValue('G'.$i, convertUTF8($row[6]))        ->setCellValue('H'.$i, convertUTF8($row[7]))        ->setCellValue('I'.$i, convertUTF8($row[8]))        ->setCellValue('J'.$i, convertUTF8($row[9]))        ->setCellValue('K'.$i, convertUTF8($row[10]))        ->setCellValue('L'.$i, convertUTF8($row[11]));$i++;}





$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.

-> SetCellValue ('A2 ', convertUTF8 ($ row [0])
-> SetCellValue ('B2', convertUTF8 ($ row [1])
-> SetCellValue ('C2 ', convertUTF8 ($ row [2])
-> SetCellValue ('D2 ', convertUTF8 ($ row [3])
-> SetCellValue ('E2', convertUTF8 ($ row [4])
-> SetCellValue ('F2', convertUTF8 ($ row [5])
-> SetCellValue ('G2 ', convertUTF8 ($ row [6])
-> SetCellValue ('h2 ', convertUTF8 ($ row [7])
-> SetCellValue ('i2 ', convertUTF8 ($ row [8])
-> SetCellValue ('j2 ', convertUTF8 ($ row [9])
-> SetCellValue ('K2', convertUTF8 ($ row [10])
-> SetCellValue ('l2', convertUTF8 ($ row [11]);

Some cycles marked with red are not changed, so the values of the previous cells are overwritten.
I don't quite understand what you mean. how should I change it?

$i = 2;while($row = sqlsrv_fetch_array($query)) {$objPHPExcel->getActiveSheet()        ->setCellValue('A'.$i, convertUTF8($row[0]))        ->setCellValue('B'.$i, convertUTF8($row[1]))        ->setCellValue('C'.$i, convertUTF8($row[2]))        ->setCellValue('D'.$i, convertUTF8($row[3]))        ->setCellValue('E'.$i, convertUTF8($row[4]))        ->setCellValue('F'.$i, convertUTF8($row[5]))        ->setCellValue('G'.$i, convertUTF8($row[6]))        ->setCellValue('H'.$i, convertUTF8($row[7]))        ->setCellValue('I'.$i, convertUTF8($row[8]))        ->setCellValue('J'.$i, convertUTF8($row[9]))        ->setCellValue('K'.$i, convertUTF8($row[10]))        ->setCellValue('L'.$i, convertUTF8($row[11]));$i++;}

Thank you! Finally solved the export problem...
But it is automatically saved now. Is there a way to save it as your own?






$ ObjPHPExcel> getActiveSheet ()
-> The result of the setCellValue () operation is to return the PHPExcel_Worksheet object. remove the preceding echo.
After the echo is removed, no error is reported, but the while loop seems to be invalid, and only one data record is exported.

-> SetCellValue ('A2 ', convertUTF8 ($ row [0])
-> SetCellValue ('B2', convertUTF8 ($ row [1])
-> SetCellValue ('C2 ', convertUTF8 ($ row [2])
-> SetCellValue ('D2 ', convertUTF8 ($ row [3])
-> SetCellValue ('E2', convertUTF8 ($ row [4])
-> SetCellValue ('F2', convertUTF8 ($ row [5])
-> SetCellValue ('G2 ', convertUTF8 ($ row [6])
-> SetCellValue ('h2 ', convertUTF8 ($ row [7])
-> SetCellValue ('i2 ', convertUTF8 ($ row [8])
-> SetCellValue ('j2 ', convertUTF8 ($ row [9])
-> SetCellValue ('K2', convertUTF8 ($ row [10])
-> SetCellValue ('l2', convertUTF8 ($ row [11]);

Some cycles marked with red are not changed, so the values of the previous cells are overwritten.
I don't quite understand what you mean. how should I change it?

$i = 2;while($row = sqlsrv_fetch_array($query)) {$objPHPExcel->getActiveSheet()        ->setCellValue('A'.$i, convertUTF8($row[0]))        ->setCellValue('B'.$i, convertUTF8($row[1]))        ->setCellValue('C'.$i, convertUTF8($row[2]))        ->setCellValue('D'.$i, convertUTF8($row[3]))        ->setCellValue('E'.$i, convertUTF8($row[4]))        ->setCellValue('F'.$i, convertUTF8($row[5]))        ->setCellValue('G'.$i, convertUTF8($row[6]))        ->setCellValue('H'.$i, convertUTF8($row[7]))        ->setCellValue('I'.$i, convertUTF8($row[8]))        ->setCellValue('J'.$i, convertUTF8($row[9]))        ->setCellValue('K'.$i, convertUTF8($row[10]))        ->setCellValue('L'.$i, convertUTF8($row[11]));$i++;}

Thank you! Finally solved the export problem...
But it is automatically saved now. Is there a way to save it as your own?

Don't read the document?
Last-> save ()

Changed:
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ('content-Disposition: inline; filename = "'. $ FileName .'"');
Header ("Content-Transfer-Encoding: binary ");
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Pragma: no-cache ");
$ ObjWriter-> save ('php: // output ');


In fact, excel has powerful functions ..

$ Str = "A1 \ tA2 \ tA3 \ n"; $ str. = "B1 \ tB2 \ b2\ n". you can write these tokens to the file and change the suffix to .xls. excel can read them directly ..




But it is not actually a real xls file.


Well, it is indeed not, and there is still the possibility of garbled characters in Chinese outside China. later it was changed to html, which not only solved the garbled text, but also controlled the style. I have to say that this excel file is too powerful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.