在Excel中左,右,底部和顶部更改边框

时间:2012-12-11 09:57:39

标签: c# excel border

首先,我将纸张的颜色边框更改为白色,因为我想要一张白纸。然后我做了一些标题,并想围绕它做边框。问题是它在标题中的值之间建立了边界,但顶部,向下是不可见的。

我的代码:

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

3 个答案:

答案 0 :(得分:10)

好的我找到了解决方案,这是我的代码:

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;

答案 1 :(得分:4)

如果要使用Borders [index]属性,请使用以下行:

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]...

答案 2 :(得分: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;
相关问题