有没有办法根据请求通过apache / .htaccess将csv文件转换为excel文件
答案 0 :(得分:68)
使用PHPExcel
include 'PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('CSV');
// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
$objReader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
$objReader->setInputEncoding('UTF-16LE');
$objPHPExcel = $objReader->load('MyCSVFile.csv');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('MyExcelFile.xls');
答案 1 :(得分:1)
注意:PHPExcel现在列为DEPRECATED。
将用户定向到PhpSpreadsheet。
答案 2 :(得分:1)
require '../../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
/* Set CSV parsing options */
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setSheetIndex(0);
/* Load a CSV file and save as a XLS */
$spreadsheet = $reader->load('../../uploads/test.csv');
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
这对我来说就像是魅力!干杯!!!
答案 3 :(得分:0)
是的,由于apache是开源的,您可以修改.htaccess解析器来调用库以将CSV文件转换为excel文件。但我认为这不是你想要的。 : - 。)
我认为你真正需要的是一个动态的网站。然后,您可以使用PHP或任何支持的语言来完成您需要做的事情。
这样的事情: http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/
答案 4 :(得分:0)
sourceforge中有一个项目可以进行此转换:
http://sourceforge.net/projects/py-csv2xls/
但是对于转换,你需要在apache中创建一个动态页面(在python,php ...中)
答案 5 :(得分:0)
不推荐使用PHPExcel。您必须使用PhpSpreadsheet。
使用PhpSpreadsheet,您可以通过以下代码将csv转换为xlsx:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Csv');
// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
// $reader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
// $reader->setInputEncoding('UTF-16LE');
$objPHPExcel = $reader->load('/home/superman/projects/test/csv-app/file.csv');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
$objWriter->save('excel_file.xlsx');