2012-12-11 118 views
7

首先,我将我的工作表的彩色边框更改为白色,因为我想要有一张白色的工作表。然后,我制作了一些标题,并希望围绕它制作边框。问题是它使标题中的值之间的边界成为边界,但是顶部,下部是不可见的。在Excel中左右,底部和顶部更改边框

我的代码:

xlWorkSheet5.Columns.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); // Color Sheet5 to white, BusLoad 
xlWorkSheet5.Columns.NumberFormat = "@"; 
Excel.Range rng = (Excel.Range)xlWorkSheet5.get_Range("A7","J7"); 
rng.RowHeight = 25.5; 
rng.BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlHairline, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic); 
rng.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; 
rng.Borders.Weight = 1d; 
rng.Font.Bold = true; 
rng.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 
rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray); 
+0

使用'BorderAround'在'BorderAround2'代替? –

+0

@K_B:我试过了,但结果是一样的 –

+0

用你的第一行代码你不会将单元格的颜色改为白色,但是边界的颜色... –

回答

10

OK我找到了解决办法做了,在这里是我的代码:

xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = 1d; 
xlWorkSheet5.Cells[7, 1].Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = 1d; 
xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = 1d; 
xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = 1d; 
+2

这也可以用一行代码'xlWorkSheet5.Cells [7,1] .Borders.Weight = 1d;' – Wayne

4

如果你想使用的边框[索引]属性然后沿着线使用的东西:

rng.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous; 
rng.Borders[XlBordersIndex.xlEdgeLeft].ColorIndex = <COLOR THAT YOU WANT> 

rng.Borders[XlBordersIndex.xlEdgeTop]... 
rng.Borders[XlBordersIndex.xlEdgeBottom]... 
rng.Borders[XlBordersIndex.xlEdgeRight]... 
1

Border.Weight财产

XlBorderWeight可以是这些XlBorderWeight常量中的一个:xlHairline xlThin xlMedium xlThick。

示例创建从Bx的为MX,将细胞连续线,其中x是线的数目

using Excel = Microsoft.Office.Interop.Excel; 

Excel.Range line = (Excel.Range)excelWorksheet.Rows[1]; 
line.Insert(); 

excelWorksheet.Range[$"B{1}:M{1}"].Cells.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; 
excelWorksheet.Range[$"B{1}:M{1}"].Cells.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin; 
相关问题