我有一个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 {}
这是一种非常非常迂回的方法,但这是我发现的最可靠的方法。
其他回答
我知道这是一个老问题,但我碰巧在与OP同样的问题作斗争时遇到了这个问题。
没有发现任何提供的解决方案是可行的选择,我开始探索是否有一种方法可以只使用Excel来做到这一点。
幸运的是,我发现丢失字符的问题只发生(在我的情况下)从xlsx格式保存到csv格式。我尝试先将xlsx文件保存到xls,然后再保存到csv。它确实起作用了。
请试一试,看看对你是否有用。祝你好运。
简单的方法: 不需要Open office和谷歌文档
Save your file as "Unicode text file"; now you have an unicode text file open it with "notepad" and "Save as" it with selecting "utf-8" or other code page that you want rename file extension from "txt" to "csv". This will result in a tab-delimited UTF-8 csv file. If you want a comma-delimited file, open the csv file you just renamed and replace all tabs with commas. To do this in Notepad on Win 10, simply select one tab field then click Ctrl+H. In the window that opens, type a comma , in the "Replace with" field then click "Replace All". Save your file. The result will be a comma-delimited UTF-8 csv file.
不管怎样,不要用ms office打开它!! 现在您有了一个以制表符分隔的CSV文件。 或者,如果应用步骤5,则使用逗号分隔。
在Excel 2016及更高版本(包括Office 365)中,有一个专门用于UTF-8格式的CSV选项。
在Office 365中,选择另存为;以前人们可能会选择CSV(逗号分隔),现在你可以保存为CSV UTF-8(逗号分隔)(*.csv)
另一个解决方案是用winword打开文件,并将其另存为txt,然后用excel重新打开,它将工作ISA
“nevets1219”是正确的谷歌文档,然而,如果你只是“导入”文件,它通常不会将其转换为UTF-8。
但是如果您将CSV导入到现有的谷歌电子表格中,它会转换为UTF-8。
下面是一个食谱:
在主文档(或驱动器)屏幕上点击“创建”按钮并选择“电子表格” 在“文件”菜单中选择“导入” 按“选择档案” 选择“替换电子表格” 选择要用作分隔符的字符 点击“导入” 从“文件”菜单选择“下载为”-> CSV(当前文件)
生成的文件将是UTF-8格式的