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

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


当前回答

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

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

在Excel 2016及更高版本(包括Office 365)中,有一个专门用于UTF-8格式的CSV选项。

在Office 365中,选择另存为;以前人们可能会选择CSV(逗号分隔),现在你可以保存为CSV UTF-8(逗号分隔)(*.csv)

我知道这是一个老问题,但我碰巧在与OP同样的问题作斗争时遇到了这个问题。

没有发现任何提供的解决方案是可行的选择,我开始探索是否有一种方法可以只使用Excel来做到这一点。

幸运的是,我发现丢失字符的问题只发生(在我的情况下)从xlsx格式保存到csv格式。我尝试先将xlsx文件保存到xls,然后再保存到csv。它确实起作用了。

请试一试,看看对你是否有用。祝你好运。

Encoding -> Convert to Ansi将其编码为Ansi /UNICODE。Utf8是Unicode的一个子集。也许在ANSI中会正确编码,但在这里我们谈论的是UTF8, @SequenceDigitale。

还有更快的方法,比如导出为csv(逗号分隔),然后用notepad++(免费)打开csv,然后Encoding > Convert to UTF8。但前提是每个文件必须执行一次。如果你需要经常更改和导出,那么最好是LibreOffice或GDocs解决方案。

我无法在Mac Excel上找到这个问题的VBA解决方案。似乎根本没有办法输出UTF-8文本。

所以我最终不得不放弃VBA,咬紧牙关,学习了AppleScript。情况远没有我想的那么糟。

解决方案如下: http://talesoftech.blogspot.com/2011/05/excel-on-mac-goodbye-vba-hello.html