在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文件相关推荐

  1. java中读取excel数据类型_在Java中读取Excel文件的内容

    利用JExcelApi来动态生成excel文档 首先,请到http://www.andykhan.com/jexcelapi/index.html下载java excel api,主页上同时有比较详细 ...

  2. python中如何打开csv文件_在Python中从CSV文件读取数据

    我正在从包含以下数据的CSV文件(xyz.CSV)中读取数据: col1,col2,col3,col4 name1,empId1,241682-27638-USD-CIGGNT ,1 name2,em ...

  3. python代码读取外部变量_在Python中从外部文件中写入和读取特定的变量

    我正在写一个程序,在这个程序中,我想在外部文件中读写具有不同数据类型的特定变量. 在尝试了几个不同的模块后,我得到的最接近的方法是使用pickle. Pickle似乎很好,因为它能理解不同的数据类型, ...

  4. .vue文件_在idea中创建vue文件

    如何让idea支持.vue文件_WinterBluestar的博客-CSDN博客_idea中怎么样.vue文件夹​blog.csdn.net

  5. jenkins修改pom文件_从Jenkins中的pom文件自动派生强制性SonarQube属性

    情况: 我想用由詹金斯(1.642.4)触发的SonarQube(5.4)分析我的项目.它是使用maven构建的Java项目. 我看到两种触发分析的方法: 发布构建操作"使用maven进行S ...

  6. matlab分析xml文件_修改Java中的XML文件(DOM分析器)

    matlab分析xml文件 We can modify XML file in Java using DOM parser. We can add elements, remove elements, ...

  7. cmd中如何运行python文件_在cmd中运行.py文件: python的操作步骤

    在cmd中运行.py文件: python的操作步骤 1 打开cmd, 不改变运行的目录: 输入python 空格  调试好的python文件路径 或者python 空格  将python文件拖入cmd ...

  8. python读取sav文件_在Python中读取SPSS(.sav)文件时,获取“title already used as a name or title”错误...

    我正在读一个SPSS文件(.sav).我下面的代码可以读取.sav文件.但是,我遇到了一个非常奇怪的错误.当我试图读取另一个.sav文件时,它会给出以下错误Traceback (most recent ...

  9. python写打开的excel 冲突_在Python中打开excel文件时出错

    嗨,我对python很陌生,这里我试图用python代码打开xls文件,但它显示了一些错误,如下所示.在 代码:from xlrd import open_workbook import os.pat ...

  10. arcgis怎么关联excel表_在arcgis中添加excel表格数据-ArcGIS如何将Excel里的数据关联至地图上...

    ArcGIS如何将Excel里的数据关联至地图上 1.打开一个arcgis工程文件. 2.在左侧窗口中右击面要素文件,选择"open attribute table". 3.在出现 ...

最新文章

  1. 《深度探索C++对象模型》--5 构造析构拷贝 6 执行期语意学
  2. python线程暂停_关于多线程:如何使“停止”按钮终止已经在Tkinter中运行的“开始”功能(Python)...
  3. Qt QGraphics体系及刷新机制介绍
  4. S1 Python 基础
  5. 设计模式----python版本
  6. 【分享】虹软人脸识别应用开发过程
  7. 数据库计算机报告,外文数据库计算机检索报告实例.pdf
  8. 关于 Oracle分页数据重复的问题
  9. win7 vs2017 程序报错_windows + VS2017 配置libpytorch
  10. django进阶02websocket
  11. Mysql使用dos命令安装
  12. 视频教程-Linux C语言编程基础视频精讲-C/C++
  13. ERROR 1018 (HY000): Can‘t read dir of ‘.‘ (errno: 13 - Permission denied)
  14. 天文竞赛怎么用计算机,师范生教学技能大赛!计算机设计大赛!主持礼仪风采大赛!天文台活动预告!民宿企划竞赛!舞蹈大赛!...
  15. 简单易用的APIv3版微信支付SDK
  16. 5分钟带你走进webpack
  17. .NET开发中的几个JS奇技淫巧
  18. 【以太网通信】RGMII 接口及其时序规范
  19. 什么是人因学( Ergonomics)?
  20. 如何在latex中加入目录,如何给目录添加超链接,以及如何去除超链接上的红色方框

热门文章

  1. 移动APP测试用例设计的关注点
  2. 计算机二级报名省市,计算机二级报名通知:全国19省市报名时间及考试安排!...
  3. ViewStub使用
  4. ipad键盘映射_如何在iPad上重新映射修饰键
  5. 经典的同态滤波算法的优化及其应用参数配置
  6. webstorm修改主题色和配色
  7. vs2019中git提交代码的步骤
  8. Git提交代码的流程
  9. 100个Python实战项目(一)使用 Python 生成二维码
  10. CNN图像识别_算法篇