I want to read a csv file using 2D array, there will be an exception error, please help me
- private void button1_Click(object sender, EventArgs e)
- {
- //new a excel
- Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
- excel.Application.Workbooks.Add(true);
- if (openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
- {
- string theFile = openFileDialog1.FileName;
- Encoding enc = Encoding.GetEncoding("UTF-8");
- //var readText1 = File.ReadAllLines(theFile);
- var readText1 = System.IO.File.ReadAllLines(theFile);
- var readText = File.ReadAllLines(theFile).Select(l =>
- l.Split(',').ToArray()).ToArray();
- var rows = readText1.Count();
- //Number of rows columns
- var columns = readText1[0].Split(',').Count();
- //var columns = readText.Count();
- excel.Visible = true;
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- excel.Cells[i + 1, j + 1] = readText[j][i];
- //Console.WriteLine();
- }
- }
- }
- }
Mageshwaran RPosted Nov 20, 2020, 8:27 AM
Sachin SinghPosted Nov 20, 2020, 4:00 AM