我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。
由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。
当前回答
我无法在Mac Excel上找到这个问题的VBA解决方案。似乎根本没有办法输出UTF-8文本。
所以我最终不得不放弃VBA,咬紧牙关,学习了AppleScript。情况远没有我想的那么糟。
解决方案如下: http://talesoftech.blogspot.com/2011/05/excel-on-mac-goodbye-vba-hello.html
其他回答
保存对话框>工具按钮> Web选项>编码选项卡
我写了一个小的Python脚本,可以导出UTF-8格式的工作表。
您只需要提供Excel文件作为第一个参数,然后是要导出的表。如果不提供工作表,脚本将导出Excel文件中存在的所有工作表。
#!/usr/bin/env python
# export data sheets from xlsx to csv
from openpyxl import load_workbook
import csv
from os import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_all_sheets(excel_file):
sheets = []
workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
all_worksheets = workbook.get_sheet_names()
for worksheet_name in all_worksheets:
sheets.append(worksheet_name)
return sheets
def csv_from_excel(excel_file, sheets):
workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
for worksheet_name in sheets:
print("Export " + worksheet_name + " ...")
try:
worksheet = workbook.get_sheet_by_name(worksheet_name)
except KeyError:
print("Could not find " + worksheet_name)
sys.exit(1)
your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for row in worksheet.iter_rows():
lrow = []
for cell in row:
lrow.append(cell.value)
wr.writerow(lrow)
print(" ... done")
your_csv_file.close()
if not 2 <= len(sys.argv) <= 3:
print("Call with " + sys.argv[0] + " <xlxs file> [comma separated list of sheets to export]")
sys.exit(1)
else:
sheets = []
if len(sys.argv) == 3:
sheets = list(sys.argv[2].split(','))
else:
sheets = get_all_sheets(sys.argv[1])
assert(sheets != None and len(sheets) > 0)
csv_from_excel(sys.argv[1], sheets)
简单的方法: 不需要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 2013中工作得很好,除了excel 2007和2010,它是提到的。
另一个我觉得有用的例子是: “数字”允许在保存为CSV时进行编码设置。