PHPExcel inserts an image into a cell to display a line break.

Source: Internet
Author: User
PHPExcel inserts an image into a cell so that you can use phpexcel to export the table to an excel format from the database. The table contains an image path. my code is as follows:
 GetProperties (); $ objProps-> setCreator ("tt"); $ objProps-> setLastModifiedBy ("tt"); $ objProps-> setTitle ("tt "); $ objProps-> setSubject ("tt"); $ objProps-> setDescription ("tt"); $ objProps-> setKeywords ("tt "); $ objProps-> setCategory ("Change Report "); //*************************************/ /set the current sheet index, used for subsequent content operations. // Display the call only when multiple sheets are used. // By default, PHPExcel will automatically create the first sheet with the SheetIndex set to 0 $ objExcel-> setActiveSheetIndex (0); $ objActSheet = $ objExcel-> getActiveSheet (); // Set the name of the current active sheet $ objActSheet-> setTitle ('tttttt '); //*************************************/ /// set the width, this value is different from that in EXCEL. I don't know what unit it is. it is slightly smaller than the width of $ objActSheet-> getColumnDimension ('A')-> setWidth (20 ); $ objActSheet-> getColumnDimension ('B')-> setWidth (15); $ objActSheet-> getRowDimension (1)-> se TRowHeight (30); $ objActSheet-> getRowDimension (2)-> setRowHeight (27); $ objActSheet-> getRowDimension (3)-> setRowHeight (16 ); // Set the cell value $ objActSheet-> setCellValue ('A1', 'tile'); // merge cells $ objActSheet-> mergeCells ('A1: N1 '); // set the style $ objStyleA1 = $ objActSheet-> getStyle ('A1'); $ objStyleA1-> getAlignment ()-> setHorizontal (ALIGN: HORIZONTAL_CENTER ); $ objFontA1 = $ objStyleA1-> getFont (); $ objFontA 1-> setName (' '); $ objFontA1-> setSize (18); $ objFontA1-> setBold (true ); // Set center aligment $ objActSheet-> getStyle ('A2 ')-> getAlignment ()-> setHorizontal (Region: HORIZONTAL_CENTER); $ objActSheet-> getStyle ('B2 ') -> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER); $ objActSheet-> setCellValue ('A2 ', 'Shoes Picture '); $ objActSheet-> setCellValue ('B2', 'product code'); // set the border $ objA CtSheet-> getStyle ('A2 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('A2 ') -> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('A2 ')-> getBorders ()-> getRight () -> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('A2 ')-> getBorders ()-> getBottom ()-> setBorderStyle (PHPExcel_Sty Le_Border: BORDER_THIN); $ objActSheet-> getStyle ('B2')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN ); $ objActSheet-> getStyle ('B2')-> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('B2 ') -> getBorders ()-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('B2')-> getBorders ()-> getBottom ()-> SetBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ I = 1; $ query = mssql_query ($ SQL); $ query1 = mssql_query ($ SQL ); $ items = array (); $ sales_sum = $ sales_sum + $ row1 ["total_sales"]; while ($ row = mssql_fetch_array ($ query )) {$ row ["Photo"] = ".. /picture /". $ row ["Photo"]; $ n = $ I + 2; $ objActSheet-> getStyle ('B '. $ n)-> getNumberFormat ()-> setFormatCode ('@'); $ objActSheet-> getStyle ('E '. $ n)-> getNumberFormat ()-> setFormatCode ('@'); $ ObjActSheet-> getRowDimension ($ n)-> setRowHeight (16); $ objActSheet-> getStyle ('A '. $ n)-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('A '. $ n)-> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('A '. $ n)-> getBorders ()-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getSty Le ('A '. $ n)-> getBorders ()-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('B '. $ n)-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('B '. $ n)-> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> getStyle ('B '. $ n)-> getBorders ()-> getRight ()-> setBorderStyle (PHPExcel_Style_Bord Er: BORDER_THIN); $ objActSheet-> getStyle ('B '. $ n)-> getBorders ()-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN); $ objActSheet-> setCellValue ('A '. $ n, $ row ["Photo"]); $ objActSheet-> setCellValue ('B '. $ n, $ row ["Article_No"]); $ I ++ ;}$ outputFileName = "tables addminus1.xls"; header ("Content-Type: application/force-download "); header (" Content-Type: application/octet-stream "); header (" Content-T Ype: application/download "); header ('content-Disposition: inline; filename = "'. $ outputFileName. '"'); header (" Content-Transfer-Encoding: binary "); header (" Expires: Mon, 26 Jul 1997 05:00:00 GMT "); header (" Cache-Control: must-revalidate, post-check = 0, pre-check = 0 "); header (" Pragma: no-cache "); $ objWriter-> save ('php: // output');?>

The exported excel file is as follows:

