[英]Need to read Excel spreadsheets, store in Oracle DB using PHP, to view in EXTJS Grouping grid
我有一個使用PHPExcel的PHP解析器,該解析器讀取Excel文件並將內容存儲到Oracle數據庫中。
問題是解析器讀取每一行,並且沒有在行的標題和這些行中包含的數據之間建立任何區別。 從數據庫中讀取信息時,會將其讀取到平面文件列表中,並且不容易瀏覽。
我目前正在將數據讀取到EXTJS網格中。 我希望能夠讀取Excel,將其存儲在數據庫中,然后將其拉出並在新的EXTJS GroupingGrid中查看,該組將成為Excel文件中每個工作表的“標題”。
有沒有人使用過PHPExcel或知道如何使用PHPExcel來讀取Excel文件並在每個工作表中輸出標題(1,1),以便我可以將其存儲在數據庫中並將其拉出並以JSON顯示,因此groupingGrid將使我能夠對每個標題使用加號,以便我可以單擊加號並查看網格中該標題下的所有內容?
您好,您可以使用此php-excell-reader http://code.google.com/p/php-excel-reader/ 。 我正在使用它並且效果完美。 您可以操縱每一行的每個單元格。
// Include the PHPExcel library
require_once './Classes/PHPExcel.php';
// Load the Excel File
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("myExcelFile.xls");
// Loop through every worksheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// Read the value at Cell A1 in the current worksheet
$cellValue = $worksheet->getCell('A1')->getValue();
// Do whatever you want with $cellValue
// Store it in a database
// Whatever...
}
當然,如果myExcelFile.xls不存在,或者您沒有讀取權限,或者不是真正的Excel文件,您可能還希望在其中進行一些錯誤處理,只需將其包裝在嘗試/捕獲塊並根據需要進行處理。
如果您只想加載每個工作表的第一行而不是每一行和每一列,則還可以設置一個讀取篩選器:
// Include the PHPExcel library
require_once './Classes/PHPExcel.php';
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class firstRowFilter implements PHPExcel_Reader_IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
// Only read the heading row
return ($row == 1);
}
// Create an instance of our Read Filter
$firstRowFilter = new firstRowFilter();
// Instantiate the correct Reader
$objReader = PHPExcel_IOFactory::createReader('Excel5');
// Tell the Reader only to load cells that match our Read Filter
$objReader->setReadFilter($firstRowFilter)
// Load the Excel File
$objPHPExcel = $objReader->load("myExcelFile.xls");
// Loop through every worksheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// Read the value at Cell A1 in the current worksheet
$cellValue = $worksheet->getCell('A1')->getValue();
// Do whatever you want with $cellValue
// Store it in a database
// Whatever...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.