问题
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