2015-11-10 52 views
2

我需要爲電子表格上的某些行添加底部邊框(簡單的實線)。我有我需要定義工作範圍,但嘗試添加邊框代碼都失敗了,因爲可以通過註釋掉的嘗試中可以看出:如何在C#中的Excel範圍中添加底部邊框?

private ApplicationClass _xlApp; 
private Workbook _xlBook; 
private Sheets _xlSheets; 
private Worksheet _xlSheet; 
. . . 
private void AddBottomBorder(int rowToBottomBorderize) 
{ 
    var rangeToBottomBorderize = (Range)_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]; 
    //rangeToBottomBorderize.Borders[_xlApp.XlBordersIndex.xlEdgeBottom] = 
    //rangeToBottomBorderize.Borders[XlBordersIndex.xlEdgeBottom] = 1d; 
    //rangeToBottomBorderize.Borders[_xlSheet. 
    //_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL].Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = 1d; 
    // someRange.Borders.Item[XlBordersIndex.xlEdgeBottom)] = ...what now? 
} 

哪個對象的屬性或方法做我需要做的幽會到或打電話,以及如何?

回答

0

這個工作對我來說:

private void AddBottomBorder(int rowToBottomBorderize) 
{ 
    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]]; 
    Borders border = rowToBottomBorderizeRange.Borders; 
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; 
} 
4

試試這個:

setBorder(rangeToBottomBorderize.Borders[_xlApp.XlBordersIndex.xlEdgeBottom], XlBorderWeight.xlThick); 

輔助功能:

private static void setBorder(Border border, XlBorderWeight borderWeight) 
    { 
    border.LineStyle = XlLineStyle.xlContinuous; 
    border.ColorIndex = XlConstants.xlAutomatic; 
    border.TintAndShade = 0; 
    border.Weight = borderWeight; 
    } 

要清除邊框你可以使用:

border.LineStyle = XlConstants.xlNone 

對於邊界體重,你可能想.xlThin

邊框重量:

參見:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(Microsoft.Office.Interop.Excel.XlBorderWeight);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6);k(DevLang-csharp)&rd=true

對於其他行樣式選項,你可以試試(我沒試過這些):

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.border.linestyle.aspx

這裏是我使用的語句(你不需要所有這些):

using Microsoft.Office.Interop.Word; 
using Microsoft.Office.Interop.Excel; 
using Application = Microsoft.Office.Interop.Excel.Application; 
using Border = Microsoft.Office.Interop.Excel.Border; 
using Range = Microsoft.Office.Interop.Excel.Range; 
using XlBorderWeight = Microsoft.Office.Interop.Excel.XlBorderWeight; 
using XlLineStyle = Microsoft.Office.Interop.Excel.XlLineStyle; 
using XlConstants = Microsoft.Office.Interop.Excel.Constants; 
+0

「XlBordersIndex」未被識別;我得到,「'Microsoft.Office.Interop.Excel.ApplicationClass'不包含'XlBordersIndex'的定義,並且沒有接受'Microsoft.Office.Interop.Excel.ApplicationClass'類型的第一個參數的擴展方法'XlBordersIndex'可能是發現(您是否缺少使用指令或程序集引用?)「 –

+0

」XlConstants「也無法識別。 –

+0

我現在使用的語句應該是正確的。 – Derek

相關問題