[英]How can I add a bottom border to an Excel range in C#?
我需要在電子表格的某些行上添加底邊框(一條簡單的實線)。 我已經定義了所需的范圍,但是添加邊界代碼的嘗試失敗了,如注釋掉的嘗試所示:
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?
}
我需要為哪個對象的屬性或方法進行分配或調用,以及如何分配?
嘗試這個:
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 =真
對於其他線型選項,您可以嘗試(我還沒有嘗試過):
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.border.linestyle.aspx
這是我的using語句(您不需要所有這些):
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;
這對我有用:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.