将Excel(xls)文件转换为不带GUI的逗号分隔(csv)文件 [英] converting an Excel (xls) file to a comma separated (csv) file without the GUI
问题描述
我需要使用脚本处理一些Excel XLS工作簿,这样就可以简单的将XLS转换成CSV格式的文件。为此,我需要将xls文件转换为csv文件。这可以通过Excel应用程序的保存来完成。但是,我想自动化这个(所以,不打开Excel应用程序窗口)。
如果工作簿中的第一张表被转换为CSV格式就足够了。
我需要处理该表中的数据。
我的系统上安装了Cygwin和Excel - 如果这有帮助。
编辑:好的,我有一个工作的解决方案与Perl。更新以供未来人使用。
我安装了 Spreadsheet :: ParseExcel 模块。
,然后使用 read-excel.pl 示例。
我的代码是这个示例代码的一个微小变化,如下所示。
#! / usr / bin / perl -w
#对于文件(工作簿)中的每个选项卡(工作表),
#分出,分隔的列,
#和以c分隔的行/ R。
使用Spreadsheet :: ParseExcel;
使用strict;
我的$ filename = shift || Book1.xls的;
my $ e = new Spreadsheet :: ParseExcel;
我的$ eBook = $ e-> Parse($ filename);
我的$ sheets = $ eBook-> {SheetCount};
我的($ eSheet,$ sheetName);
foreach我的$ sheet(0 .. $ sheets - 1){
$ eSheet = $ eBook-> {Worksheet} [$ sheet];
$ sheetName = $ eSheet-> {Name};
打印#Worksheet $ sheet:$ sheetName\\\
;
next(exists($ eSheet-> {MaxRow})和(exists($ eSheet-> {MaxCol})));
foreach我的$行($ eSheet-> {MinRow} .. $ eSheet-> {MaxRow}){
foreach我的$列($ eSheet-> {MinCol} .. $ eSheet - > {MaxCol}){
if(defined $ eSheet-> {Cells} [$ row] [$ column])
{
print $ eSheet-> {Cells} [$ row] [$ column] - > Value。 ;
} else {
print,;
}
}
打印\\\
;
}
}
更新:这是一个Powershell脚本,也可能很容易使用;
as-is from 这个MSDN博客 和SO参考。
$ excel =新对象 - 对象Excel.Application
$工作簿= $ excel.Workbooks.Open(C:\test.xlsx)
$ workheets = $ workbooks.Worksheets
$ worksheet = $ worksheets.Item(1)
$ range = $ worksheet.UsedRange
foreach($ row in $ range.Rows)
{
foreach($ col in $ row.Columns)
{
echo $ col .Text
}
}
更新:我最近遇到了一个Windows工具 CSVed 此超级用户应答呃这可能对某些人有用。
使用perl脚本。使用CPAN中的Spreadsheet :: ParseExcel perl模块来解析xls文件,然后输出为csv,应该可以正常运行。
http://search.cpan.org/dist/Spreadsheet-ParseExcel
您也可以尝试使用VBScript。
Is there a simple way to translate an XLS to a CSV formatted file without starting the Excel windowed application?
I need to process some Excel XLS workbooks with scripts. For this i need to convert the xls file into a csv file. This can be done with a save-as from the Excel application. But, i would like to automate this (so, not open the Excel application window).
It will suffice if the first sheet from the workbook gets translated to the CSV format. I need to just process data in that sheet.
I have Cygwin and Excel installed on my system -- if that helps.
Edit: Ok, i have a working solution with Perl. Updating for future use by others.
I installed the Spreadsheet::ParseExcel module. and then used read-excel.pl sample.
My code is a slight variation of this sample code, as below.
#!/usr/bin/perl -w
# For each tab (worksheet) in a file (workbook),
# spit out columns separated by ",",
# and rows separated by c/r.
use Spreadsheet::ParseExcel;
use strict;
my $filename = shift || "Book1.xls";
my $e = new Spreadsheet::ParseExcel;
my $eBook = $e->Parse($filename);
my $sheets = $eBook->{SheetCount};
my ($eSheet, $sheetName);
foreach my $sheet (0 .. $sheets - 1) {
$eSheet = $eBook->{Worksheet}[$sheet];
$sheetName = $eSheet->{Name};
print "#Worksheet $sheet: $sheetName\n";
next unless (exists ($eSheet->{MaxRow}) and (exists ($eSheet->{MaxCol})));
foreach my $row ($eSheet->{MinRow} .. $eSheet->{MaxRow}) {
foreach my $column ($eSheet->{MinCol} .. $eSheet->{MaxCol}) {
if (defined $eSheet->{Cells}[$row][$column])
{
print $eSheet->{Cells}[$row][$column]->Value . ",";
} else {
print ",";
}
}
print "\n";
}
}
Update: Here is a Powershell script that might also be easy to work with; as-is from this MSDN blog and, SO Reference.
$excel = New-Object -comobject Excel.Application
$workbooks = $excel.Workbooks.Open("C:\test.xlsx")
$worksheets = $workbooks.Worksheets
$worksheet = $worksheets.Item(1)
$range = $worksheet.UsedRange
foreach($row in $range.Rows)
{
foreach($col in $row.Columns)
{
echo $col.Text
}
}
Update: I recently came across a Windows tool CSVed at this Superuser answer which might be useful to some people.
Use a perl script. Using the Spreadsheet::ParseExcel perl module from CPAN to parse the xls file followed by output as csv should work fine.
http://search.cpan.org/dist/Spreadsheet-ParseExcel
You could also try using VBScript.
这篇关于将Excel(xls)文件转换为不带GUI的逗号分隔(csv)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!