CSV format

Loading data from a CSV (Comma-Separated Values) file is a common operation in ReoGrid, and both the workbook and individual worksheets support this functionality. Each has its unique way of handling CSV data:

Loading CSV into a Workbook

When a CSV file is loaded into a workbook in ReoGrid:

  • The CSV data populates the workbook, filling the cells with the content structured in the CSV file.
  • All existing worksheets within the workbook are removed, and the workbook is essentially reset to accommodate the newly loaded CSV data.
  • This approach is suitable when you want to start fresh with the data from a CSV file, effectively replacing any existing content in the workbook.

To load a CSV format into a workbook, use the Load method and specify the file format as CSV:

grid.Load("C:\\mypath\\data.csv", IO.FileFormat.CSV);

Because the CSV format does not support multiple worksheets, all existing worksheets in the workbook will be removed and a new worksheet will be created to load the CSV content. If the content is loaded from a file, the file name will automatically be used as the sheet name.

Loading CSV into a Worksheet

Loading a CSV file into a specific worksheet, on the other hand, behaves slightly differently:

  • The worksheet is filled with the data from the CSV file, starting from the specified starting cell or the top-left cell by default.
  • This operation does not affect other worksheets within the workbook; it only updates the content of the specific worksheet into which the CSV is loaded.
  • This method is ideal for updating or adding data to an existing worksheet without disturbing other data within the workbook.

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 = null);

// Load from path and convert string by specified encoding
sheet.LoadCSV(string path, Encoding encoding = null);

// Load from a stream by specifying autoSpread, bufferLines, and encoding
sheet.LoadCSV(Stream stream, bool autoSpread = true, int bufferLines = 10, Encoding encoding = null);

For example, to load a CSV file using the Japanese "shift-jis" encoding, you can use the following code:

sheet.LoadCSV(@"G:\ReoGrid\Samples\13TOKYO.CSV", Encoding.GetEncoding("shift-jis"));

Sample after loading: 142

Auto Spread

By default, ReoGrid automatically adds rows and columns to expand the spreadsheet to accommodate additional 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);

Exporting a Specified Range

In ReoGrid, it is possible to export a specific range of cells instead of the entire worksheet. This can be achieved by using the overloaded methods of ExportAsCSV and specifying either the grid range or row range.

// Export starting from the second row. This is useful for excluding the header, 
// which is typically the first row, from the export.
sheet.ExportAsCSV(@"C:\mypath\file.csv", 1);

// Export a specific range of cells. For example, to export cells from A1 to H30:
sheet.ExportAsCSV(@"C:\mypath\file.csv", "A1:H30");

The first example demonstrates how to start the export from the second row, which is useful when you want to exclude the first row, often used for headers. The second example shows how to export a specific range of cells, defined by the "A1:H30" range.

Exporting with Specified Encoding

When a worksheet contains Unicode text, it may be necessary to specify the encoding to ensure the exported CSV file correctly represents the text. To specify the encoding, use the third argument in the ExportAsCSV method:

// Export the range A1:H30 to a CSV file using the "shift-jis" encoding. This is useful
// for preserving special characters or language-specific text.
sheet.ExportAsCSV(@"C:\mypath\file.csv", "A1:H30", Encoding.GetEncoding("shift-jis"));

This code snippet exports the specified range "A1:H30" into a CSV file, using the "shift-jis" encoding. This approach is particularly useful for preserving the integrity of special characters or text in languages that require specific character encodings.


Was the content of the page helpful?