Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Button Export Code as follows;


C#
private void Btn_Export_Click(object sender, EventArgs e)
       {

           try
           {
               Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
               Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
               Microsoft.Office.Interop.Excel.Worksheet xlsheet;

               //Create object of excel
               xlWorkBook = (Microsoft.Office.Interop.Excel.Workbook)xlapp.Workbooks.Add(1);
               xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet;
               int i = 0;
               int j = 0;
               for (i = 0; i < datagridView.Columns.Count; i++)
               {
                   xlsheet.Cells[1, i+1] = datagridView.Columns[i].HeaderText;
               }

               for (i = 0; i < datagridView.RowCount; i++)
               {
                   for (j = 0; j < datagridView.ColumnCount; j++)
                   {
                       xlsheet.Cells[i+2, j+1] = datagridView.Rows[i].Cells[j].Value;
                   }
               }
                   xlapp.Visible = true;

                   Microsoft.Office.Interop.Excel.Range myRange = xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[this.datagridView.RowCount + 1, this.datagridView.Columns.Count]);
                   Microsoft.Office.Interop.Excel.Font x = myRange.Font;

                   //set bold font to column header

                   myRange = xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[1, this.datagridView.Columns.Count]);
                   x = myRange.Font;
                   x.Bold = true;
                   //autofit all columns
                   myRange.EntireColumn.AutoFit();
                   myRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

           }
              catch (Exception ex)
           {
               MessageBox.Show(ex.ToString(), "Error");
               return;
           }
       }


from the above code i set column header BOLD is working.

but i want set borders for each row how can i do? from my above code how to set border for each row.

how can i do? please help me.

Rgds,
Narasiman P.
Posted
Updated 4-Feb-13 21:26pm
v2

 
Share this answer
 
Hi,
Same kind of topic is discussed in below URL
http://www.dotnetfunda.com/forums/thread3242-use-csharp-add-excel-borders.aspx[^]
 
Share this answer
 

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