>백엔드 개발 >PHP 튜토리얼 >phpexcel 클래스 라이브러리의 기본 사용법, phpexcel 입문 예제의 전체 모음

phpexcel 클래스 라이브러리의 기본 사용법, phpexcel 입문 예제의 전체 모음

WBOY
WBOY원래의
2016-07-25 08:51:36967검색
  1. //写入excel文件

  2. //클래스 포함
  3. require_once('클래스/ PHPExcel.php');
  4. require_once('Classes/PHPExcel/Writer/Excel2007.php');
  5. $objPHPExcel = new PHPExcel();

  6. //Set 속성 设置文件属性

  7. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  8. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  9. $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX 테스트 문서");
  10. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX 테스트 문서");
  11. $objPHPExcel->getProperties()->setDescription("PHP 클래스를 사용하여 생성된 Office 2007 XLSX용 테스트 문서.");
  12. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  13. $objPHPExcel->getProperties()->setCategory("테스트 결과 파일");

  14. //데이터 추가 添加数据

  15. $ objPHPExcel->setActiveSheetIndex(0);
  16. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');//可以指정位置
  17. $objPHPExcel->getActiveSheet() ->setCellValue('A2', true);
  18. $objPHPExcel->getActiveSheet()->setCellValue('A3', false);
  19. $objPHPExcel->getActiveSheet()->setCellValue ('B2', '세계!');
  20. $objPHPExcel->getActiveSheet()->setCellValue('B3', 2);
  21. $objPHPExcel->getActiveSheet()->setCellValue( 'C1', 'Hello');
  22. $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');

  23. //循环

  24. for($i = 1;$i<200;$i ) {
  25. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i);
  26. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, '테스트 값');
  27. }
  28. //日期格式化

  29. $objPHPExcel->getActiveSheet()->setCellValue('D1', time());
  30. $objPHPExcel-> getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);

  31. //댓글 추가 添加注释

  32. $objPHPExcel->getActiveSheet()->getComment('E11')->setAuthor('PHPExcel');
  33. $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11') ->getText()->createTextRun('PHPExcel:');
  34. $objCommentRichText->getFont()->setBold(true);
  35. $objPHPExcel->getActiveSheet()-> getComment('E11')->getText()->createTextRun("rn");
  36. $objPHPExcel->getActiveSheet()->getComment('E11')->getText()-> ;createTextRun('VAT를 제외한 현재 청구서의 총액.');

  37. //서식 있는 텍스트 문자열 추가 添加文字 可设置样式

  38. $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18') );
  39. $objRichText->createText('이 송장은 ');
  40. $objPayable = $objRichText->createTextRun(' 월말 이후 30일 이내에 지급');
  41. $objPayable->getFont()->setBold(true);
  42. $objPayable->getFont()->setItalic(true) ;
  43. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
  44. $objRichText->createText(', 송장에 달리 지정되지 않는 한.');< ;/p>
  45. //셀 병합 合并分离单元格

  46. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
  47. $objPHPExcel-> getActiveSheet()->unmergeCells('A18:E22');

  48. //셀 보호 保护单元格

  49. $objPHPExcel->getActiveSheet()->getProtection( )->setSheet(true);//워크시트 보호를 활성화하려면 true로 설정해야 합니다!
  50. $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel' );

  51. //셀 번호 형식 설정 数字格式化

  52. $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()- >setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  53. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ) ;

  54. //열 너비 설정 设置列宽titude

  55. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  56. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);

  57. //글꼴 설정 设置字体

  58. $objPHPExcel ->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
  59. $objPHPExcel->getActiveSheet()->getStyle('B1' )->getFont()->setSize(20);
  60. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  61. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  62. $objPHPExcel->getActiveSheet()-> getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);

  63. //정렬 설정 设置对齐

  64. $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  65. $objPHPExcel->getActiveSheet()->getStyle( 'A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  66. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()-> setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  67. $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setWrapText(true);

  68. //열 테두리 설정 设置列边框

  69. $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border ::BORDER_THIN);
  70. $objPHPExcel->getActiveSheet()->getStyle('A10')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  71. $objPHPExcel->getActiveSheet()->getStyle('E10')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  72. $objPHPExcel->getActiveSheet ()->getStyle('D13')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  73. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);

  74. < p>//테두리 색상 설정 设置边框颜color
  75. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()-> ;setARGB('FF993300');
  76. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB ('FF993300');
  77. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB(' FF993300');
  78. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300' );

  79. //채우기 설정 设置填充

  80. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType (PHPExcel_Style_Fill::FILL_SOLID);
  81. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');< ;/p>
  82. //시트에 하이퍼링크 추가 添加链接

  83. $objPHPExcel->getActiveSheet()->setCellValue('E26', 'www.phpexcel.net');
  84. $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setUrl('http://www.phpexcel.net');
  85. $objPHPExcel- >getActiveSheet()->getCell('E26')->getHyperlink()->setTooltip('웹사이트로 이동');
  86. $objPHPExcel->getActiveSheet()->getStyle('E26 ')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

  87. //워크시트에 그림 추가 添加图文

  88. $obj드로잉 = new PHPExcel_Worksheet_드로잉( );
  89. $obj드로잉->setName('로고');
  90. $obj드로잉->setDescription('로고');
  91. $obj드로잉->setPath('./images/officelogo.jpg ');
  92. $obj드로잉->setHeight(36);
  93. $obj드로잉->setCoordinates('B15');
  94. $obj드로잉->setOffsetX(110);
  95. $obj드로잉- >setRotation(25);
  96. $obj드로잉->getShadow()->setVisible(true);
  97. $obj드로잉->getShadow()->setDirection(45);
  98. $obj드로잉 ->setWorksheet($objPHPExcel->getActiveSheet());

  99. //행과 열을 삽입하고 제거해 보세요

  100. $objPHPExcel->getActiveSheet()- >insertNewRowBefore(6, 10);
  101. $objPHPExcel->getActiveSheet()->removeRow(6, 10);
  102. $objPHPExcel->getActiveSheet()->insertNewColumnBefore('E', 5);
  103. $objPHPExcel->getActiveSheet()->removeColumn('E', 5);

  104. //조건부 서식 추가

  105. $objConditional1 = new PHPExcel_Style_Conditional();
  106. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
  107. $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
  108. $objConditional1->setCondition('0' ) ;
  109. $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
  110. $objConditional1->getStyle()->getFont ()->setBold(true);

  111. //자동 필터 설정 自动过滤

  112. $objPHPExcel->getActiveSheet()->setAutoFilter('A1:C9') ;

  113. //"전화" 및 "팩스" 열 숨기기 隐藏列

  114. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setVisible (false);
  115. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);

  116. //문서 보안 설정 设置文档안전

  117. $objPHPExcel->getSecurity()->setLockWindows(true);
  118. $objPHPExcel->getSecurity()->setLockStructure(true);
  119. $objPHPExcel->getSecurity( )->setWorkbookPassword("PHPExcel");

  120. //시트 보안 설정 设置工작업表안전

  121. $objPHPExcel->getActiveSheet()->getProtection()- >setPassword('PHPExcel');
  122. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// 다음 중 하나를 활성화하려면 이 기능을 활성화해야 합니다!
  123. $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
  124. $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
  125. $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);

  126. //계산된 데이터 计算

  127. echo 'B14 값 [ =횟수(B2:B12)]: ' . $objPHPExcel->getActiveSheet()->getCell('B14')->getCalculatedValue() . "rn";

  128. //개요 수준 설정

  129. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
  130. $objPHPExcel->getActiveSheet()-> ;getColumnDimension('E')->setVisible(false);
  131. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setCollapsed(true);

  132. //창 고정

  133. $objPHPExcel->getActiveSheet()->freezePane('A2');

  134. //상단에서 반복할 행

  135. $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);

  136. //데이터 유효성 검사 설정 验证输入值

  137. $ objValidation = $objPHPExcel->getActiveSheet()->getCell('B3')->getDataValidation();
  138. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
  139. $objValidation-> setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
  140. $objValidation->setAllowBlank(true);
  141. $objValidation->setShowInputMessage(true);
  142. $objValidation->setShowErrorMessage(true);
  143. $objValidation->setErrorTitle('입력 오류');
  144. $objValidation->setError('번호가 허용되지 않습니다!');
  145. $objValidation->setPromptTitle('허용된 입력');
  146. $objValidation->setPrompt('10에서 20 사이의 숫자만 허용됩니다.');
  147. $objValidation->setFormula1(10);
  148. $objValidation->setFormula2(20);
  149. $objPHPExcel->getActiveSheet()->getCell('B3')->setDataValidation($objValidation);

  150. //기본 시트 뒤에 새 워크시트를 만듭니다. 创建새로운 작업

  151. $objPHPExcel->createSheet();
  152. $objPHPExcel->setActiveSheetIndex(1);

  153. //머리글과 바닥글을 설정합니다. 홀수/짝수에 대해 다른 헤더가 사용되지 않는 경우 홀수 헤더가 가정됩니다. 页眉页脚

  154. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&H이 문서를 기밀로 처리해 주십시오!');
  155. $objPHPExcel->getActiveSheet()-> ;getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

  156. // 페이지 방향 및 크기 설정 方向大小

  157. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
  158. $objPHPExcel->getActiveSheet()->getPageSetup ()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

  159. //시트 이름 바꾸기 큼직한 작업 'Simple');

  160. //활성 시트 인덱스를 첫 번째 시트로 설정하여 Excel에서 이를 첫 번째 시트로 엽니다

  161. $objPHPExcel->setActiveSheetIndex(0);< ;/p>
  162. //Excel 2007 파일 저장 保存

  163. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  164. $objWriter->save(str_replace('.php', '. xlsx', __FILE__));
  165. //Excel 5 파일 저장 保存
  166. require_once('Classes/PHPExcel/Writer/Excel5.php');
  167. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel) ;
  168. $objWriter->save(str_replace('.php', '.xls', __FILE__));

  169. //1.6.2신규版保存

  170. require_once ('Classes/PHPExcel/IOFactory.php');
  171. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  172. $objWriter->save(str_replace('.php', '. xls', __FILE__));
  173. 读excel

  174. //클래스 포함

  175. require_once('Classes/PHPExcel/Reader/Excel2007.php');
  176. $objReader = new PHPExcel_Reader_Excel2007;
  177. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  178. 读写csv

  179. require_once("05featuredemo.inc.php" );

  180. require_once('Classes/PHPExcel/Writer/CSV.php');
  181. require_once('Classes/PHPExcel/Reader/CSV.php');
  182. require_once('Classes/PHPExcel/Writer/ Excel2007.php');
  183. //CSV 형식으로 쓰기 写
  184. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  185. $objWriter->setDelimiter(';');
  186. $objWriter- >setEnclosure('');
  187. $objWriter->setLineEnding("rn");
  188. $objWriter->setSheetIndex(0);
  189. $objWriter->save(str_replace('. php', '.csv', __FILE__));
  190. //CSV 형식에서 읽기 读
  191. $objReader = new PHPExcel_Reader_CSV();
  192. $objReader->setDelimiter(';');
  193. $objReader->setEnclosure('');
  194. $objReader->setLineEnding("rn");
  195. $objReader->setSheetIndex(0);
  196. $objPHPExcelFromCSV = $objReader-> ;load(str_replace('.php', '.csv', __FILE__));
  197. //Excel2007 형식으로 쓰기
  198. $objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
  199. $objWriter2007->save (str_replace('.php', '.xlsx', __FILE__));
  200. 写html

  201. require_once("05featuredemo.inc.php");

  202. require_once('Classes/PHPExcel/Writer/HTML.php');
  203. //HTML 형식으로 쓰기
  204. $objWriter = new PHPExcel_Writer_HTML ($objPHPExcel);
  205. $objWriter->setSheetIndex(0);
  206. $objWriter->save(str_replace('.php', '.htm', __FILE__));
  207. 写pdf< /p>
  208. require_once("05featuredemo.inc.php");

  209. require_once('Classes/PHPExcel/IOFactory.php');
  210. //PDF 형식으로 쓰기
  211. $ objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
  212. $objWriter->setSheetIndex(0);
  213. $objWriter->save(str_replace('.php', '.pdf', __FILE__));
  214. //에코 메모리 최대 사용량
  215. echo date('H:i:s') . " 최대 메모리 사용량: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MBrn";

复代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.