Ajit N

Ajit N

  • 1.3k
  • 352
  • 68.1k

How to set Border to excel when we export datagrid to excel?

Dec 23 2016 5:37 AM
Hello, i simply export the datagridview into excel. now, I want to set border to data in excel.
so how can i do this, help me for the solution.
 
 this is the code of export datagridview to excel.
  1. private void btnexport_Click(object sender, EventArgs e)  
  2.         {  
  3.             if (dataGridView1.Rows.Count > 0)  
  4.             {  
  5.                 Microsoft.Office.Interop.Excel._Application XcelApp = new Microsoft.Office.Interop.Excel.Application();  
  6.                 XcelApp.Application.Workbooks.Add(Type.Missing);  
  7.   
  8.                 for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)  
  9.                 {  
  10.                     XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;  
  11.                 }  
  12.   
  13.                 for (int i = 0; i < dataGridView1.Rows.Count; i++)  
  14.                 {  
  15.                     for (int j = 0; j < dataGridView1.Columns.Count; j++)  
  16.                     {  
  17.                         XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;  
  18.                     }  
  19.                 }  
  20.                 XcelApp.Columns.AutoFit();  
  21.                 XcelApp.Visible = true;  
  22.             }  
  23.   
  24.   
  25.         }  
 
 
i want the excel in this format. 
 

Answers (2)