我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
当前回答
“nevets1219”是正确的谷歌文档,然而,如果你只是“导入”文件,它通常不会将其转换为UTF-8。
但是如果您将CSV导入到现有的谷歌电子表格中,它会转换为UTF-8。
下面是一个食谱:
在主文档(或驱动器)屏幕上点击“创建”按钮并选择“电子表格” 在“文件”菜单中选择“导入” 按“选择档案” 选择“替换电子表格” 选择要用作分隔符的字符 点击“导入” 从“文件”菜单选择“下载为”-> CSV(当前文件)
生成的文件将是UTF-8格式的
其他回答
做到这一点的唯一“简单方法”如下。首先,要意识到Excel .csv文件中显示的内容和隐藏的内容之间是有区别的。
Open an Excel file where you have the info (.xls, .xlsx) In Excel, choose "CSV (Comma Delimited) (*.csv) as the file type and save as that type. In NOTEPAD (found under "Programs" and then Accessories in Start menu), open the saved .csv file in Notepad Then choose -> Save As... and at the bottom of the "save as" box, there is a select box labelled as "Encoding". Select UTF-8 (do NOT use ANSI or you lose all accents etc). After selecting UTF-8, then save the file to a slightly different file name from the original.
该文件采用UTF-8格式,保留所有字符和重音,可以导入,例如,MySQL和其他数据库程序。
这个答案来自这个论坛。
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 {}
这是一种非常非常迂回的方法,但这是我发现的最可靠的方法。
使用notepad++打开.csv文件。如果你看到你的编码是好的(你看到所有字符,因为他们应该)按编码,然后转换为ANSI Else -找出当前的编码
“nevets1219”是正确的谷歌文档,然而,如果你只是“导入”文件,它通常不会将其转换为UTF-8。
但是如果您将CSV导入到现有的谷歌电子表格中,它会转换为UTF-8。
下面是一个食谱:
在主文档(或驱动器)屏幕上点击“创建”按钮并选择“电子表格” 在“文件”菜单中选择“导入” 按“选择档案” 选择“替换电子表格” 选择要用作分隔符的字符 点击“导入” 从“文件”菜单选择“下载为”-> CSV(当前文件)
生成的文件将是UTF-8格式的
用记事本+
这将修复Excel保存的损坏的CSV文件,并以适当的编码重新保存它。
从Excel导出CSV 加载到notepad++ 解决编码 保存
Excel保存在CP-1252 / Windows-1252中。在notepad++中打开CSV文件。选择
Encoding > Character Sets > Western European > Windows-1252
Then
Encoding > Convert to UTF-8
File > Save
首先告诉notepad++编码,然后转换。有些其他答案在转换时没有先设置正确的编码,这会使文件更加混乱。他们会把原本应该是“達”的东西变成“達”。如果您的字符不符合CP-1252,那么它在保存为CSV时就已经丢失了。用另一个答案。