Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i get the excel sheet but it has no borders or columns, i want this in correct borders and columns

What I have tried:

private void CopyGridToClipboard(DataGridView grid)
       {
           //Exclude row headers
           grid.RowHeadersVisible = false;

           //Include column headers
           grid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
           grid.SelectAll();
           DataObject dataObj = grid.GetClipboardContent();
           if (dataObj != null)
               Clipboard.SetDataObject(dataObj);

           //Set the visibility of row headers back
           grid.RowHeadersVisible = true;
       }

       private void button9_Click(object sender, EventArgs e)
       {
            //Copy grid to clipboard
    this.CopyGridToClipboard(dataGridView1);

    //Open the excel application and add a workbook
    XL.Application application;
    XL.Workbook book;
    XL.Worksheet sheet;
    application = new XL.Application();
    application.Visible = true;
    book = application.Workbooks.Add();
    sheet = (XL.Worksheet)book.Worksheets[1];

    //label1 Text in Cell[1,1]
    ((XL.Range)sheet.Cells[3, 5]).Value = this.label1.Text;

    //textBox1 Text in Cell[1,2]
    //((XL.Range)sheet.Cells[12, 2]).Value = this.textBox1.Text;

    


    //Let row 3 empty
    //Paste grid into Cell[4,1]
    XL.Range gridRange = (XL.Range)sheet.Cells[7, 3];
    gridRange.Select();
    sheet.PasteSpecial(gridRange);
}

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            double cell1 = 0;   //Grand total of column 0
            double cell2 = 0;   //Grand total of column 2

            // Only interested in changes in columns 0 and 2 so ignore other columns
            if (e.ColumnIndex != 0 && e.ColumnIndex != 2) return;

            //Calculate grand total ... if only the total for this row is required
            // then comment out the foreach...
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                double c1 = 0;
                double c2 = 0;
                if (r.Cells[0].Value != null)
                    if (double.TryParse(r.Cells[0].Value.ToString(), out c1))
                        cell1 += c1;
                if (r.Cells[2].Value != null)
                    if (double.TryParse(r.Cells[2].Value.ToString(), out c2))
                        cell2 += c2;
            }
            // Insert the grand total into this row
            dataGridView1.Rows[e.RowIndex].Cells[6].Value = cell1 + cell2;
        }



i have tried this but it has no borders and columns , only the names
Posted
Comments
Member 13854008 20-Aug-18 6:22am    
please any one help...plz

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900