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)
- {
-
- 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 = System.IO.File.ReadAllLines(theFile);
- var readText = File.ReadAllLines(theFile).Select(l =>
- l.Split(',').ToArray()).ToArray();
- var rows = readText1.Count();
-
- var columns = readText1[0].Split(',').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];
-
- }
- }
- }
- }