csv 到 excel 转换

发布于 2024-09-26 01:17:21 字数 56 浏览 3 评论 0原文

有没有办法根据请求通过 apache/.htaccess 将 csv 文件转换为 excel 文件

Is there a way to convert csv file to excel file upon request through apache/.htaccess

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

灯角 2024-10-03 01:17:21

使用 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');

Using 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');
蓝咒 2024-10-03 01:17:21

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');

PHPExcel is deprecated. You must use PhpSpreadsheet.

With PhpSpreadsheet, you can convert csv to xlsx by the following code:

$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');
软糖 2024-10-03 01:17:21

使用 PhpSpreadheet

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);

这对我来说就像一个魅力!干杯!

Using PhpSpreadheet:

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);

This one worked for me like a charm ! Cheers !!!

暗藏城府 2024-10-03 01:17:21

注意:PHPExcel 现在被列为已弃用

用户将被定向到 PhpSpreadsheet

Note: PHPExcel is now listed as DEPRECATED.

Users are directed to PhpSpreadsheet.

路还长,别太狂 2024-10-03 01:17:21

是的,由于 apache 是开源的,您可以修改 .htaccess 解析器来调用库将 CSV 文件转换为 excel 文件。但我不认为这就是你要找的。 :-)。

我认为您真正需要的是一个动态网站。然后您可以使用 PHP 或任何支持的语言来完成您需要做的事情。

像这样的东西:
http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/" westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/

Yes, since apache is open-source, you can modify the .htaccess parser to call a library to convert your CSV files into excel files. But I don't think this is what you're looking for. :-).

I think really what you need is a dynamic web site. Then you can use PHP or any supported language to do what you need to do.

something like this:
http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/

流绪微梦 2024-10-03 01:17:21

sourceforge 中有一个项目可以执行此转换:

http://sourceforge.net/projects/py-csv2xls /

但是为了进行转换,您需要在 apache 中创建一个动态页面(在 python、php...)

There is a project in sourceforge that does this conversion:

http://sourceforge.net/projects/py-csv2xls/

But for the conversion you need to make a dynamic page in apache (in python, php...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文