How can I change the image path to a real image output code? In addition, how can I split the title of an excel file into two rows for display and bold, and how can I modify the code?


Reply to discussion (solution)

The image is as follows:

You only need to add a line break to the place where the line feed is needed.
For example, "Product \ ncode"

Others tomorrow

You only need to add a line break to the place where the line feed is needed.
For example, "Product \ ncode"

Others tomorrow

$objActSheet->setCellValue('A2', 'Shoes Picture'); $objActSheet->setCellValue('B2', 'Product code'); $objActSheet->setCellValue('C2', 'Original Retail  Price (VAT)'); $objActSheet->setCellValue('D2', '  Total \nOrdered Qty'); $objActSheet->setCellValue('E2', '  Total Sales Qty'); $objActSheet->setCellValue('F2', 'Sellthru'); $objActSheet->setCellValue('G2', 'Total Sales'); $objActSheet->setCellValue('H2', 'Cumulated Qty'); $objActSheet->setCellValue('I2', 'Cumulated  /nQty %'); 

/N \ n all tried... none.

Bold example
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setBold (true );

Add image example
$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
$ ObjDrawing-> setName ('logo ');
$ ObjDrawing-> setDescription ('logo ');
$ ObjDrawing-> setPath ('./images/officelogo.jpg ');
$ ObjDrawing-> setHeight (36 );
$ ObjDrawing-> setCoordinates ('b15 ');
$ ObjDrawing-> setOffsetX (110 );
$ ObjDrawing-> setRotation (25 );
$ ObjDrawing-> getShadow ()-> setVisible (true );
$ ObjDrawing-> getShadow ()-> setDirection (45 );
$ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());

Bold example
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setBold (true );

Add image example
$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
$ ObjDrawing-> setName ('logo ');
$ ObjDrawing-> setDescription ('logo ');
$ ObjDrawing-> setPath ('./images/officelogo.jpg ');
$ ObjDrawing-> setHeight (36 );
$ ObjDrawing-> setCoordinates ('b15 ');
$ ObjDrawing-> setOffsetX (110 );
$ ObjDrawing-> setRotation (25 );
$ ObjDrawing-> getShadow ()-> setVisible (true );
$ ObjDrawing-> getShadow ()-> setDirection (45 );
$ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());
Where should I add the code for inserting an image? I am not doing this

