以编程方式在Excel中创build边框

我需要知道如何在基于单个单元格的for循环中为excel创build边框。 下面是我的代码导出到Excel

for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) { worksheet.Cells[5, i] = dataGridView1.Columns[i - 1].HeaderText; worksheet.Cells[5, i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; worksheet.Cells[5, i].Font.Bold = true; } 

尝试使用来自Microsoft.Office.Interop.Excel的示例:如何将边框应用于ONE CELL ,例如:

 Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange; Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1]; Microsoft.Office.Interop.Excel.Borders border = cell.Borders; border[XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeTop].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; 

这就是我所做的

  public void MarkBorder(string fromColName, string toColName) { string colName = fromColName + ":" + toColName; CurrentWorksheet.Range[colName].BorderAround(); } 

CurrentWorksheet被定义为

  public Worksheet CurrentWorksheet { get; set; }