如何在行中为excel设置边框 [英] how to set border in excel for rows

查看:90
本文介绍了如何在行中为excel设置边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮导出代码如下;





Button Export Code as follows;


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;
           }
       }




来自上面代码的
我设置列标题BOLD正在工作。



但我想为每一行设置边框我该怎么办?从我上面的代码如何为每一行设置边框。



我该怎么办?请帮助我。



Rgds,

Narasiman P.



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.

推荐答案

请看这里:

http://stackoverflow.com/a/3100862 [ ^ ]

你可能会发现这个也有用:http://snipplr.com/view/67825/net--c-- interop - excel --- utilitywrapper-class / [ ^ ]
Look here:
http://stackoverflow.com/a/3100862[^]
You might find this also of use: http://snipplr.com/view/67825/net--c--interop--excel---utilitywrapper-class/[^]




同样的话题在下面的URL

http://www.dotnetfunda.com/forums /thread3242-use-csharp-add-excel-borders.aspx [ ^ ]


这篇关于如何在行中为excel设置边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