Phpexcel Chinese help Manual (Knowledge point)

Source: Internet
Author: User
Tags echo date getcolor
    1. $objPHPExcel->getactivesheet ()->setcellvalue (' A1 ', ' String ');
    2. $objPHPExcel->getactivesheet ()->setcellvalue (' A2 ', 12);
    3. $objPHPExcel->getactivesheet ()->setcellvalue (' A3 ', true);
    4. $objPHPExcel->getactivesheet ()->setcellvalue (' C5 ', ' =sum (C2:C4) ');
    5. $objPHPExcel->getactivesheet ()->setcellvalue (' B8 ', ' =min (B2:C5) ');
Copy Code

Merge cells

    1. $objPHPExcel->getactivesheet ()->mergecells (' A18:e22 ');
Copy Code

Separating cells

    1. $objPHPExcel->getactivesheet ()->unmergecells (' a28:b28 ');
Copy Code

Protect cell

    1. $objPHPExcel->getactivesheet ()->getprotection ()->setsheet (true); Needs to BES set to true in order to enable any worksheet protection!
    2. $objPHPExcel->getactivesheet ()->protectcells (' A3:e13 ', ' phpexcel ');
Copy Code

Set format

    1. Set Cell number formats
    2. echo Date (' H:i:s '). "Set cell number formats\n";
    3. $objPHPExcel->getactivesheet ()->getstyle (' E4 ')->getnumberformat ()->setformatcode (phpexcel_style_ Numberformat::format_currency_eur_simple);
    4. $objPHPExcel->getactivesheet ()->duplicatestyle ($objPHPExcel->getactivesheet ()->getstyle (' E4 '), ' E5: E13 ');
Copy Code

Setting width width

    1. Set column Widths
    2. $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setautosize (true);
    3. $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (12);
Copy Code

Set font

    1. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setname (' Candara ');
    2. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setsize (20);
    3. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setbold (true);
    4. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setunderline (Phpexcel_style_font:: Underline_single);
    5. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_white);
    6. $objPHPExcel->getactivesheet ()->getstyle (' E1 ')->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_white);
    7. $objPHPExcel->getactivesheet ()->getstyle (' D13 ')->getfont ()->setbold (true);
    8. $objPHPExcel->getactivesheet ()->getstyle (' E13 ')->getfont ()->setbold (true);
Copy Code

