Đọc ghi dữ liệu file excel sử dụng PHPExcel
Trong bài trước mình có giới thiệu cho các bạn cách xuất dữ liệu ra file excel 1 cách đơn giản nhất,trong bài này mình sẽ giới thiệu cho các bạn thư viện PHPExcel,đây là 1 thư viện cho phép chúng ta làm việc đọc ghi dữ liệu từ file excel 1 cách đơn giản và tốt nhất.
– Trước tiên các bạn cần vào đây để download thư viện này về làm việc
– Trước tiên các bạn cần vào đây để download thư viện này về làm việc
Ghi dữ liệu vào file excel – Export excel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| //luu cac thong tin vao file excelrequire_once 'PHPExcel/PHPExcel.php';$objPHPExcel = new PHPExcel();$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'Tên')->setCellValue('B1', 'Email')->setCellValue('C1', 'Số điện thoại');$lists = array(array('name' => 'Nobita','email' => 'nobitacnt@gmail.com','phone' => '0123.456.789',),array('name' => 'Xuka','email' => 'xuka@gmail.com','phone' => '0222.333.444',),array('name' => 'Chaien','email' => 'chaien@gmail.com','phone' => '0111.333.444',),);//set gia tri cho cac cot du lieu$i = 2;foreach ($lists as $row){$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $row['name'])->setCellValue('B'.$i, $row['email'])->setCellValue('C'.$i, $row['phone']);$i++;}//ghi du lieu vao file,định dạng file excel 2007$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$full_path = 'data.xlsx';//duong dan file$objWriter->save($full_path); |
– Với đoạn code trên các bạn cần chú ý tới phương thức
setCellValue nó cho phép chúng ta gắn dữ liệu vào các cột tương ứng,tham số đầu là vị trí cột,tham số 2 là dữ liệu
Sau khi chạy file này chúng ta sẽ tạo file
data.xlsx như sau:Đọc và hiển thị dữ liệu từ file excel
– Trong ví dụ này chúng ta sẽ sử dụng luôn file
data.xlsx đã tạo ở trên nhé,ở đây mình sẽ tạo file read.php với nội dung sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| <?phprequire_once 'PHPExcel/PHPExcel.php';$filename = 'data.xlsx';$inputFileType = PHPExcel_IOFactory::identify($filename);$objReader = PHPExcel_IOFactory::createReader($inputFileType);$objReader->setReadDataOnly(true);/** Load $inputFileName to a PHPExcel Object **/$objPHPExcel = $objReader->load("$filename");$total_sheets=$objPHPExcel->getSheetCount();$allSheetName=$objPHPExcel->getSheetNames();$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);$highestRow = $objWorksheet->getHighestRow();$highestColumn = $objWorksheet->getHighestColumn();$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);$arraydata = array();for ($row = 2; $row <= $highestRow;++$row){ for ($col = 0; $col <$highestColumnIndex;++$col) { $value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); $arraydata[$row-2][$col]=$value; }}echo '<pre>';print_r($arraydata);echo '</pre>'; |
Và khi chạy file này sẽ cho kết quả như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| Array( [0] => Array ( [0] => Nobita [1] => nobitacnt@gmail.com [2] => 0123.456.789 ) [1] => Array ( [0] => Xuka [1] => xuka@gmail.com [2] => 0222.333.444 ) [2] => Array ( [0] => Chaien [1] => chaien@gmail.com [2] => 0111.333.444 )) |
Tổng kết: Qua bài này mình đã giới thiệu cho các bạn cách làm việc đọc ghi dữ liệu với file excel sử dụng thư viện PHPExcel 1 cách đơn giản,để tìm hiểu thêm về các phương thức khác các bạn có thể truy cập https://phpexcel.codeplex.com/ nhé,các bạn có thể tải code demo trong bài viết này tại đây,chúc các bạn áp dụng tốt với dự án của mình
Tải mã nguồn bài viết: http://ouo.io/hKG9Mj
- See more at: http://hocphp.info/doc-ghi-du-lieu-file-excel-su-dung-phpexcel/#sthash.xoCzin9t.dpuf
Tải mã nguồn bài viết: http://ouo.io/hKG9Mj
Comments
Post a Comment