private void Excelbutton1_Click(object sender, EventArgs e) { fromDate = Convert.ToString(Convert.ToDateTime(fromDateTimePicker.Text).ToShortDateString()); toDate = Convert.ToString(Convert.ToDateTime(toDateTimePicker.Text).ToShortDateString()); EfficencyBLL efficency = new EfficencyBLL(); DataTable dt = new DataTable(); BindingSource dataBind = new BindingSource(); dt = efficency.GetProductionAndOtSummeryReport(fromDate, toDate); dataBind.DataSource = dt; productionDataGridView.DataSource = dataBind; if (productionDataGridView.Rows.Count > 0) { try { // Bind table data to Stream Writer to export data to respective folder StreamWriter wr = new StreamWriter(@"E:\\Production_OT_Repports.xls"); wr.Write("HIRDARAMANI APPARELS BANGLADESH"); wr.WriteLine(); wr.Write("Production & OT Summery"); wr.WriteLine(); wr.WriteLine(); wr.WriteLine(); // Write Columns to excel file for (int i = 0; i < dt.Columns.Count; i++) { wr.Write(dt.Columns[i].ToString().ToUpper() + "\t"); } wr.WriteLine(); //write rows to excel file for (int i = 0; i < (dt.Rows.Count); i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (dt.Rows[i][j] != null) { wr.Write(Convert.ToString(dt.Rows[i][j]) + "\t"); } else { wr.Write("\t"); } } wr.WriteLine(); } wr.Close(); } catch (Exception ex) { throw ex; } } }
public void GetProductionAndOtSummeryReport(string FromDate, string ToDate) { EfficencyBLL efficency = new EfficencyBLL(); DataTable dt = new DataTable(); BindingSource dataBind = new BindingSource(); try { dt = efficency.GetProductionAndOtSummeryReport(FromDate, ToDate); dataBind.DataSource = dt; productionDataGridView.DataSource = dataBind; } catch (Exception ex) { } }