使用PHP Excel,我以编程方式尝试将.xlsx
文件转换为.csv
。我的代码采用用户上传的电子表格,保存为xlsx
,csv
,xls
并将其上传到Oracle 11g数据库。该代码目前适用于xls
和csv
(至少在大多数情况下)。当我上传.xlsx
文件时,为什么会收到错误:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'The filename /tmp/phpsu3acF is not recognised as an OLE file' in /opt/apache/servers/planninganddesign/htdocs/JointUse/Classes/PHPExcel/Shared/OLERead.php:89 Stack trace: #0 /opt/apache/servers/planninganddesign/htdocs/JointUse/Classes/PHPExcel/Reader/Excel5.php(1164): PHPExcel_Shared_OLERead->read('/tmp/phpsu3acF') #1 /opt/apache/servers/planninganddesign/htdocs/JointUse/Classes/PHPExcel/Reader/Excel5.php(612): PHPExcel_Reader_Excel5->_loadOLE('/tmp/phpsu3acF') #2 /opt/apache/servers/planninganddesign/htdocs/JointUse/update_handler.php(21): PHPExcel_Reader_Excel5->load('/tmp/phpsu3acF') #3 {main} thrown in /opt/apache/servers/planninganddesign/htdocs/JointUse/Classes/PHPExcel/Shared/OLERead.php on line 89
形式:
<?php require('reader.php');
ini_set("memory_limit","200M");
?>
<form name="file" enctype="multipart/form-data" action="update_handler.php" method="post" >
<b>Accepted File Extensions: .csv .xls </b><br>
Please save your excel table as one of the <br>accepted file extensions prior to uploading. <br><br>
<u>Upload File:</u> <input type="file" name="uploaded"><br>
<input type="submit" value="Submit">
</form>
处理程序:
<?php
require_once('Classes/PHPExcel/IOFactory.php');
require_once("mcl_Oci.php");
?>
<br>
<?php
$filename = $_FILES['uploaded']['name'];
$file = $_FILES['uploaded']['tmp_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext='xls') {
$inputFileType = 'Excel5';
$inputFileName = $file;
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save('abc.csv');}
$files = fopen('abc.csv', 'r');
if($files) {
while (($line = fgetcsv($files)) !== FALSE) {
$csv_array[] = array_combine(range(1, count($line)), array_values($line));
} echo 'Your .xls file was uploaded successfully. Have a nice day.';
} elseif($ext='csv'){
while (($line = fgetcsv($files)) !== FALSE) {
$csv_array[] = array_combine(range(1, count($line)), array_values($line));
} echo 'Your .csv file was uploaded successfully. Have a nice day.';
} elseif($ext='xlsx') {
while (($line = fgetcsv($files)) !== FALSE) {
$csv_array[] = array_combine(range(1, count($line)), array_values($line));
} echo 'Your .xlsx file was uploaded successfully. Have a nice day.';
}
else {
echo "This is not an accepted file type. Please save as either '*.csv' or '*.xls' and re-upload.";
}
}
$ArrayNumber = 3;
while ($csv_array["$ArrayNumber"] != NULL) {
$ArrayNumber = $ArrayNumber + 1;
$RECEIVED = trim($csv_array["$ArrayNumber"]["1"], "'\"");
$REGION = trim($csv_array["$ArrayNumber"]["2"], "'\"");
$PERMIT = trim($csv_array["$ArrayNumber"]["3"], "'\"");
$CUSTOMER = trim($csv_array["$ArrayNumber"]["4"], "'\"");
$TO_BUSINESS = trim($csv_array["$ArrayNumber"]["5"], "'\"");
$TO_PERMIT = trim($csv_array["$ArrayNumber"]["6"], "'\"");
$POWER_ADDRESS = trim($csv_array["$ArrayNumber"]["7"], "'\"");
$COMPANY = trim($csv_array["$ArrayNumber"]["8"], "'\"");
$MAPSECTION = trim($csv_array["$ArrayNumber"]["9"], "'\"");
$LOCATION = trim($csv_array["ArrayNumber"]["10"], "'\"");
$POLES = trim($csv_array["$ArrayNumber"]["11"], "'\"");
$FEE = trim($csv_array["$ArrayNumber"]["12"], "'\"");
$INVOICE = trim($csv_array["$ArrayNumber"]["13"], "'\"");
$DATE_LARRY = trim($csv_array["$ArrayNumber"]["14"], "'\"");
$DAYS_LARRY = trim($csv_array["$ArrayNumber"]["15"], "'\"");
$PI_PON = trim($csv_array["$ArrayNumber"]["16"], "'\"");
$DATE_PD = trim($csv_array["$ArrayNumber"]["17"], "'\"");
$DAYS_LARRY_PD = trim($csv_array["$ArrayNumber"]["18"], "'\"");
$RETURNED = trim($csv_array["$ArrayNumber"]["19"], "'\"");
$DAYS_PD = trim($csv_array["$ArrayNumber"]["20"], "'\"");
$ERRORS_PD = trim($csv_array["$ArrayNumber"]["21"], "'\"");
$ERRORS_MULTIPLE = trim($csv_array["$ArrayNumber"]["22"], "'\"");
$DATE_MAKEREADY_JU = trim($csv_array["$ArrayNumber"]["23"], "'\"");
$DATE_MAKEREADY_COMPLETE = trim($csv_array["$ArrayNumber"]["24"], "'\"");
$DAYS_MAKEREADY = trim($csv_array["$ArrayNumber"]["25"], "'\"");
$APPROVAL = trim($csv_array["$ArrayNumber"]["26"], "'\"");
$DAYS_PERMIT_IN_JU = trim($csv_array["$ArrayNumber"]["27"], "'\"");
$DATE_PLANNING = trim($csv_array["$ArrayNumber"]["28"], "'\"");
$DAYS_OPEN = trim($csv_array["$ArrayNumber"]["29"], "'\"");
$DATE_ROUTED = trim($csv_array["$ArrayNumber"]["30"], "'\"");
$DAYS_TOTAL = trim($csv_array["$ArrayNumber"]["31"], "'\"");
$COMMENTS = trim($csv_array["$ArrayNumber"]["32"], "'\"");
$check = "SELECT COUNT(*) AS NUMBER_OF FROM INTOXDM.JOINT_USE WHERE PERMIT = '$PERMIT'";
$checkparse = oci_parse($objConnect,$check);
oci_define_by_name($checkparse, 'NUMBER_OF', $CHECK);
oci_execute($checkparse);
oci_fetch($checkparse);
if ($CHECK == 0) {
$objConnect = oci_connect("user", "pass", "(description=(address=(protocol=tcp)(host=HOST)(port=1533))(connect_data=(service_name=sid)))");
$strSQL = "INSERT INTO INTOXDM.JOINT_USE (RECEIVED, REGION, PERMIT, CUSTOMER, TO_BUSINESS, TO_PERMIT, POWER_ADDRESS, COMPANY, MAPSECTION, POLES, FEE, INVOICE, DATE_LARRY, DAYS_LARRY, PI_PON, DATE_PD, DAYS_LARRY_PD, RETURNED, DAYS_PD, ERRORS_PD, ERRORS_MULTIPLE, DATE_MAKEREADY_JU, DATE_MAKEREADY_COMPLETE, DAYS_MAKEREADY, APPROVAL, DAYS_PERMIT_IN_JU, DATE_PLANNING, DAYS_OPEN, DATE_ROUTED, DAYS_TOTAL, COMMENTS) VALUES ('$RECEIVED', '$REGION', '$PERMIT', '$CUSTOMER', '$TO_BUSINESS', '$TO_PERMIT', '$POWER_ADDRESS', '$COMPANY', '$MAPSECTION', '$POLES', '$FEE', '$INVOICE', '$DATE_LARRY', '$DAYS_LARRY', '$PI_PON', '$DATE_PD', '$DAYS_LARRY_PD', '$RETURNED', '$DAYS_PD', '$ERRORS_PD', '$ERRORS_MULTIPLE', '$DATE_MAKEREADY_JU', '$DATE_MAKEREADY_COMPLETE', '$DAYS_MAKEREADY', '$APPROVAL', '$DAYS_PERMIT_IN_JU', '$DATE_PLANNING', '$DAYS_OPEN', '$DATE_ROUTED', '$DAYS_TOTAL', '$COMMENTS')";
$parse = oci_parse($objConnect, $strSQL);
$execute = oci_execute(oci_parse($objConnect, $strSQL));
} else {
$strSQL = "UPDATE INTOXDM.JOINT_USE SET RECEIVED = '$RECEIVED', REGION = '$REGION', CUSTOMER = '$CUSTOMER', TO_BUSINESS = '$TO_BUSINESS', TO_PERMIT = '$TO_PERMIT', POWER_ADDRESS = '$POWER_ADDRESS', COMPANY = '$COMPANY', MAPSECTION = '$MAPSECTION', POLES = '$POLES', FEE = '$FEE', INVOICE = '$INVOICE', DATE_LARRY = '$DATE_LARRY', DAYS_LARRY = '$DAYS_LARRY', PI_PON = '$PI_PON', DATE_PD = '$DATE_PD', DAYS_LARRY_PD = '$DAYS_LARRY_PD', RETURNED = '$RETURNED', DAYS_PD = '$DAYS_PD', ERRORS_PD = '$ERRORS_PD', ERRORS_MULTIPLE = '$ERRORS_MULTIPLE', DATE_MAKEREADY_JU = '$DATE_MAKEREADY_JU', DATE_MAKEREADY_COMPLETE = '$DATE_MAKEREADY_COMPELTE', DAYS_MAKEREADY = '$DAYS_MAKEREADY', APPROVAL = '$APPROVAL', DAYS_PERMIT_IN_JU = '$DAYS_PERMIT_IN_JU', DATE_PLANNING = '$DATE_PLANNING', DAYS_OPEN = '$DAYS_OPEN', DATE_ROUTED = '$DATE_ROUTED', DAYS_TOTAL = '$DAYS_TOTAL', COMMENTS = '$COMMENTS' WHERE PERMIT = '$PERMIT'";
$parse = oci_parse($objConnect, $strSQL);
$execute = oci_execute($parse); }
$DELETE = "DELETE FROM INTOXDM.JOINT_USE WHERE PERMIT IS NULL";
$parseDelete = oci_parse($objConnect, $DELETE);
$execute = oci_execute($parseDelete);
}
$conn = oci_connect("user", "pass", "(description=(address=(protocol=tcp)(host=HOST)(port=1533))(connect_data=(service_name=sid)))");
$sel = "update INTOXDM.JOINT_USE set DAYS = trunc(sysdate - to_date(RECEIVED, 'MM/DD/YY'))";
$par = oci_parse($conn, $sel);
$exe = oci_execute($par);
header("location: website"); ?>
答案 0 :(得分:2)
请勿使用Excel5
阅读器获取(OfficeOpenXML
).xlsx
个文件:Excel5
阅读器适用于BIFF
- 格式{{1文件;您需要使用.xls
阅读器获取Excel2007
- 格式文件。
如果您的用户可以上传不同格式的文件;或者使用IOFactory OfficeOpenXML
方法告诉您使用哪个Reader,然后实例化该Reader;或者只是使用IOFactory identify()
方法,它将自动选择正确的Reader。