SpreadJS Documentation > Developer's Guide > Managing Data > Importing and Exporting CSV Files |
You can save or load data to comma-separated value (.csv) files. You can also specify whether to load unformatted text or headers when loading a comma-separated file.
Use the getCsv file method to save the data to a comma-separated file and the setCsv file method to load the data. Use the TextFileOpenFlags type to specify the import options.
The following code saves and loads a CSV file. Select button 1 to save and button 2 to load the file. Change sheets to see the results.
JavaScript |
Copy Code
|
---|---|
var sheet1 = spread.getSheet(0); sheet1.setRowCount(3); sheet1.setColumnCount(5); sheet1.setColumnHeaderAutoText(GcSpread.Sheets.HeaderAutoText.blank); sheet1.setRowHeaderAutoText(GcSpread.Sheets.HeaderAutoText.blank); sheet1.setText(0, 0, "Row1", GcSpread.Sheets.SheetArea.rowHeader); sheet1.setText(1, 0, "Row2", GcSpread.Sheets.SheetArea.rowHeader); sheet1.setText(2, 0, "Row3", GcSpread.Sheets.SheetArea.rowHeader); sheet1.setText(0, 0, "Column1", GcSpread.Sheets.SheetArea.colHeader); sheet1.setText(0, 1, "Column2", GcSpread.Sheets.SheetArea.colHeader); sheet1.setText(0, 2, "Column3", GcSpread.Sheets.SheetArea.colHeader); sheet1.setText(0, 3, "Column4", GcSpread.Sheets.SheetArea.colHeader); sheet1.setText(0, 4, "Column5", GcSpread.Sheets.SheetArea.colHeader); sheet1.setText(0, 0, "SPREAD-1"); sheet1.setText(1, 0, "SPREAD-2"); sheet1.setText(2, 0, "SPREAD-3"); sheet1.setValue(0, 1, new Date().setFullYear(2006, 8, 1)); sheet1.setValue(1, 1, new Date().setFullYear(2006, 8, 2)); sheet1.setValue(2, 1, new Date().setFullYear(2006, 8, 3)); sheet1.setColumnWidth(1, 90); sheet1.setValue(0, 2, 123.45); sheet1.setValue(1, 2, 99.9999); sheet1.setValue(2, 2, 100); sheet1.setValue(0, 3, true); sheet1.setValue(1, 3, false); sheet1.setValue(2, 3, true); sheet1.setValue(0, 4, "aaa"); sheet1.setValue(1, 4, "bbb"); sheet1.setValue(2, 4, "ccc"); $("#button1").click(function(){ //Save all data on the first sheet in a csv format. window.CSVString = spread.getSheet(0).getCsv(0, 0, spread.getSheet(0).getRowCount(), spread.getSheet(0).getColumnCount(), "\r", ","); }); $("#button2").click(function(){ //Load the saved csv file on the second sheet. spread.getSheet(1).setCsv(0, 0, window.CSVString, "\r", ",", GcSpread.Sheets.TextFileOpenFlags.None); }); //Add button controls to page <input type="button" id="button1" value="button1"/> <input type="button" id="button2" value="button2"/> |