PHP đọc từng hàng tệp Excel

Lớp này hữu ích cho kích thước tệp excel vừa và nhỏ (vài MB), vì nó đọc toàn bộ bảng tính cùng một lúc và nếu bạn có một bảng tính lớn, bộ nhớ sẽ cạn kiệt. Trong trường hợp này, tốt hơn là chuyển đổi dữ liệu excel sang định dạng CSV, sau đó sử dụng nó trong PHP

Lớp PhpExcelReader lưu trữ dữ liệu tệp excel thành một mảng nhiều chiều, trong thuộc tính $sheets, được truy cập bằng.
$objectClass->sheets
- $objectClass là thể hiện đối tượng của lớp

Ví dụ như bảng excel này

PHP đọc từng hàng tệp Excel

Để đọc tệp excel này bằng lớp PhpExcelReader, hãy sử dụng mã này

____1'; .
array (
 0 => array (
 'maxrow' => 0,
 'maxcol' => 0,
 'numRows' => 8,
 'numCols' => 4,
 'cells' => array (
 2 => array ( 2 => 'Web sites data' ),
 4 => array (
 1 => 'Title',
 2 => 'Url',
 3 => 'Visitors',
 4 => 'Accesses'
 ),
 5 => array (
 1 => 'Web Programming Courses',
 2 => 'https://coursesweb.net/',
 3 => '5000',
 4 => '9800'
 ),
 6 => array (
 1 => 'Courses Games and Anime',
 2 => 'https://marplo.net/',
 3 => '6000',
 4 => '22000'
 ),
 7 => array (
 1 => 'PHP: Hypertext Processor',
 2 => 'http://php.net/',
 3 => '30000',
 4 => '92000'
 ),
 8 => array (
 1 => 'Yahoo!',
 2 => 'http://yahoo.com/',
 3 => '100000',
 4 => '650000'
 ),
 ),
 'cellsInfo' => array (
 5 => array (
 3 => array (
 'raw' => 5000,
 'type' => 'unknown'
 ),
 4 => array (
 'raw' => 9800,
 'type' => 'unknown'
 ),
 ),
 6 => array (
 3 => array (
 'raw' => 6000,
 'type' => 'unknown'
 ),
 4 => array (
 'raw' => 22000,
 'type' => 'unknown'
 ),
 ),
 7 => array (
 3 => array (
 'raw' => 30000,
 'type' => 'unknown'
 ),
 4 => array (
 'raw' => 92000,
 'type' => 'unknown'
 ),
 ),
 8 => array (
 3 => array (
 'raw' => 100000,
 'type' => 'unknown'
 ),
 4 => array (
 'raw' => 650000,
 'type' => 'unknown'
 ),
 ),
 2 => array (
 2 => array ( 'colspan' => 3 ),
 ),
 ),
 )
)
- $excel->sheets[0]['numRows'] chứa số hàng có dữ liệu trong trang tính đầu tiên.
- $excel->sheets[0]['numCols'] chứa số cột có dữ liệu trong sheet đầu tiên.
- $excel->sheets[0] chứa mảng nhiều chiều với dữ liệu của sheet đầu tiên trong file excel. Dữ liệu được lưu trữ trong 'ô' và siêu dữ liệu được lưu trữ trong một mảng có tên là 'cellsInfo'.
$sheets[index] --> 'cells' --> row --> column --> Interpreted value
 --> 'cellsInfo' --> row --> column --> 'type' (Can be 'date', 'number', or 'unknown')
 --> 'raw' (The raw data that Excel stores for that data cell)

- $objectClass->boundsheets[index_sheet]['name'] chứa tên của trang tính excel có thứ tự chỉ mục là "index_sheet".

Bạn có thể duyệt qua mảng $objectClass->sheets để thêm dữ liệu vào bảng HTML

- Dưới đây là ví dụ tạo và xuất bảng HTML với dữ liệu excel từ mỗi sheet

read('test.xls');

