What's the best way to export UTF8 data into Excel?

前端 未结 8 1467
不思量自难忘°
不思量自难忘° 2020-12-03 03:12

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 proble

相关标签:
8条回答
  • 2020-12-03 03:39

    Excel does not handle UTF-8 properly. You should use instead a code page that satisfy your needs

    Response.ContentType = "text/plain";
    // codepage: 28591, codepage name:iso-8859-1, codepage display name: Western European (ISO)
    Response.ContentEncoding = System.Text.Encoding.GetEncoding(28591);
    
    0 讨论(0)
  • 2020-12-03 03:43

    I had the exact same issue of sending UTF8 data to Excel. My solution:

    The current version of the Perl Spreadsheet::WriteExcel cpan code correctly writes Excel files using UTF8 data.

    So I wrote a Rails plugin that a) opens a two-way pipe to a perl program b) sends the data, a row at a time, to the perl program. I use Yaml as the message data format. (Standard Ruby yaml is not UTF8, there's a special version available, ya2yaml) c) The perl program creates the excel file d) When the Rails program indicates (via a yaml message) that the last row has been sent, the perl program creates the excel file and sends the status back to the rails program.

    Of course, adding a perl program to a rails project via a parallel process and a pipe is very much in the "Engineering" spectrum rather than "Computer Science." (It gets the job done but is not elegant.) But it does work well and saved me the weeks it would take to port the WriteExcel code to Ruby. Also note that the currently available Ruby port of WriteExcel does not handle utf8.

    My sw is permissive open source but I haven't gotten around to releasing it yet. If you want it in its current state, see http://sandbox.kluger.com/write_excel_v.5.tar

    Note that you'll want to create your excel files in a background process, not in the process of the Rails controller since that would block other browser clients as you grind away producing the excel file. I use DelayedJob plugin, works well.

    Hope this helps,

    Larry

    0 讨论(0)
提交回复
热议问题