Linux csv到excel的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3874840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:36:25  来源:igfitidea点击:

csv to excel conversion

phplinuxapachecommand-line

提问by Ashraf Sufian

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

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

采纳答案by Mark Baker

Using PHPExcel

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

回答by gtrak

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. :-).

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

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.

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

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

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

回答by Pablo Alba

There is a project in sourceforge that does this conversion:

sourceforge 中有一个项目可以进行这种转换:

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

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

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

但是对于转换,您需要在 apache 中创建一个动态页面(在 python 中,php ...)

回答by bagsmode

Note: PHPExcel is now listed as DEPRECATED.

注意:PHPExcel 现在被列为DEPRECATED

Users are directed to PhpSpreadsheet.

用户被定向到PhpSpreadsheet

回答by user3785966

PHPExcel is deprecated. You must use PhpSpreadsheet.

PHPExcel 已弃用。您必须使用PhpSpreadsheet

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

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

回答by Plabon Dutta

Using PhpSpreadheet:

使用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 !!!

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