php读取excel并写入到数据库

php读取excel并写入到数据库,第1张

用 php Win32 OLE

##Using OLE;

read('Book1xls');

// print number of rows, columns and sheets

echo "Number of sheets: " sizeof($excel->sheets) "\n";

for ($x=0; $xsheets); $x++) {

echo "Number of rows in sheet " ($x+1) ": " $excel->sheets[$x]["numRows"] "\n";

echo "Number of columns in sheet " ($x+1) ": " $excel->sheets[$x]["numCols"] "\n";

PHP读取excel的方法,如果你的服务器不支持COM组件,我就不好说了,下面的只是示范的例子。<PHP

$filename = "c:/spreadhseet/testxls";

$sheet1 = 1;

$sheet2 = "sheet2";

$excel_app = new COM("Excelapplication") or Die ("Did not connect");

print "Application name: {$excel_app->Application->value}n" ;

print "Loaded version: {$excel_app->Application->version}n";

$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");

$Worksheet = $Workbook->Worksheets($sheet1);

$Worksheet->activate;

$excel_cell = $Worksheet->Range("C4");

$excel_cell->activate;

$excel_result = $excel_cell->value;

print "$excel_resultn";

$Worksheet = $Workbook->Worksheets($sheet2);

$Worksheet->activate;

$excel_cell = $Worksheet->Range("C4");

$excel_cell->activate;

$excel_result = $excel_cell->value;

print "$excel_resultn";

#To close all instances of excel:

$Workbook->Close;

unset($Worksheet);

unset($Workbook);

$excel_app->Workbooks->Close();

$excel_app->Quit();

unset($excel_app);

>

$fp = fopen('php://output', 'a');

// 输出Excel列名信息

$head = array("电子邮件");

foreach ($head as $i => $v) {

// CSV的Excel支持GBK编码,一定要转换,否则乱码

$head[$i] = iconv('utf-8', 'gbk', $v);

}

// 将数据通过fputcsv写到文件句柄

fputcsv($fp, $head);

// 计数器

$cnt = 0;

// 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小

$limit = 100000;

// 逐行取出数据,不浪费内存

$count = count($email);

for($t=0;$t<$count;$t++) {

$cnt ++;

if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题

ob_flush();

flush();

$cnt = 0;

}

$row[] = $email[$t];

foreach ($row as $i => $v) {

$row[$i] = iconv('utf-8', 'gbk', $v);

}

fputcsv($fp, $row);

unset($row);

}

<php

////////////////////////////////// BEGIN SETUP

$filename ="document_namexls";

header('Content-type: application/ms-excel');

header('Content-Disposition: attachment; filename='$filename);

////////////////////////////////// END SETUP

////////////////////////////////// BEGIN GATHER

// Your MySQL queries, fopens, et cetera go here

// Cells are delimited by \t

// \n is just like you might expect; new line/row below

// EG:

$stuff="PART\tQTY\tVALUE\t\n";

$stuff=$stuff"01-001-0001\t37\t2876\t\n";

$stuff=$stuff"01-001-0002\t6\t34706\t\n";

$stuff=$stuff"01-001-0003\t12\t711\t\n";

////////////////////////////////// END GATHER

// The point is to get all of your data into one string and then:

////////////////////////////////// BEGIN DUMP

echo $stuff;

////////////////////////////////// END DUMP

>

以上就是关于php读取excel并写入到数据库全部的内容,包括:php读取excel并写入到数据库、php能预览excel吗、php怎么导出大量数据的Excel等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9400854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存