$objDrawing = new PHPExcel_Worksheet_Drawing();while($row=mssql_fetch_array($query)){if(''!==$row["Photo"]){$row["Photo"]="../picture/".$row["Photo"];$objDrawing->setName('Logo');$objDrawing->setDescription('Logo');$objDrawing->setPath($row["Photo"]);$objDrawing->setHeight(36);$objDrawing->setCoordinates('B15');$objDrawing->setOffsetX(110);$objDrawing->setRotation(25);$objDrawing->getShadow()->setVisible(true);$objDrawing->getShadow()->setDirection(45);$objDrawing->setWorksheet($objExcel->getActiveSheet());}

$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
Put it in a loop, that is, each image requires a PHPExcel_Worksheet_Drawing instance.

SetHeight Image height
Cell where setCoordinates is located
SetOffsetX: x offset from the upper left corner of the cell
SetRotation rotation angle
For more methods, see the document.

$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
Put it in a loop, that is, each image requires a PHPExcel_Worksheet_Drawing instance.

SetHeight Image height
Cell where setCoordinates is located
SetOffsetX: x offset from the upper left corner of the cell
SetRotation rotation angle
For more methods, see the document.
Exported, but the location is incorrect. how can I modify the code for setting the image to the cell?

My setCoordinates ('b15') (15 rows in column B)
Are you also using setCoordinates ('b15?

My setCoordinates ('b15') (15 rows in column B)
Are you also using setCoordinates ('b15?
This is true, but there is another problem. I set the image to the position where the image name appears.

While ($ row = mssql_fetch_array ($ query) {$ objDrawing = new PHPExcel_Worksheet_Drawing (); if (''! = $ Row ["Photo"]) {$ row ["Photo"] = "picture /". $ row ["Photo"]; $ objDrawing-> setName ('logo '); $ objDrawing-> setDescription ('logo '); $ objDrawing-> setPath ($ row ["Photo"]); $ objDrawing-> setHeight (36); $ objDrawing-> setCoordinates ('A '. $ aa); // The cell where the image name is located $ objDrawing-> setOffsetX (80); $ objDrawing-> setRotation (25); $ objDrawing-> getShadow () -> setVisible (true); $ objDrawing-> getShadow ()-> setDirection (45); $ objDrawing-> setWorksheet ($ objExcel-> getActiveSheet ()); $ aa ++ ;}

The result is as follows:

The image only appears at the cell position, not the cell value.
How to Set cells to achieve the following results?

Didn't you say that?
$ ObjDrawing-> setOffsetX (80); // the left margin of the image is 80.
$ ObjDrawing-> setRotation (25); // indicates 25 degrees of rotation.
Obviously not what you need
You only need to set the cells to center

Batch settings are usually written using PHPExcel_Style, as shown in figure

$style_obj = new PHPExcel_Style(); $style_array = array( 'borders' => array( 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN),'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),//'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN) ),'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,'vertical'=> PHPExcel_Style_Alignment::VERTICAL_CENTER,'wrap'=> true));$style_obj->applyFromArray($style_array); $sheet->setSharedStyle($style_obj, "B$y:$x$y");

Of course, there is no problem with setting one by one.

Didn't you say that?
$ ObjDrawing-> setOffsetX (80); // the left margin of the image is 80.
$ ObjDrawing-> setRotation (25); // indicates 25 degrees of rotation.
Obviously not what you need
You only need to set the cells to center

Batch settings are usually written using PHPExcel_Style, as shown in figure

$style_obj = new PHPExcel_Style(); $style_array = array( 'borders' => array( 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN),'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),//'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN) ),'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,'vertical'=> PHPExcel_Style_Alignment::VERTICAL_CENTER,'wrap'=> true));$style_obj->applyFromArray($style_array); $sheet->setSharedStyle($style_obj, "B$y:$x$y");

Of course, there is no problem with setting one by one.
Now we have two problems:
1. export the image to the cell.
2. there is an excel export button on the page. how to click the button to upload the page parameters to the background (the foreground page does not jump) to export the corresponding excel.
The master took this score together. The younger brother scored less... http://bbs.csdn.net/topics/390730241.

1. export the image as mentioned above, and you have done the same. You only need to adjust the format
2. the export excel button can be a form submission button. setting the target of the form to point to a hidden iframe will not jump.

 

1. export the image as mentioned above, and you have done the same. You only need to adjust the format
2. the export excel button can be a form submission button. setting the target of the form to point to a hidden iframe will not jump.

 

The master asked me another question. when I wrote an image to excel, when the image does not exist, phpexcel throws an exception and cannot form an excel file. so I caught the exception (the code that was commented out)
If (''! = $ Row ["Photo"]) {// $ row ["Photo"] = "picture /". $ row ["Photo"]; // try {$ objDrawing-> setName ('logo '); $ objDrawing-> setDescription ('logo '); $ objDrawing-> setPath ($ row ["Photo"]); $ objDrawing-> setHeight (36); $ objDrawing-> setCoordinates ('A '. $ n); // The cell where the image name is located $ objDrawing-> setOffsetX (15); $ objDrawing-> setRotation (25); $ objDrawing-> getShadow () -> setVisible (true); $ objDrawing-> getShadow ()-> setDirection (45); $ objDrawing-> setWorksheet ($ objExcel-> getActiveSheet ()); //} catch (Exception $ e) {// $ objDrawing-> setName ('logo1'); // $ objDrawing-> setDescription ('logo1 '); // $ objDrawing-> setPath ("notf.jpg"); // $ objDrawing-> setHeight (36); // $ objDrawing-> setCoordinates ('A '. $ n); // The cell where the image name is located // $ objDrawing-> setOffsetX (15); // $ objDrawing-> setRotation (25 ); // $ objDrawing-> getShadow ()-> setVisible (true); // $ objDrawing-> getShadow ()-> setDirection (45 ); // $ objDrawing-> setWorksheet ($ objExcel-> getActiveSheet ());//}}

In this way, excel can be exported even if the image does not exist, but the exported excel cannot be opened. how can this problem be solved?

if(file_exists("picture/".$row["Photo"])) {   .....  $objDrawing->setWorksheet($objExcel->getActiveSheet());}

if(file_exists("picture/".$row["Photo"])) {   .....  $objDrawing->setWorksheet($objExcel->getActiveSheet());}

How can I output the pdf output by the master?
The output file is very small and cannot be opened.
/Output Content // if ($ outtype = "pdf") {// header ('content-Type: application/pdf '); // header ('content-Disposition: attachment; filename = "Global network1_" '); // header ('cache-Control: max-age = 0'); // $ objWriter = PHPExcel_IOFactory :: createWriter ($ objExcel, 'PDF '); // $ objWriter-> save ('php: // output'); // exit; //} ELSE {$ outputFileName = "Global Network.xls"; // to the file // $ objWriter-> save ($ outputFileName); // to the browser header ("Content-Type: application/force-download "); header (" Content-Type: application/octet-stream "); header (" Content-Type: application/download "); header ('content-Disposition: inline; filename = "'. $ outputFileName. '"'); 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'); // exit ;//}

I did not do it. I tried it just now.
Not only is the style ugly, but it cannot display Chinese characters

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.