项目需要读取Excel的内容,从百度搜索了下,主要有两个选择,第一个是PHPExcelReader,另外一个是PHPExcel。
require_once '/libs/PHPExcel-1.8.0/Classes/PHPExcel.php'; //修改为自己的目录 echo '<p>TEST PHPExcel 1.8.0: read xlsx file</p>'; $objReader = PHPExcel_IOFactory::createReaderForFile($filename); $objPHPExcel = $objReader->load($filename); $objPHPExcel->setActiveSheetIndex(1); $date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();
<span style="color: #008080;"> 1</span> <table id="table_id"> <span style="color: #008080;"> 2</span> <?<span style="color: #000000;">php </span><span style="color: #008080;"> 3</span> <span style="color: #800080;">$objWorksheet</span> = <span style="color: #800080;">$objPHPExcel</span>-><span style="color: #000000;">getActiveSheet(); </span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$i</span> = 0<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$objWorksheet</span>->getRowIterator() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$row</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 6</span> ?> <span style="color: #008080;"> 7</span> <tr> <span style="color: #008080;"> 8</span> <?<span style="color: #000000;">php </span><span style="color: #008080;"> 9</span> <span style="color: #800080;">$cellIterator</span> = <span style="color: #800080;">$row</span>-><span style="color: #000000;">getCellIterator(); </span><span style="color: #008080;">10</span> <span style="color: #800080;">$cellIterator</span>->setIterateOnlyExistingCells(<span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$i</span> == 0<span style="color: #000000;"> ){ </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">echo</span> '<thead>'<span style="color: #000000;">; </span><span style="color: #008080;">14</span> <span style="color: #000000;"> } </span><span style="color: #008080;">15</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$cellIterator</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$cell</span><span style="color: #000000;">){ </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #0000ff;">echo</span> '<td>' . <span style="color: #800080;">$cell</span>->getValue() . '</td>'<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$i</span> == 0<span style="color: #000000;"> ){ </span><span style="color: #008080;">21</span> <span style="color: #0000ff;">echo</span> '</thead>'<span style="color: #000000;">; </span><span style="color: #008080;">22</span> <span style="color: #000000;"> } </span><span style="color: #008080;">23</span> <span style="color: #800080;">$i</span>++<span style="color: #000000;">; </span><span style="color: #008080;">24</span> ?> <span style="color: #008080;">25</span> </tr> <span style="color: #008080;">26</span> <?<span style="color: #000000;">php </span><span style="color: #008080;">27</span> <span style="color: #000000;"> } </span><span style="color: #008080;">28</span> ?> <span style="color: #008080;">29</span> </table>