gpt4 book ai didi

php - 将 xlsx 转换为 csv 在服务器上不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 21:01:09 24 4
gpt4 key购买 nike

我想将数据从 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++;
}

关于php - 将 xlsx 转换为 csv 在服务器上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com