// this function creates and returns a HTML table with excel rows and columns data
// Parameter - array with excel worksheet data
function sheetData($sheet) {
 $re = ''; // starts html table

 $x = 1;
 while($x <= $sheet['numRows']) {
 $re .= "\n";
 $y = 1;
 while($y <= $sheet['numCols']) {
 $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';
 $re .= " \n"; 
 $y++;
 } 
 $re .= "\n";
 $x++;
 }

 return $re .'$cell'; // ends and returns the html table
}

$nr_sheets = count($excel->sheets); // gets the number of worksheets
$excel_data = ''; // to store the the html tables with data of each sheet

// traverses the number of sheets and sets html table with each sheet data in $excel_data
for($i=0; $i<$nr_sheets; $i++) {
 $excel_data .= '

Sheet '. ($i + 1) .' ('. $excel->boundsheets[$i]['name'] .')

'. sheetData($excel->sheets[$i]) .'
'; } echo $excel_data; // outputs HTML tables with excel file data

- Trong kho lưu trữ với lớp PhpExcelReader, bạn sẽ tìm thấy một "bài kiểm tra. xls" để kiểm tra và hai ví dụ và nhận xét có chi tiết trong mã.
• Lớp này là phiên bản dành cho PHP 5. 3+ của lớp Spreadsheet_Excel_Reader, từ. Trang web PHP-ExcelReader.

Trong bài viết này, chúng ta sẽ thảo luận về cách đọc tệp Excel và chọn các hàng và cột cụ thể từ tệp đó bằng Ngôn ngữ lập trình R

Tệp được sử dụng

PHP đọc từng hàng tệp Excel

Để đọc một tệp Excel vào R, chúng ta phải chuyển đường dẫn của nó làm đối số cho thư viện readxl của hàm read_excel()

cú pháp

read_excel(đường dẫn)

Để chọn một cột cụ thể, chúng ta có thể sử dụng lập chỉ mục

cú pháp

df [ row_index , column_index ]

Ở đây df đại diện cho tên khung dữ liệu hoặc tên tệp Excel hoặc bất cứ thứ gì

Trích xuất các hàng cụ thể từ tệp Excel

Đối với điều này, chúng ta phải chuyển chỉ mục của hàng được trích xuất làm đầu vào cho việc lập chỉ mục. Do đó, hàng tại chỉ mục được cung cấp sẽ được tìm nạp và hiển thị

ví dụ 1

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

(readxl)3

(readxl)4(readxl)5

đầu ra

PHP đọc từng hàng tệp Excel

ví dụ 2

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

 9

(readxl)4(readxl)5

đầu ra

PHP đọc từng hàng tệp Excel

ví dụ 3

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

(5

(readxl)4(readxl)5

đầu ra

PHP đọc từng hàng tệp Excel

Để có được nhiều hàng tương tự, không cần sửa đổi nhiều. Chỉ mục của các hàng được trích xuất phải được chuyển dưới dạng vectơ tới phần row_index của cú pháp lập chỉ mục

Ví dụ 4.  

R




library(

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
00
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2____13(
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
06
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

(readxl)4

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
09____110
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
11

đầu ra

PHP đọc từng hàng tệp Excel

Trích xuất các cột cụ thể từ tệp Excel

Điều này tương tự như cách tiếp cận ở trên ngoại trừ việc trích xuất chỉ mục cột của cột cần được đưa ra làm đối số

ví dụ 1

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
25

(readxl)4(readxl)5

đầu ra

PHP đọc từng hàng tệp Excel

ví dụ 2

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

(1

(readxl)4(readxl)5

đầu ra.  

PHP đọc từng hàng tệp Excel

ví dụ 3

R




library(readxl)

 

setwd____9

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
2
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
3((readxl)0
read('excel_file.xls'); // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
echo '
';
var_export($excel->sheets);
echo '
1

 

(readxl)07

(readxl)4(readxl)5

đầu ra

PHP đọc từng hàng tệp Excel

Để có được nhiều cột cùng một lúc, chỉ mục của các cột được trích xuất phải được cung cấp dưới dạng vectơ trong phần column_index của cú pháp lập chỉ mục. Tất cả các cột có chỉ mục được cung cấp sẽ được tìm nạp và hiển thị