MESCIUS SPREAD for Windows Forms 17.0J サンプルコード集 > インポート/エクスポート > インポート/エクスポート : csv(テキスト)ファイルの保存/ロード |
SPREADではxls形式ファイルやxml形式ファイルの入出力処理をサポートしていますが、基本的にこれらの機能ではセルのデータだけではなく各スタイル情報も対象となっています。そのため、広範囲に渡るセルに対して各属性(表示色、罫線、フォントなど)の設定が行われている場合にはある程度の処理時間を要することも考えられます。
そのため、シート上のデータのみを保存したいような場合は、SheetViewクラスのSaveTextFileメソッド(オーバーロード形式)呼び出しを行います。これによって指定したシート上の全データをcsv(テキスト)形式ファイルに保存することができます。また保存済みファイルのロードについては、同様にSheetViewクラスのLoadTextFileメソッド(オーバーロード形式)呼び出しを行います。
|
private void Form1_Load(object sender, System.EventArgs e) { fpSpread1.ActiveSheet.RowCount = 3; fpSpread1.ActiveSheet.ColumnCount = 5; fpSpread1.ActiveSheet.Rows[0].Label = "行1"; fpSpread1.ActiveSheet.Rows[1].Label = "行2"; fpSpread1.ActiveSheet.Rows[2].Label = "行3"; fpSpread1.ActiveSheet.Columns[0].Label = "列1"; fpSpread1.ActiveSheet.Columns[1].Label = "列2"; fpSpread1.ActiveSheet.Columns[2].Label = "列3"; fpSpread1.ActiveSheet.Columns[3].Label = "列4"; fpSpread1.ActiveSheet.Columns[4].Label = "列5"; fpSpread1.ActiveSheet.Columns[0].CellType = new FarPoint.Win.Spread.CellType.TextCellType(); fpSpread1.ActiveSheet.SetText(0, 0, "SPREAD-1"); fpSpread1.ActiveSheet.SetText(1, 0, "SPREAD-2"); fpSpread1.ActiveSheet.SetText(2, 0, "SPREAD-3"); fpSpread1.ActiveSheet.Columns[1].CellType = new FarPoint.Win.Spread.CellType.DateTimeCellType(); fpSpread1.ActiveSheet.SetValue(0, 1, new DateTime(2015, 6, 15)); fpSpread1.ActiveSheet.SetValue(1, 1, new DateTime(2015, 6, 16)); fpSpread1.ActiveSheet.SetValue(2, 1, new DateTime(2015, 6, 17)); fpSpread1.ActiveSheet.SetColumnWidth(1, 90); FarPoint.Win.Spread.CellType.NumberCellType n = new FarPoint.Win.Spread.CellType.NumberCellType(); n.DecimalPlaces = 4; fpSpread1.ActiveSheet.Columns[2].CellType = n; fpSpread1.ActiveSheet.SetValue(0, 2, 123.45); fpSpread1.ActiveSheet.SetValue(1, 2, 99.9999); fpSpread1.ActiveSheet.SetValue(2, 2, 100); fpSpread1.ActiveSheet.Columns[3].CellType = new FarPoint.Win.Spread.CellType.CheckBoxCellType(); fpSpread1.ActiveSheet.SetValue(0, 3, true); fpSpread1.ActiveSheet.SetValue(1, 3, false); fpSpread1.ActiveSheet.SetValue(2, 3, true); FarPoint.Win.Spread.CellType.ComboBoxCellType c = new FarPoint.Win.Spread.CellType.ComboBoxCellType(); c.Items = new String[] {"aaa", "bbb", "ccc"}; c.EditorValue = FarPoint.Win.Spread.CellType.EditorValue.String; c.Editable = true; fpSpread1.ActiveSheet.Columns[4].CellType = c; fpSpread1.ActiveSheet.SetValue(0, 4, "aaa"); fpSpread1.ActiveSheet.SetValue(1, 4, "bbb"); fpSpread1.ActiveSheet.SetValue(2, 4, "ccc"); } private void button1_Click(object sender, System.EventArgs e) { //アクティブシート上の全データをcsv形式ファイルに保存します fpSpread1.ActiveSheet.SaveTextFile("保存ファイルパス名.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, System.Environment.NewLine, ",", ""); } private void button2_Click(object sender, System.EventArgs e) { //保存済みcsv形式ファイルをアクティブシートにロードします fpSpread1.ActiveSheet.LoadTextFile("保存ファイルパス名.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, System.Environment.NewLine, ",", ""); }
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load FpSpread1.ActiveSheet.RowCount = 3 FpSpread1.ActiveSheet.ColumnCount = 5 FpSpread1.ActiveSheet.Rows(0).Label = "行1" FpSpread1.ActiveSheet.Rows(1).Label = "行2" FpSpread1.ActiveSheet.Rows(2).Label = "行3" FpSpread1.ActiveSheet.Columns(0).Label = "列1" FpSpread1.ActiveSheet.Columns(1).Label = "列2" FpSpread1.ActiveSheet.Columns(2).Label = "列3" FpSpread1.ActiveSheet.Columns(3).Label = "列4" FpSpread1.ActiveSheet.Columns(4).Label = "列5" FpSpread1.ActiveSheet.Columns(0).CellType = New FarPoint.Win.Spread.CellType.TextCellType FpSpread1.ActiveSheet.SetText(0, 0, "SPREAD-1") FpSpread1.ActiveSheet.SetText(1, 0, "SPREAD-2") FpSpread1.ActiveSheet.SetText(2, 0, "SPREAD-3") FpSpread1.ActiveSheet.Columns(1).CellType = New FarPoint.Win.Spread.CellType.DateTimeCellType FpSpread1.ActiveSheet.SetValue(0, 1, New DateTime(2015, 6, 15)) FpSpread1.ActiveSheet.SetValue(1, 1, New DateTime(2015, 6, 16)) FpSpread1.ActiveSheet.SetValue(2, 1, New DateTime(2015, 6, 17)) FpSpread1.ActiveSheet.SetColumnWidth(1, 90) Dim n As New FarPoint.Win.Spread.CellType.NumberCellType n.DecimalPlaces = 4 FpSpread1.ActiveSheet.Columns(2).CellType = n FpSpread1.ActiveSheet.SetValue(0, 2, 123.45) FpSpread1.ActiveSheet.SetValue(1, 2, 99.9999) FpSpread1.ActiveSheet.SetValue(2, 2, 100) FpSpread1.ActiveSheet.Columns(3).CellType = New FarPoint.Win.Spread.CellType.CheckBoxCellType FpSpread1.ActiveSheet.SetValue(0, 3, True) FpSpread1.ActiveSheet.SetValue(1, 3, False) FpSpread1.ActiveSheet.SetValue(2, 3, True) Dim c As New FarPoint.Win.Spread.CellType.ComboBoxCellType c.Items = New String() {"aaa", "bbb", "ccc"} c.EditorValue = FarPoint.Win.Spread.CellType.EditorValue.String c.Editable = True FpSpread1.ActiveSheet.Columns(4).CellType = c FpSpread1.ActiveSheet.SetValue(0, 4, "aaa") FpSpread1.ActiveSheet.SetValue(1, 4, "bbb") FpSpread1.ActiveSheet.SetValue(2, 4, "ccc") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'アクティブシート上の全データをcsv形式ファイルに保存します FpSpread1.ActiveSheet.SaveTextFile("保存ファイルパス名.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, System.Environment.NewLine, ",", "") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '保存済みcsv形式ファイルをアクティブシートにロードします FpSpread1.ActiveSheet.LoadTextFile("保存ファイルパス名.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, System.Environment.NewLine, ",", "") End Sub