我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
当前回答
I needed to automate this process on my Mac. I originally tried using catdoc/xls2csv as suggested by mpowered, but xls2csv had trouble detecting the original encoding of the document and not all documents were the same. What I ended up doing was setting the default webpage output encoding to be UTF-8 and then providing the files to Apple's Automator, applying the Convert Format of Excel Files action to convert to Web Page (HTML). Then using PHP, DOMDocument and XPath, I queried the documents and formatted them to CSV.
这是PHP脚本(process.php):
<?php
$pi = pathinfo($argv[1]);
$file = $pi['dirname'] . '/' . $pi['filename'] . '.csv';
$fp = fopen($file,'w+');
$doc = new DOMDocument;
$doc->loadHTMLFile($argv[1]);
$xpath = new DOMXPath($doc);
$table = [];
foreach($xpath->query('//tr') as $row){
$_r = [];
foreach($xpath->query('td',$row) as $col){
$_r[] = trim($col->textContent);
}
fputcsv($fp,$_r);
}
fclose($fp);
?>
这是我用来将HTML文档转换为csv的shell命令:
find . -name '*.htm' | xargs -I{} php ./process.php {}
这是一种非常非常迂回的方法,但这是我发现的最可靠的方法。
其他回答
我无法在Mac Excel上找到这个问题的VBA解决方案。似乎根本没有办法输出UTF-8文本。
所以我最终不得不放弃VBA,咬紧牙关,学习了AppleScript。情况远没有我想的那么糟。
解决方案如下: http://talesoftech.blogspot.com/2011/05/excel-on-mac-goodbye-vba-hello.html
“nevets1219”是正确的谷歌文档,然而,如果你只是“导入”文件,它通常不会将其转换为UTF-8。
但是如果您将CSV导入到现有的谷歌电子表格中,它会转换为UTF-8。
下面是一个食谱:
在主文档(或驱动器)屏幕上点击“创建”按钮并选择“电子表格” 在“文件”菜单中选择“导入” 按“选择档案” 选择“替换电子表格” 选择要用作分隔符的字符 点击“导入” 从“文件”菜单选择“下载为”-> CSV(当前文件)
生成的文件将是UTF-8格式的
我写了一个小的Python脚本,可以导出UTF-8格式的工作表。
您只需要提供Excel文件作为第一个参数,然后是要导出的表。如果不提供工作表,脚本将导出Excel文件中存在的所有工作表。
#!/usr/bin/env python
# export data sheets from xlsx to csv
from openpyxl import load_workbook
import csv
from os import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_all_sheets(excel_file):
sheets = []
workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
all_worksheets = workbook.get_sheet_names()
for worksheet_name in all_worksheets:
sheets.append(worksheet_name)
return sheets
def csv_from_excel(excel_file, sheets):
workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
for worksheet_name in sheets:
print("Export " + worksheet_name + " ...")
try:
worksheet = workbook.get_sheet_by_name(worksheet_name)
except KeyError:
print("Could not find " + worksheet_name)
sys.exit(1)
your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for row in worksheet.iter_rows():
lrow = []
for cell in row:
lrow.append(cell.value)
wr.writerow(lrow)
print(" ... done")
your_csv_file.close()
if not 2 <= len(sys.argv) <= 3:
print("Call with " + sys.argv[0] + " <xlxs file> [comma separated list of sheets to export]")
sys.exit(1)
else:
sheets = []
if len(sys.argv) == 3:
sheets = list(sys.argv[2].split(','))
else:
sheets = get_all_sheets(sys.argv[1])
assert(sheets != None and len(sheets) > 0)
csv_from_excel(sys.argv[1], sheets)
你可以在没有第三方软件的现代Windows机器上做到这一点。这种方法是可靠的,它可以处理包含引号逗号、引号制表符、CJK字符等的数据。
1. 从Excel中保存
在Excel中,使用Unicode文本(*.txt)类型将数据保存到file.txt。
2. 开始PowerShell
从开始菜单运行powershell。
3.在PowerShell中加载文件
$data = Import-Csv C:\path\to\file.txt -Delimiter "`t" -Encoding BigEndianUnicode
4. 将数据保存为CSV格式
$data | Export-Csv file.csv -Encoding UTF8 -NoTypeInformation
(在Mac上:)从Excel保存为CSV文件。在TextWrangler中打开CSV文件(它是免费的),并使用“另存为”。在保存对话框中选择Unicode (UTF-8)。完成
(我猜你也可以用TextEdit做到这一点-如果你玩打开和保存设置。尝试打开文件:自动,保存文件:UTF-8)