Set align

    1. $objPHPExcel->getactivesheet ()->getstyle (' D11 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
    2. $objPHPExcel->getactivesheet ()->getstyle (' D12 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
    3. $objPHPExcel->getactivesheet ()->getstyle (' D13 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
    4. $objPHPExcel->getactivesheet ()->getstyle (' A18 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_justify);
Copy Code

Center vertically

    1. $objPHPExcel->getactivesheet ()->getstyle (' A18 ')->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center);
Copy Code

Set the border of column

    1. $objPHPExcel->getactivesheet ()->getstyle (' A4 ')->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
    2. $objPHPExcel->getactivesheet ()->getstyle (' B4 ')->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
    3. $objPHPExcel->getactivesheet ()->getstyle (' C4 ')->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
    4. $objPHPExcel->getactivesheet ()->getstyle (' D4 ')->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
    5. $objPHPExcel->getactivesheet ()->getstyle (' E4 ')->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
Copy Code

Set the color of the border

    1. $objPHPExcel->getactivesheet ()->getstyle (' D13 ')->getborders ()->getleft ()->getcolor () Setargb (' FF993300 ');
    2. $objPHPExcel->getactivesheet ()->getstyle (' D13 ')->getborders ()->gettop ()->getcolor ()->setargb (' FF993300 ');
    3. $objPHPExcel->getactivesheet ()->getstyle (' D13 ')->getborders ()->getbottom ()->getcolor () Setargb (' FF993300 ');
    4. $objPHPExcel->getactivesheet ()->getstyle (' E13 ')->getborders ()->gettop ()->getcolor ()->setargb (' FF993300 ');
    5. $objPHPExcel->getactivesheet ()->getstyle (' E13 ')->getborders ()->getbottom ()->getcolor () Setargb (' FF993300 ');
    6. $objPHPExcel->getactivesheet ()->getstyle (' E13 ')->getborders ()->getright ()->getcolor () Setargb (' FF993300 ');
Copy Code

Set Fill Color

    1. $objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
    2. $objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getfill ()->getstartcolor ()->setargb (' FF808080 ') ;
    3. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
    4. $objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfill ()->getstartcolor ()->setargb (' FF808080 ') ;
Copy Code

Add picture

    1. $objDrawing = new phpexcel_worksheet_drawing ();

    2. $objDrawing->setname (' Logo ');
    3. $objDrawing->setdescription (' Logo ');
    4. $objDrawing->setpath ('./images/officelogo.jpg ');
    5. $objDrawing->setheight (36);
    6. $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());

    7. $objDrawing = new phpexcel_worksheet_drawing ();

    8. $objDrawing->setname (' Paid ');
    9. $objDrawing->setdescription (' Paid ');
    10. $objDrawing->setpath ('./images/paid.png ');
    11. $objDrawing->setcoordinates (' B15 ');
    12. $objDrawing->setoffsetx (110);
    13. $objDrawing->setrotation (25);
    14. $objDrawing->getshadow ()->setvisible (true);
    15. $objDrawing->getshadow ()->setdirection (45);
    16. $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());

Copy Code

Processing the Chinese output problem requires the conversion of the string to UTF-8 encoding to normal output, otherwise the Chinese characters will be output blank, the following processing: $STR = Iconv (' gb2312 ', ' utf-8 ', $str); or you can write a function that specializes in Chinese strings:

    1. function ConvertUTF8 ($STR)
    2. {
    3. if (empty ($STR)) return ';
    4. Return Iconv (' gb2312 ', ' utf-8 ', $str);
    5. }
Copy Code

Output data processing from the database

Read data from a database such as:

  1. $db = new Mysql ($dbconfig);

  2. $sql = "SELECT * from table name";
  3. $row = $db->getall ($sql); $row as a two-dimensional array

  4. $count = count ($row);

  5. for ($i = 2; $i <= $count +1; $i + +) {
  6. $objPHPExcel->getactivesheet ()->setcellvalue (' A '. $i, ConvertUTF8 ($row [$i -2][1]);
  7. $objPHPExcel->getactivesheet ()->setcellvalue (' B '. $i, ConvertUTF8 ($row [$i -2][2]);
  8. $objPHPExcel->getactivesheet ()->setcellvalue (' C '. $i, ConvertUTF8 ($row [$i -2][3]);
  9. $objPHPExcel->getactivesheet ()->setcellvalue (' D '. $i, ConvertUTF8 ($row [$i -2][4]);
  10. $objPHPExcel->getactivesheet ()->setcellvalue (' E '. $i, convertUTF8 (Date ("y-m-d", $row [$i -2][5]));
  11. $objPHPExcel->getactivesheet ()->setcellvalue (' F '. $i, ConvertUTF8 ($row [$i -2][6]);

  12. $objPHPExcel->getactivesheet ()->setcellvalue (' G '. $i, ConvertUTF8 ($row [$i -2][7]);

  13. $objPHPExcel->getactivesheet ()->setcellvalue (' H '. $i, ConvertUTF8 ($row [$i -2][8]);
  14. }

Copy Code

After the default sheet, create a worksheet here to insert a paragraph, before the scripting School also introduced a few articles about Phpexcel, Interested friends can look at: use Phpexcel to discriminate and format the date format in Excel example Phpexcel export Excel colors inconsistent with colors in the Web page resolution CI uses PHPEXCEL to export data to Excel

    1. echo Date (' H:i:s '). "Create new Worksheet object\n";

    2. $objPHPExcel->createsheet ();

    3. $objWriter = Phpexcel_iofactory::createwriter ($objExcel, ' Excel5 ');

    4. $objWriter-save (' php://output ');

Copy Code

Attached, several common methods of use.

  1. Include ' phpexcel.php ';

  2. Include ' phpexcel/writer/excel2007.php ';
  3. Or include ' phpexcel/writer/excel5.php '; For the output. xls
  4. Create an Excel
  5. $objPHPExcel = new Phpexcel ();

  6. Save excel-2007 Format

  7. $objWriter = new phpexcel_writer_excel2007 ($objPHPExcel);
  8. or $objwriter = new Phpexcel_writer_excel5 ($objPHPExcel); Non-2007 format
  9. $objWriter->save ("xxx.xlsx");

  10. Direct output to Browser

  11. $objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);
  12. Header ("Pragma:public");
  13. Header ("Expires:0″");
  14. Header ("Cache-control:must-revalidate, post-check=0, Pre-check=0″);
  15. Header ("Content-type:application/force-download");
  16. Header ("Content-type:application/vnd.ms-execl");
  17. Header ("Content-type:application/octet-stream");
  18. Header ("Content-type:application/download");;
  19. Header (' content-disposition:attachment;filename= ' Resume.xls "');
  20. Header ("Content-transfer-encoding:binary");
  21. $objWriter->save (' php://output ');
  22. ?>

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