将 xlsx 转换为 csv 在服务器上不起作用
conversion of xlsx into csv not work on server
我想将数据从 xlsx 文件导入我的数据库为此,我使用 phpexcel 库将 xls 或 xlsx 文件转换为 csv,然后读取数据。
此代码可在本地主机和服务器上正常将 xls 文件转换为 csv,但 xlsx 到 csv 的转换仅在本地主机上完成。
这是我的代码:
function convertXLStoCSV($infile,$outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
并调用此函数使用:
if($ext == 'xls' or $ext == 'xlsx')
{
$new_doc_name = time() . "." .$ext;
$target_path = "../../uploaded_files/";
$target_path = $target_path . $new_doc_name ;
if ($_FILES["CSV_file"]["type"] == "application/xls" or $_FILES["CSV_file"]["type"] == "application/xlsx" or $_FILES["CSV_file"]["type"] == "application/vnd.ms-excel" or $_FILES["CSV_file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
if(move_uploaded_file($_FILES['CSV_file']['tmp_name'], $target_path)) {
convertXLStoCSV($target_path,'output.csv');
if(($handle = fopen("output.csv" , "r")) !== FALSE)
请帮忙
试试这个
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadFIle);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($index);
// write out each worksheet to it's name with CSV extension
$outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";
$objWriter->setSheetIndex($index);
$objWriter->save($outFile);
$index++;
}
我想将数据从 xlsx 文件导入我的数据库为此,我使用 phpexcel 库将 xls 或 xlsx 文件转换为 csv,然后读取数据。 此代码可在本地主机和服务器上正常将 xls 文件转换为 csv,但 xlsx 到 csv 的转换仅在本地主机上完成。 这是我的代码:
function convertXLStoCSV($infile,$outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
并调用此函数使用:
if($ext == 'xls' or $ext == 'xlsx')
{
$new_doc_name = time() . "." .$ext;
$target_path = "../../uploaded_files/";
$target_path = $target_path . $new_doc_name ;
if ($_FILES["CSV_file"]["type"] == "application/xls" or $_FILES["CSV_file"]["type"] == "application/xlsx" or $_FILES["CSV_file"]["type"] == "application/vnd.ms-excel" or $_FILES["CSV_file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
if(move_uploaded_file($_FILES['CSV_file']['tmp_name'], $target_path)) {
convertXLStoCSV($target_path,'output.csv');
if(($handle = fopen("output.csv" , "r")) !== FALSE)
请帮忙
试试这个
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadFIle);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($index);
// write out each worksheet to it's name with CSV extension
$outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";
$objWriter->setSheetIndex($index);
$objWriter->save($outFile);
$index++;
}