1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
//
using System.IO;
namespace csv_dataGrid
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(540, 327);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(251, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(145, 24);
this.button1.TabIndex = 2;
this.button1.Text = "Importer_Excel";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(142, 24);
this.button2.TabIndex = 3;
this.button2.Text = "Importer_CSV";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(594, 379);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// on cree une table
DataTable dt = new DataTable("test");
// on cree les colonnes
dt.Columns.Add("t1",System.Type.GetType("System.String"));
dt.Columns.Add("i1",System.Type.GetType("System.Int32"));
dt.Columns.Add("t2",System.Type.GetType("System.String"));
// on insert une nvelle ligne
DataRow dr = dt.NewRow();
dr["t1"] = "test01";
dr["i1"] = 10;
dr["t2"] = "test02";
// on ajoute la ligne
dt.Rows.Add(dr);
// on affiche la table
dataGrid1.DataSource = dt;
}
private void button1_Click(object sender, System.EventArgs e)
{
// on cree une table
DataTable dt = new DataTable("test");
// on cree les colonnes
dt.Columns.Add("t1", System.Type.GetType("System.String"));
dt.Columns.Add("t2", System.Type.GetType("System.String"));
dt.Columns.Add("t3", System.Type.GetType("System.String"));
// on lit et on insert une nvelle ligne
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str1, str2, str3;
int rCnt = 0;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("C:/Documents and Settings/zakaria/Bureau/lecture-fichier-csv-et-affichage-dans-un-datagrid/bin/Debug/fichier.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
int nbr_xlWorkSheet = xlWorkBook.Worksheets.Count;
for (int k = 1; k <= nbr_xlWorkSheet; k++)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(k);
range = xlWorkSheet.UsedRange;
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
str1 = (string)(range.Cells[rCnt, 1] as Excel.Range).Value2;
str2 = (string)(range.Cells[rCnt, 2] as Excel.Range).Value2;
str3 = (string)(range.Cells[rCnt, 3] as Excel.Range).Value2;
DataRow dr = dt.NewRow();
dr["t1"] = str1;
dr["t2"] = str2;
dr["t3"] = str3;
// on ajoute la ligne
dt.Rows.Add(dr);
}
dataGrid1.DataSource = dt;
releaseObject(xlWorkSheet);
}
xlWorkBook.Close(false, null, null);
xlApp.Quit();
//releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
// on cree une table
DataTable dt = new DataTable("test");
// on cree les colonnes
dt.Columns.Add("t1",System.Type.GetType("System.String"));
// on lit et on insert une nvelle ligne
StreamReader fichier = File.OpenText("fichier.csv");
while(fichier.Peek() >= 0)
{
// on lit 1 ligne et on ajoute
string ligne = fichier.ReadLine();
string[] vals = ligne.Split(';');
DataRow dr = dt.NewRow();
dr["t1"] = ligne.ToString();
// on ajoute la ligne
dt.Rows.Add(dr);
}
fichier.Close();
// on genere le tableau
dataGrid1.DataSource = dt;
}
private void button3_Click(object sender, EventArgs e)
{
dataGrid1.Controls.Clear();
}
}
} |
Partager