naga jyothi

naga jyothi

  • NA
  • 62
  • 110.7k

datagridview data is shown in excelreport but the problem is grid view last cell values are not display in excel report

Jun 26 2012 3:52 AM
private void cmdYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            int dvcount;
            int row =1;
            string str = "select * from vwEmpSalaryDetails where Month='" + cmbMonth.Text + "' and Year='" + cmdYear.Text + "'";
            dbConnection con =new  dbConnection();
            DataTable dt = con.executeSelectQueryNonParameter(str);
            dvExcelreport.DataSource = dt;
            DataGridViewColumn col = new DataGridViewColumn();
            DataGridViewCell cel2 = new DataGridViewTextBoxCell();
            col.CellTemplate = cel2;
            col.HeaderText = "Signature";
            col.Name = "Signature";
            col.Visible = true;
            col.Width = 70;
            dvExcelreport.Columns.Add(col);
            dvcount = dvExcelreport.Rows.Count -1;
            dvExcelreport.Rows[dvcount].Cells["Leaves"].Value = "Total";
            dvExcelreport.Rows[dvcount].Cells["NetSalary"].Value = NetTotal().ToString();
            dvExcelreport.Rows[dvcount].Cells["GrossSalary"].Value = GrossTotal().ToString();
           
           

           

        }
        private double NetTotal()
        {
            double tot = 0;
            int i = 0;
            for (i = 0; i < dvExcelreport.Rows.Count; i++)
            {
                tot = tot + Convert.ToDouble(dvExcelreport.Rows[i].Cells["NetSalary"].Value);
            }
            return tot;
        }
        private double GrossTotal()
        {
            double tot = 0;
            int i = 0;
            for (i = 0; i < dvExcelreport.Rows.Count; i++)
            {
                tot = tot + Convert.ToDouble(dvExcelreport.Rows[i].Cells["GrossSalary"].Value);
            }
            return tot;
        }


        private void dvExcelreport_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnExcelReport_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excelapp.Application.Workbooks.Add(Type.Missing);
            excelapp.Columns.ColumnWidth = 20;
            for (int i = 1; i < dvExcelreport.Columns.Count + 1; i++)
            {
               
                excelapp.Cells[1, i] = dvExcelreport.Columns[i - 1].HeaderText;

            }
            for (int i = 0; i < dvExcelreport.Rows.Count -1 ; i++)
            {

                for (int j = 0; j < dvExcelreport.Columns.Count -1; j++)
                {
                   
                    excelapp.Cells[i + 2, j + 1] = dvExcelreport.Rows[i].Cells[j].Value.ToString();
                    if (i == dvExcelreport.Rows.Count )
                    {
                        int dvcount = dvExcelreport.Rows.Count - 1;

                        excelapp.Cells[i + 2, j + 1] = NetTotal().ToString();
                        excelapp.Cells[i + 2, j + 1] = GrossTotal().ToString();
                    }

                }

            }
            excelapp.ActiveWorkbook.SaveCopyAs("D:\\ExcelSheetReport.xls");
            excelapp.ActiveWorkbook.Saved = true;
            excelapp.Quit();
            MessageBox.Show("Excel file you can find the file D:\\ExcelSheetReport.xls");

        }

Answers (2)