我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。

由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。


当前回答

您可以在Unix下使用iconv命令(也可以在Windows上作为libiconv)。

在Excel下保存为CSV后,在命令行输入:

iconv -f cp1250 -t utf-8 file-encoded-cp1250.csv > file-encoded-utf8.csv

(记住用你的编码替换cp1250)。

工作快速和伟大的大文件,如邮政编码数据库,不能导入到GoogleDocs(400.000单元格限制)。

其他回答

另一个解决方案是用winword打开文件,并将其另存为txt,然后用excel重新打开,它将工作ISA

使用notepad++打开.csv文件。如果你看到你的编码是好的(你看到所有字符,因为他们应该)按编码,然后转换为ANSI Else -找出当前的编码

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 {}

这是一种非常非常迂回的方法,但这是我发现的最可靠的方法。

另一种方法是在记事本中打开UTF-8 CSV文件,它将正确显示。 然后将所有的“,”替换为制表符。 将所有这些粘贴到一个新的excel文件中。

我也遇到过同样的问题,但有一个简单的解决方案。

在Excel 2016或更高版本中打开xlsx文件。 在“另存为”中选择此选项:"(CSV UTF-8(逗号分隔)*.csv)"

它工作完美,并生成一个csv文件,可以导入到任何软件。我在我的SQLITE数据库中导入了这个csv文件,它与所有unicode字符完好无损地完美工作。