>  기사  >  백엔드 개발  >  Excel을 내보내고 하이퍼링크를 추가하는 phpExcel의 예제 코드

Excel을 내보내고 하이퍼링크를 추가하는 phpExcel의 예제 코드

WBOY
WBOY원래의
2016-07-25 08:55:231799검색
  1. //写excel

  2. //수업 포함

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

  6. /**

  7. * phpExcel 내보내기 엑셀
  8. * by bbs.it-home.org
  9. */

  10. //속성 설정 设置文件属性

  11. $objPHPExcel->getProperties()->setCreator("마르텐 Balliauw");
  12. $objPHPExcel->getProperties()->setLastModifiedBy("마틴 Balliauw");
  13. $objPHPExcel->getProperties()->setTitle(“Office 2007 XLSX 테스트 문서”);
  14. $objPHPExcel->getProperties()->setSubject(“Office 2007 XLSX 테스트 문서”);
  15. $objPHPExcel->getProperties()->setDescription(“PHP 클래스를 사용하여 생성된 Office 2007 XLSX용 테스트 문서.”);
  16. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  17. $objPHPExcel->getProperties()->setCategory(“테스트 결과 파일”);

  18. //일부 데이터 추가 添加数据

  19. $objPHPExcel-> setActiveSheetIndex(0);
  20. $objPHPExcel->getActiveSheet()->setCellValue('A1′, 'Hello');//可以指정位置
  21. $objPHPExcel->getActiveSheet()->setCellValue('A2′, 진실);
  22. $objPHPExcel->getActiveSheet()->setCellValue('A3', false);
  23. $objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!');
  24. $objPHPExcel->getActiveSheet()->setCellValue('B3', 2);
  25. $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello');
  26. $objPHPExcel->getActiveSheet()->setCellValue('D2′, 'world!');
  27. //循环
  28. for($i = 1;$i<200;$i ) {
  29. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i );
  30. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, '테스트 값');
  31. }
  32. //日期格式化
  33. $objPHPExcel->getActiveSheet()->setCellValue('D1′, time());
  34. $objPHPExcel->getActiveSheet()->getStyle('D1′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);

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

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

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

  42. $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18′) );
  43. $objRichText->createText('이 청구서는 ');
  44. $objPayable = $objRichText->createTextRun('월말 이후 30일 이내에 지급 가능');
  45. $objPayable->getFont()->setBold(true);
  46. $objPayable->getFont()->setItalic(true);
  47. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
  48. $objRichText->createText(', 인보이스에 달리 지정되지 않는 한.');

  49. //셀 병합 合并分离单元格

  50. $objPHPExcel-> getActiveSheet()->mergeCells('A18:E22');
  51. $objPHPExcel->getActiveSheet()->unmergeCells('A18:E22′);

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

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

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

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

  58. < ;p>//열 너비 설정 设置列宽degree
  59. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  60. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);

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

  62. $ objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
  63. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
  64. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  65. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  66. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);

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

  68. $objPHPExcel->getActiveSheet()->getStyle('D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  69. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  70. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  71. $objPHPExcel->getActiveSheet()->getStyle('A3′)->getAlignment()->setWrapText(true);

  72. //열 설정 borders 设置列边框

  73. $objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  74. $objPHPExcel->getActiveSheet()->getStyle('A10')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  75. $objPHPExcel->getActiveSheet()->getStyle('E10')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  76. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  77. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);

  78. //테두리 색상 설정 设置边框颜color

  79. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getLeft()->getColor( )->setARGB('FF993300');
  80. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
  81. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
  82. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor()->setARGB('FF993300′);< /p>
  83. //채우기 설정 设置填充

  84. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->setFillType(PHPExcel_Style_Fill:: FILL_SOLID);
  85. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′);

  86. //시트에 하이퍼링크 추가 添加链接

  87. $objPHPExcel->getActiveSheet()->setCellValue('E26′, 'www.phpexcel.net');
  88. $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setUrl('http://www.phpexcel.net');
  89. $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setTooltip('웹사이트로 이동');
  90. $objPHPExcel->getActiveSheet()->getStyle('E26′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

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

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

  103. //행과 열 삽입 및 제거 실험

  104. $objPHPExcel-> ;getActiveSheet()->insertNewRowBefore(6, 10);
  105. $objPHPExcel->getActiveSheet()->removeRow(6, 10);
  106. $objPHPExcel->getActiveSheet()->insertNewColumnBefore('E', 5);
  107. $objPHPExcel->getActiveSheet()->removeColumn('E', 5);

  108. //조건부 서식 추가

  109. $objConditional1 = new PHPExcel_Style_Conditional() ;
  110. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
  111. $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
  112. $objConditional1->setCondition('0′);
  113. $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
  114. $objConditional1->getStyle()->getFont()->setBold(true);

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

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

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

  118. $objPHPExcel->getActiveSheet ()->getColumnDimension('C')->setVisible(false);
  119. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);

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

  121. $objPHPExcel->getSecurity()->setLockWindows(true);
  122. $objPHPExcel->getSecurity()->setLockStructure(true);
  123. $objPHPExcel->getSecurity()->setWorkbookPassword(“PHPExcel”);

  124. //시트 보안 설정 设置工作表안전

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

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

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

  132. //개요 수준 설정

  133. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
  134. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setVisible(false);
  135. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setCollapsed(true);

  136. //창 고정

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

  138. //맨 위에서 반복할 행

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

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

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

  154. //다음에 새 워크시트를 만듭니다. 기본 시트 创建新的工작품标签

  155. $objPHPExcel->createSheet(); bbs.it-home.org
  156. $objPHPExcel->setActiveSheetIndex(1);

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

  158. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&H이 문서를 기밀로 처리해주세요!');
  159. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');< ;/p>
  160. //페이지 방향 및 크기 설정 方向大小

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

  163. //시트 이름 바꾸기 重命name工作表标签

  164. $objPHPExcel->getActiveSheet()->setTitle('Simple');

  165. //활성 시트 색인을 첫 번째 시트로 설정하여 Excel에서 이를 첫 번째 시트로 엽니다. sheet

  166. $objPHPExcel->setActiveSheetIndex(0);

  167. //Excel 2007 파일 저장 保存

  168. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  169. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));

  170. //Excel 5 파일 저장 保存

  171. require_once( '클래스/PHPExcel/Writer/Excel5.php');
  172. $objWriter = 새로운 PHPExcel_Writer_Excel5($objPHPExcel);
  173. $objWriter->save(str_replace('.php', '.xls', __FILE__));

  174. //1.6.2신규版保存

  175. require_once( '클래스/PHPExcel/IOFactory.php');
  176. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007′);
  177. $objWriter->save(str_replace('.php', '.xls', __FILE__));

  178. //读excel

  179. //클래스 포함
  180. require_once('클래스/PHPExcel/Reader/Excel2007.php');
  181. $objReader = 새로운 PHPExcel_Reader_Excel2007;

  182. $objPHPExcel = $objReader->load(“05featuredemo.xlsx”);

  183. //读写csv

  184. require_once(“05featuredemo.inc.php”);
  185. require_once('클래스/PHPExcel/Writer/CSV.php');
  186. require_once('클래스/PHPExcel/Reader/CSV.php');
  187. require_once('Classes/PHPExcel/Writer/Excel2007.php');

  188. //CSV 형식으로 쓰기 写

  189. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  190. $objWriter->setDelimiter(';');
  191. $objWriter->setEnclosure(”);
  192. $objWriter->setLineEnding(“rn”);
  193. $objWriter->setSheetIndex(0);
  194. $objWriter->save(str_replace('.php', '.csv', __FILE__));

  195. //CSV 형식에서 읽기 读

  196. $objReader = new PHPExcel_Reader_CSV ();
  197. $objReader->setDelimiter(';');
  198. $objReader->setEnclosure(”);
  199. $objReader->setLineEnding(“rn”);
  200. $objReader->setSheetIndex(0);
  201. $objPHPExcelFromCSV = $objReader->load(str_replace('.php', '.csv', __FILE__));

  202. //Excel2007 형식으로 쓰기

  203. $objWriter2007 = 새로운 PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
  204. $objWriter2007->save(str_replace('.php', '.xlsx', __FILE__));

  205. //写html

  206. require_once(“05featuredemo. inc.php”);
  207. require_once('Classes/PHPExcel/Writer/HTML.php');

  208. //HTML 형식으로 쓰기

  209. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
  210. $objWriter->setSheetIndex(0);
  211. $objWriter->save(str_replace('.php', '.htm', __FILE__));

  212. //写pdf

  213. require_once(“05featuredemo. inc.php”);
  214. require_once('Classes/PHPExcel/IOFactory.php');

  215. //PDF 형식으로 쓰기

  216. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF ');
  217. $objWriter->setSheetIndex(0);
  218. $objWriter->save(str_replace('.php', '.pdf', __FILE__));
  219. //에코 메모리 최대 사용량
  220. echo date('H:i:s') . ” 최대 메모리 사용량: ” . (memory_get_peak_usage(true) / 1024 / 1024) . ” MBrn”;

复代码


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