Phpexcel inserting a picture into a cell to wrap a line

Source: Internet
Author: User
To export a table from the database to Excel using Phpexcel, the table has a picture path now 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 statement");    Sets the current sheet index for subsequent content operations.    It is generally only necessary to display a call when multiple sheet are used.    By default, Phpexcel automatically creates the first sheet is set sheetindex=0 $objExcel->setactivesheetindex (0);       $objActSheet = $objExcel->getactivesheet ();       Sets the name of the currently active sheet $objActSheet->settitle (' tttt '); //Set width, this value is different from Excel, do not know what unit, slightly smaller than the width of Excel $objactsheet-> Getcolumndimension (' A ')->setwidth (20); $objActSheet->getcolumndimension (' B ')->setwidth (15); $objActSheet->getrowdimension (1)->setrowheight (30); $objActSheet->getrowdimension (2)->setrowheight (27);  $objActSheet->getrowdimension (3)->setrowheight (16); Set the value of a cell $objActSheet->setcellvalue (' A1 ', ' titile '); Merge cell $objactsheet->mergecells (' a1:n1 ');    Set style $OBJSTYLEA1 = $objActSheet->getstyle (' A1 '); $objStyleA 1->getalignment ()->sethorizontal (phpexcel_style_alignment::horizontal_center); $objFontA 1 = $    Objstylea1->getfont ();    $objFontA 1->setname (' Song-Body ');  $objFontA 1->setsize (18);    $objFontA 1->setbold (TRUE); Set the center alignment $objactsheet->getstyle (' A2 ')->getalignment ()->sethorizontal (phpexcel_style_alignment:: Horizontal_center); $objActSheet->getstyle (' B2 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_center); $objActSheet->setcellvalue (' A2 ', ' Shoes picture '); $objActSheet->setcellvalue (' B2 ', ' Product code '); Set Border $objactsheet->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_style_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); $query 1 = mssql_query ($sql); $items =array (); $sales _sum=$ sales_sum+ $row 1["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 ($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->getstyle (' 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_border::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-type:application/download "); Header (' Content-disposition:inline;filename=" '. $outputFileName. ' "); Header ("Content-transfer-encoding:binary"), Header ("Expires:mon, 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 is as follows

Ask to change the path of the picture to the real picture output code how to change? And how to make the title of Excel appear in two lines and bold, how can the code be modified?


Reply to discussion (solution)

Picture below

Title wrapping simply add line breaks where you need to wrap the line
such as "Product \ncode"

The other tomorrow said

Title wrapping simply add line breaks where you need to wrap the line
such as "Product \ncode"

The other tomorrow said

$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  

/n \ n have tried ... Not even.

Bold example
$objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setbold (true);

Add Picture 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 Picture 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 do you add the code to insert a picture? I'm not doing this right

$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, $objDrawing->setcoordinates (' B15 '); $objDrawing- >setoffsetx, $objDrawing->setrotation (+), $objDrawing->getshadow ()->setvisible (true); Objdrawing->getshadow (->setdirection), $objDrawing->setworksheet ($objExcel->getactivesheet ());}

$objDrawing = new phpexcel_worksheet_drawing ();
To be placed within the loop, i.e. each picture requires a phpexcel_worksheet_drawing instance

SetHeight Pictures High
The cell where the setcoordinates is located
Setoffsetx x offset of the upper-left corner relative to the cell
Setrotation Rotation angle
See the documentation for more methods

$objDrawing = new phpexcel_worksheet_drawing ();
To be placed within the loop, i.e. each picture requires a phpexcel_worksheet_drawing instance

SetHeight Pictures High
The cell where the setcoordinates is located
Setoffsetx x offset of the upper-left corner relative to the cell
Setrotation Rotation angle
See the documentation for more methods
Export, but the position is not correct, to set the picture to the cell code how to change?

I setcoordinates (' B15 ') (15 rows B)
Are you also in Setcoordinates (' B15 ')?

I setcoordinates (' B15 ') (15 rows B)
Are you also in Setcoordinates (' B15 ')?
Sure enough, but there is a problem, I set the picture to the location of 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, $objDrawing->setcoordinates (' A '. $aa);// The image name is in cell $objdrawing->setoffsetx, $objDrawing->setrotation (), $objDrawing->getshadow () SetVisible (true); $objDrawing->getshadow ()->setdirection, $objDrawing->setworksheet ($objExcel, Getactivesheet ()); $aa + +;}

Out of effect is like this

The picture just appears in the cell position, not the cell value
How do I set the cell to achieve the following effect?

Didn't you say it?
$objDrawing->setoffsetx (80); Indicates that the left margin of the picture is 80
$objDrawing->setrotation (25); Indicates rotation 25 degrees
Obviously, it's not what you need.
You only need to set the cell center arrangement

Batch setup, I generally use phpexcel_style to write, such as

$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 ' and Phpexcel_style_alignment::vertical_ CENTER, ' wrap ' = true); $style _obj->applyfromarray ($style _array); $sheet->setsharedstyle ($style _obj, "b$y: $x $y");

Of course, the settings are no problem.

Didn't you say it?
$objDrawing->setoffsetx (80); Indicates that the left margin of the picture is 80
$objDrawing->setrotation (25); Indicates rotation 25 degrees
Obviously, it's not what you need.
You only need to set the cell center arrangement

Batch setup, I generally use phpexcel_style to write, such as

$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 ' and Phpexcel_style_alignment::vertical_ CENTER, ' wrap ' = true); $style _obj->applyfromarray ($style _array); $sheet->setsharedstyle ($style _obj, "b$y: $x $y");

Of course, the settings are no problem.
The format's done. Now there are two questions left:
1. Export the image to the cell inside.
2, the page has the Export Excel button, how to click the button, the page parameters to the background (the foreground page does not jump) to export the corresponding Excel.
Master, take this score with you. Little Brother score ... http://bbs.csdn.net/topics/390730241

1, export the picture above already said, you also did. Just pay attention to adjusting the formatting
2, the Export Excel button can be the form of the Submit button, set the target of the form to point to a hidden iframe will not jump

1, export the picture above already said, you also did. Just pay attention to adjusting the formatting
2, the Export Excel button can be the form of the Submit button, set the target of the form to point to a hidden iframe will not jump


Master again ask a question, when writing a picture to Excel, when the picture does not exist, Phpexcel will throw an exception cannot form an Excel file, so I catch the exception (commented out code)
if ("!== $row [" Photo "]) {//$row [" Photo "]=" picture/". $row [" Photo "];//try{$ Objdrawing->setname (' logo '), $objDrawing->setdescription (' logo '), $objDrawing->setpath ($row ["Photo"]); $ Objdrawing->setheight, $objDrawing->setcoordinates (' A '. $n);//The cell where the picture name is $objdrawing->setoffsetx (15) ; $objDrawing->setrotation; $objDrawing->getshadow ()->setvisible (true); $objDrawing->getshadow ()- >setdirection, $objDrawing->setworksheet ($objExcel->getactivesheet ());//}catch (Exception $e) {//$ Objdrawing->setname (' Logo1 ');//$objDrawing->setdescription (' Logo1 ');//$objDrawing->setpath ("Notf.jpg" )///$objDrawing->setheight,//$objDrawing->setcoordinates (' A '. $n);//The cell where the picture name is located//$objDrawing Setoffsetx ()//$objDrawing->setrotation (//$objDrawing->getshadow ()->setvisible (TRUE);//$ Objdrawing->getshadow (->setdirection),//$objDrawing->setworksheet ($objExcel->getactivesheet ()) ;// }}

So even if the picture does not exist can also export Excel, but the export of Excel is not open, how to solve?

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

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

How does the master output PDF document output?
My output file is small and small, and I can't open it.
/output content    //if ($outtype = = "pdf") {//Header (' content-type:application/pdf ');//Header (' Content-disposition: Attachment;filename= "Global network.pdf");//Header (' cache-control:max-age=0 ');//$objWriter = Phpexcel_iofactory: : Createwriter ($objExcel, ' PDF ');//$objWriter->save (' php://output ');//exit;//}else{$outputFileName = "Global Network.xls ";    To 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, 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 didn't do it, I just tried it.
Not only the style is ugly, but also can't display Chinese

  • 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.