Using the PHPExcel library

WBOY
Release: 2016-07-25 08:42:24
Original
897 people have browsed it

Please introduce the PHPExcel library before use

  1. /**
  2. * Excel writing, based on PHPExcel library
  3. * @param array $data
  4. * @param $file
  5. * @return bool
  6. * @throws PHPExcel_Exception
  7. *
  8. */
  9. function excel_insert(array $data,$file){
  10. // $data = [
  11. // 'Warehouse'=>[
  12. // ['Warehouse number','Warehouse noun',1],
  13. // ['Warehouse number','Warehouse noun',1],
  14. // ['Warehouse number','Warehouse noun',1] ,
  15. // ['Warehouse number','Warehouse noun',1],
  16. // ['Warehouse number','Warehouse noun',1],
  17. // ],
  18. // 'Warehouse 2'=> [
  19. // ['Warehouse number','Warehouse noun',1],
  20. // ['Warehouse number','Warehouse noun',1],
  21. // ['Warehouse number','Warehouse noun',1 ],
  22. // ['Warehouse number','Warehouse noun',1],
  23. // ['Warehouse number','Warehouse noun',1],
  24. // ],
  25. // ];
  26. // excel_insert ($data,'s.xlsx');
  27. if(!$data||!$file){
  28. return false;
  29. }
  30. $sheet_id = 0;
  31. //Create excel operation object
  32. $objPHPExcel = new PHPExcel();
  33. //Get the file property object and provide setting resources for the following
  34. $objPHPExcel->getProperties()->setCreator("Mianyang Carbon Cloud Information Technology Co., Ltd.")
  35. ->setLastModifiedBy( "Mianyang Carbon Cloud Information Technology Co., Ltd.")
  36. ->setTitle("Input_Goods_message")
  37. ->setSubject("Topic 1")
  38. ->setDescription("Any one described")
  39. -> ;setKeywords("Keywords separated by spaces")
  40. ->setCategory("Category");
  41. for($i=1;$i $objPHPExcel->addSheet (new PHPExcel_Worksheet($objPHPExcel,'sheet'.$i));
  42. }
  43. foreach($data as $sheetName => $sheetData){
  44. $Sheet = $objPHPExcel->setActiveSheetIndex($sheet_id);
  45. $ Sheet->setTitle($sheetName);
  46. $insert_id = 1;
  47. foreach($sheetData as $rowData){
  48. if(is_array($rowData)&&$rowData){
  49. foreach($rowData as $id => $cellData){
  50. if(is_numeric($id)&&(is_string($cellData)||is_numeric($cellData))){
  51. $Sheet->setCellValue(chr(65+$id).$insert_id,$cellData );
  52. }else{
  53. return false;
  54. }
  55. }
  56. $insert_id++;
  57. }else{
  58. return false;
  59. }
  60. }
  61. $sheet_id++;
  62. }
  63. try{
  64. $objWriter = PHPExcel_IOFactory::createWriter($objPH PExcel , 'Excel2007');
  65. $objWriter->save($file);
  66. }catch (Exception $e){
  67. return false;
  68. }
  69. }
Copy code

Usage:
  1. $data = [
  2. 'Warehouse'=>[
  3. ['Warehouse number','Warehouse noun',1],
  4. ['Warehouse number','Warehouse noun',1],
  5. ['Warehouse number','Warehouse noun',1],
  6. ['Warehouse number','Warehouse noun',1],
  7. ['Warehouse number','Warehouse noun',1],
  8. ],
  9. ' Warehouse 2'=>[
  10. ['Warehouse number','Warehouse noun',1],
  11. ['Warehouse number','Warehouse noun',1],
  12. ['Warehouse number','Warehouse noun',1 ],
  13. ['Warehouse number','Warehouse noun',1],
  14. ['Warehouse number','Warehouse noun',1],
  15. ],
  16. ];
  17. excel_insert($data,'s.xlsx') ;
Copy code

PHPExcel


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template