private void btnLoadData_Click(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { Microsoft.Office.Interop.Excel.Application excelApp; Microsoft.Office.Interop.Excel.Workbook workbook; Microsoft.Office.Interop.Excel.Worksheet worksheet; Microsoft.Office.Interop.Excel.Range range; excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); // Opening Excel file workbook = excelApp.Workbooks.Open(txtPath.Text, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1); range = excelApp.get_Range("A1", Type.Missing); if (range != null) { var val = range.Value2; var sourceString= val.ToString().Trim(); var remove="Period"; var a = sourceString.IndexOf(remove); var b = string.Concat(sourceString.Substring(0, a), sourceString.Substring(a + remove.Length)); var c = b.ToString().TrimStart(); var remove1 = ":"; var d = c.IndexOf(remove1); var f = string.Concat(c.Substring(0, d), c.Substring(d + remove1.Length)); var g = f.ToString().TrimStart(); var remove2 = "to"; var h = g.IndexOf(remove2); var i = string.Concat(g.Substring(0, h), g.Substring(h + remove2.Length)); var j = i.ToString().TrimStart(); string sub = j.Substring(0,10); string sub1 = j.Substring(12,10); textEdit1.Text += Convert.ToString(sub).Trim(); textEdit3.Text += Convert.ToString(sub1).Trim(); } range = excelApp.get_Range("A2", Type.Missing); if (range != null) { var val1 = range.Value2; var sourceString= val1.ToString().Trim(); var remove="Supplier"; var a = sourceString.IndexOf(remove); var b = string.Concat(sourceString.Substring(0, a), sourceString.Substring(a + remove.Length)); var c = b.ToString().TrimStart(); var remove1=":"; var d = c.IndexOf(remove1); var f = string.Concat(c.Substring(0, d), c.Substring(d + remove1.Length)); var g = f.ToString().TrimStart(); textEdit2.Text += Convert.ToString(g).Trim(); } range = worksheet.UsedRange; int column = 0; int row = 0; System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("Product"); dt.Columns.Add("Item/Model Code"); dt.Columns.Add("Item/Model"); dt.Columns.Add("Qty."); dt.Columns.Add("Sales Value"); dt.Columns.Add("Discount Amt."); dt.Columns.Add("Amount"); dt.Columns.Add("Net Amount"); dt.Columns.Add("SR Qty"); dt.Columns.Add("SR Value"); dt.Columns.Add("SR Discount Amt."); dt.Columns.Add("SR Tax Amt."); dt.Columns.Add("SR Net Amt."); dt.Columns.Add("Total Qty"); dt.Columns.Add("Total Value"); dt.Columns.Add("Total Discount"); dt.Columns.Add("Total Tax"); dt.Columns.Add("Total Net Value"); for (row = 5; row <= range.Rows.Count; row++) { DataRow dr = dt.NewRow(); for (column = 1; column <= range.Columns.Count; column++) { dr[column - 1] = (range.Cells[row, column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString(); } dt.Rows.Add(dr); } //workbook.Close(); //excelApp.Quit(); // Binding to DataGridView dataGridView1.DataSource = dt; }; worker.RunWorkerCompleted += delegate { pictureEdit1.Visible = false; worker.Dispose(); btnLoadData.Enabled = true; }; //backgroundWorker1.RunWorkerAsync(); worker.RunWorkerAsync(); }
Attachment: reading_excel_microsoftofficeinteropexcel.zip