Is there any way to specify the encoding used in SpreadsheetGear to generate CSV files?

自作多情 提交于 2021-02-08 06:39:39

问题


I am trying to export data containing Unicode characters from our system using Spreadsheet Gear to csv format. (Fine for excel).

However because the CSV format is not UTF-8 encoded all the Unicode characters are exported as ???

I am aware that Spreadsheet Gear supports Unicode by having a tab-delimited UTF-8 text file, however we require the comma-delimited file.

This is what currently exists (including my check that the Unicode Text file format exports the characters correctly):

public static void ExportToStream(Stream stream, IDataSource data, IEnumerable<ColumnInfo> columns, ExportFormat format)
    {
        var exporter = new ExportCreator
        {
            Data = data,
            Columns = columns.ToArray(),
            EscapeFormulas = format == ExportFormat.Xlsx
        };
        var workbook = exporter.GetWorkBookForExport();
        switch (format)
        {
            //need to put something in here to enable the csv to be encoded as utf-8 - 
            //currently spreadsheet gear only supports utf-8 encoded tab delimited text file
            case ExportFormat.Csv:
                workbook.SaveToStream(stream, FileFormat.UnicodeText);
                break;
            case ExportFormat.Xlsx:
                workbook.SaveToStream(stream, FileFormat.OpenXMLWorkbook);
                break;
        }
    }

As far as I can tell from trawling the Spreadsheet Gear documentation and SO, UnicodeText format would solve the problem but doesn't meet our requirements for the CSV file format.

Is there a way of specifying the encoding of the stream so the CSV File format is saved as UTF-8 encoded?


回答1:


SpreadsheetGear provides no option to specify the encoding of a CSV file, or option to specify what delimiter is used for a given text-based data file. If you have a CSV encoded with UTF, you would need to build your own routine that reads and handles this file accordingly, then manually insert that data into your worksheet cell-by-cell.



来源:https://stackoverflow.com/questions/29212241/is-there-any-way-to-specify-the-encoding-used-in-spreadsheetgear-to-generate-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!