Loading CSV format
Workbook vs. Worksheet
Both workbook and worksheet have the ability to load data from CSV format. Workbook will load the content from a CSV format and remove all other existed worksheets; Worksheet loads content from CSV format only fill itself.
Loading CSV into workbook
To load a CSV format into a workbook, use the Load method and pass the file format as CSV:
grid.Load("C:\\mypath\\data.csv", IO.FileFormat.CSV);
Loading CSV into worksheet
To load a CSV format into worksheet, use the following methods:
// load from stream sheet.LoadCSV(Stream s); // load from file sheet.LoadCSV(string path); // load from stream and convert string by specified encoding sheet.LoadCSV(Stream s, Encoding encoding); // load from path and convert string by specified encoding sheet.LoadCSV(string path, Encoding encoding); // load from stream by specifying autoSpread, bufferLines and encoding sheet.LoadCSV(Stream s, bool autoSpread, int bufferLines, Encoding encoding);
For example: load from file by using Japanese encoding “shift-jis”
sheet.LoadCSV(@"G:\ReoGrid\Samples\13TOKYO.CSV", Encoding.GetEncoding("shift-jis"));
Sample after loading:
Auto Spread
By default, ReoGrid will add rows and columns automatically in order to expending the spreadsheet to load more data, to disable this behavior, set the autoSpread
argument to false.
Export as CSV Format
To save worksheet as CSV format, use the following methods:
var sheet = grid.CurrentWorksheet; // export to file sheet.ExportAsCSV(string path); // export to stream sheet.ExportAsCSV(Stream s);
Export only specified range
It’s possible to export a specified range rather than entire worksheet, use the overload methods of ExportAsCSV and specifying the the grid or row range.
// export from second row, this is useful if do not // want export the header that is the first row sheet.ExportAsCSV("C:\\mypath\\file.csv", 1); // export a range sheet.ExportAsCSV("C:\\mypath\\file.csv", "A1:H30");
Export by specified encoding
Sometime it is necessary to specify the encoding if worksheet contains Unicode text, to specify the encoding, use the third argument of these methods:
sheet.ExportAsCSV("C:\\mypath\\file.csv", "A1:H30", Encoding.GetEncoding("shift-jis"));
Hi,
is there a way to set the separation mark? In some country the comma is used as decimal separator, so the cell separator in csv files become “;”.
This sounds a nice improvement and would be implemented.
Track this issue at https://github.com/unvell/ReoGrid/issues/65