php读取excel文件_在PHP中读取Excel文件
在PHP中讀取Excel文件
我試圖讀取Excel文件(Office 2003)。 有一個Excel文件需要上傳并解析其內容。
通過谷歌,我只能找到這些相關(和不充分的主題)的答案:生成Excel文件,閱讀Excel XML文件,閱讀Excel CSV文件或不完整的廢棄項目。 我擁有Office 2003,所以如果我需要任何文件,它們都可用。 它安裝在我的盒子上,但不能安裝在我的共享主機上。
編輯:到目前為止,所有答案都指向PHP-ExcelReader和/或關于如何使用它的這篇附加文章。
Dinah asked 2019-08-25T22:06:00Z
7個解決方案
57 votes
據我所知,你有兩個選擇:
Spreadsheet_Excel_Reader,它知道Office 2003二進制格式
PHPExcel,它既了解Office 2003,也了解Excel 2007(XML)。 (按照鏈接,您將看到他們將此庫升級到PHPSpreadSheet)
PHPExcel使用Spreadsheet_Excel_Reader作為Office 2003格式。
更新:我曾經閱讀過一些Excel文件,但我使用Office 2003 XML格式來閱讀它們并告訴使用該應用程序的人保存并僅上傳該類型的Excel文件。
Ionu? G. Stan answered 2019-08-25T22:07:03Z
44 votes
我使用PHP-ExcelReader來讀取xls文件,效果很好。
Luis Melgratti answered 2019-08-25T22:06:11Z
17 votes
這取決于您希望如何使用excel文件中的數據。 如果要將其導入mysql,只需將其保存為CSV格式的文件,然后使用fgetcsv進行解析。
Jimbo answered 2019-08-25T22:07:27Z
5 votes
閱讀XLSX(Excel 97-2003)
[https://github.com/shuchkin/simplexls]
if ( $xls = SimpleXLS::parse('book.xls') ) {
print_r( $xls->rows() );
} else {
echo SimpleXLS::parseError();
}
閱讀XLSX(Excel 2003+)
[https://github.com/shuchkin/simplexlsx]
if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
print_r( $xlsx->rows() );
} else {
echo SimpleXLSX::parseError();
}
產量
Array (
[0] => Array
(
[0] => ISBN
[1] => title
[2] => author
[3] => publisher
[4] => ctry
)
[1] => Array
(
[0] => 618260307
[1] => The Hobbit
[2] => J. R. R. Tolkien
[3] => Houghton Mifflin
[4] => USA
)
)
CSV php閱讀器
[https://github.com/shuchkin/simplecsv]
Sergey Shuchkin answered 2019-08-25T22:08:27Z
3 votes
試試這個...
我使用以下代碼來閱讀" xls和xlsx"
include 'excel_reader.php'; // include the class
$excel = new PhpExcelReader; // creates object instance of the class
$excel->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 '
';or
echo '
';print_r($excel->sheets);
echo '
';參考文獻:[http://coursesweb.net/php-mysql/read-excel-file-data-php_pc]
Deenadhayalan Manoharan answered 2019-08-25T22:08:59Z
3 votes
有一篇很棒的文章解釋如何通過php代碼讀取/寫入excel文件,他們建議使用MS-Excel Stream Handler PHP類,這是一流的頂級庫:)
Leopathu answered 2019-08-25T22:09:23Z
2 votes
// Here is the simple code using COM object in PHP
class Excel_ReadWrite{
private $XLSHandle;
private $WrkBksHandle;
private $xlBook;
function __construct() {
$this->XLSHandle = new COM("excel.application") or die("ERROR: Unable to instantaniate COM!\r\n");
}
function __destruct(){
//if already existing file is opened
if($this->WrkBksHandle != null)
{
$this->WrkBksHandle->Close(True);
unset($this->WrkBksHandle);
$this->XLSHandle->Workbooks->Close();
}
//if created new xls file
if($this->xlBook != null)
{
$this->xlBook->Close(True);
unset($this->xlBook);
}
//Quit Excel Application
$this->XLSHandle->Quit();
unset($this->XLSHandle);
}
public function OpenFile($FilePath)
{
$this->WrkBksHandle = $this->XLSHandle->Workbooks->Open($FilePath);
}
public function ReadData($RowNo, $ClmNo)
{
$Value = $this->XLSHandle->ActiveSheet->Cells($RowNo, $ClmNo)->Value;
return $Value;
}
public function SaveOpenedFile()
{
$this->WrkBksHandle->Save();
}
/***********************************************************************************
* Function Name:- WriteToXlsFile() will write data based on row and column numbers
* @Param:- $CellData- cell data
* @Param:- $RowNumber- xlsx file row number
* @Param:- $ColumnNumber- xlsx file column numbers
************************************************************************************/
function WriteToXlsFile($CellData, $RowNumber, $ColumnNumber)
{
try{
$this->XLSHandle->ActiveSheet->Cells($RowNumber,$ColumnNumber)->Value = $CellData;
}
catch(Exception $e){
throw new Exception("Error:- Unable to write data to xlsx sheet");
}
}
/****************************************************************************************
* Function Name:- CreateXlsFileWithClmName() will initialize xls file with column Names
* @Param:- $XlsColumnNames- Array of columns data
* @Param:- $XlsColumnWidth- Array of columns width
*******************************************************************************************/
function CreateXlsFileWithClmNameAndWidth($WorkSheetName = "Raman", $XlsColumnNames = null, $XlsColumnWidth = null)
{
//Hide MS Excel application window
$this->XLSHandle->Visible = 0;
//Create new document
$this->xlBook = $this->XLSHandle->Workbooks->Add();
//Create Sheet 1
$this->xlBook->Worksheets(1)->Name = $WorkSheetName;
$this->xlBook->Worksheets(1)->Select;
if($XlsColumnWidth != null)
{
//$XlsColumnWidth = array("A1"=>15,"B1"=>20);
foreach($XlsColumnWidth as $Clm=>$Width)
{
//Set Columns Width
$this->XLSHandle->ActiveSheet->Range($Clm.":".$Clm)->ColumnWidth = $Width;
}
}
if($XlsColumnNames != null)
{
//$XlsColumnNames = array("FirstColumnName"=>1, "SecondColumnName"=>2);
foreach($XlsColumnNames as $ClmName=>$ClmNumber)
{
// Cells(Row,Column)
$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Value = $ClmName;
$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Font->Bold = True;
$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Interior->ColorIndex = "15";
}
}
}
//56 is for xls 8
public function SaveCreatedFile($FileName, $FileFormat = 56)
{
$this->xlBook->SaveAs($FileName, $FileFormat);
}
public function MakeFileVisible()
{
//Hide MS Excel application window`enter code here`
$this->XLSHandle->Visible = 1;
}
}//end of EXCEL class
Vicky answered 2019-08-25T22:09:40Z
總結
以上是生活随笔為你收集整理的php读取excel文件_在PHP中读取Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pdf复制去除换行
- 下一篇: php读取excel文件_PHP读取Ex