将UTF8数据导出到Excel中的最佳方法是什么?

问题描述:

所以我们有这个网络应用程序,我们支持UTF8数据。 Hooray UTF8。我们可以将用户提供的数据导出到CSV没有问题 - 它仍然在UTF8在那一点。问题是当你在Excel中打开一个典型的UTF8 CSV,它读取它作为ANSII编码的文本,因此试图读取ø和ü两个字节的字符作为两个单独的字符,你最后失败。

So we have this web app where we support UTF8 data. Hooray UTF8. And we can export the user-supplied data into CSV no problem - it's still in UTF8 at that point. The problem is when you open a typical UTF8 CSV up in Excel, it reads it as ANSII encoded text, and accordingly tries to read two-byte chars like ø and ü as two separate characters and you end up with fail.

所以我做了一些挖掘(Intervals的人有一个有趣的帖子在这里),并有一些有限的,如果可笑的恼人的选择。其中:

So I've done a bit of digging (the Intervals folks have a interesting post about it here), and there are some limited if ridiculously annoying options out there. Among them:


  • 提供一个UTF-16 Little Endian TSV文件,Excel将正确解释,但不支持多行数据

  • 在HTML表格中提供Excel mime类型或文件扩展名的数据(不确定此选项是否支持UTF8)

  • 三种或四种方式来获取XML数据到各种最新版本的excel,并且那些将支持UTF8,在理论上。 SpreadsheetML,使用自定义XSLT,或通过模板生成新的Excel XML格式。

看起来像什么,将继续为仍未为Excel使用的用户提供一个原始的CSV文件,并为Excel提供单独的下载选项。

It looks like no matter what, I'm probably going to want to continue offering a plain-old CSV file for the folks who aren't using it for Excel anyway, and a separate download option for Excel.

最简单的生成Just-For-Excel文件的方法是什么,它将正确支持UTF8,我亲爱的Stack Overflowers?如果这个最简单的选项只支持最新版本的Excel,仍然感兴趣。

What's the simplest way of generating that Just-For-Excel file that will correctly support UTF8, my dear Stack Overflowers? If that simplest option only supports the latest version of Excel, that's still of interest.

我在Rails堆栈上做这个,但是好奇的是.Net-和任何框架的人处理这一点。

I'm doing this on a Rails stack, but curious how the .Net-ers and folks on any frameworks handle this. I work in a few different environments myself and this is definitely an issue that will becoming up again.

更新2010-10-22:我们会在几个不同的环境下工作,已在我们的时间跟踪系统速度中使用Ruport gem,以便在我首次发布此问题时提供CSV导出。我的同事之一Erik Hollensbee一起为Ruport提供了一个快速过滤器,为我们提供了实际的Excel XSL输出,我想我会在这里分享任何其他ruby-ists:

Update 2010-10-22: We had been using the Ruport gem in our time-tracking system Tempo to provide the CSV exports when I first posted this question. One of my coworkers, Erik Hollensbee, threw together a quick filter for Ruport to provide us with actual Excel XSL output, and I figured I'd share that here for any other ruby-ists:

require 'rubygems'
require 'ruport'
require 'spreadsheet'
require 'stringio'

Spreadsheet.client_encoding = "UTF-8"

include Ruport::Data

class Ruport::Formatter::Excel < Ruport::Formatter
  renders :excel, :for => Ruport::Controller::Table

  def output
    retval = StringIO.new

    if options.workbook
      book = options.workbook
    else
      book = Spreadsheet::Workbook.new
    end

    if options.worksheet_name
      book_args = { :name => options.worksheet_name }
    else
      book_args = { }
    end

    sheet = book.create_worksheet(book_args)

    offset = 0

    if options.show_table_headers
      sheet.row(0).default_format = Spreadsheet::Format.new(
        options.format_options || 
        { 
          :color => :blue,
          :weight => :bold,
          :size => 18
        }
      )
      sheet.row(0).replace data.column_names
      offset = 1
    end

    data.data.each_with_index do |row, i|
      sheet.row(i+offset).replace row.attributes.map { |x| row.data[x] }
    end

    book.write retval
    retval.seek(0)
    return retval.read
  end
end


您忘记创建OleDB数据源Excel Interop,但这些也有问题。

You're forgetting creating an OleDB datasource and Excel Interop, but there are issues with those as well.

我推荐 SpreadsheetML 选项。它的工作原理很好,赔率是你的平台有一些体面的工具,用于构建xml文件,它完全支持作为OfficeXP。 Office2000不受支持,但个人经验是,它的工作有限的方式。

I recommend the SpreadsheetML option. It works pretty well, odds are your platform has some decent tools for building xml files, and it's fully supported as far back as OfficeXP. Office2000 is not supported, but personal experience is that it works in a limited way.