我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
当前回答
做到这一点的唯一“简单方法”如下。首先,要意识到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 {}
这是一种非常非常迂回的方法,但这是我发现的最可靠的方法。
一个简单的解决方法是使用谷歌电子表格。粘贴(只有当您有复杂公式时才使用值)或导入工作表,然后下载CSV。我只是试了几个字符,效果相当不错。
注意:谷歌表在导入时有限制。在这里看到的。
注意:小心使用谷歌表的敏感数据。
编辑:另一种选择-基本上他们使用VB宏或插件强制保存为UTF8。我没有尝试过这些解决方案,但它们听起来很合理。
简单的方法: 不需要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,则使用逗号分隔。
我发现OpenOffice的电子表格应用程序Calc非常擅长处理CSV数据。
在“另存为…”对话框中,单击“格式选项”可获得CSV的不同编码。LibreOffice的工作原理与AFAIK相同。
(在Mac上:)从Excel保存为CSV文件。在TextWrangler中打开CSV文件(它是免费的),并使用“另存为”。在保存对话框中选择Unicode (UTF-8)。完成
(我猜你也可以用TextEdit做到这一点-如果你玩打开和保存设置。尝试打开文件:自动,保存文件:UTF-8)