C# 更改 Excel 中的左、右、下和上边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13817702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:53:15  来源:igfitidea点击:

Change Border in Excel left, right, bottom and top

c#excelborder

提问by Le Viet Hung

Firstly, I changed color borders of my sheet to white, because I want to have a white sheet.

首先,我将工作表的颜色边框更改为白色,因为我想要一张白色的工作表。

Then I made some headers and want to make a border around it. The problem is that it made borders between the values in header, but top, down are not visible.

然后我做了一些标题并想在它周围做一个边框。问题是它在标题中的值之间设置了边框,但顶部、底部不可见。

My code:

我的代码:

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

采纳答案by Le Viet Hung

OK I found a solution to do it, here is my code:

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

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;

回答by K_B

If you want to use the Borders[index] property then use something along the lines of:

如果您想使用 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]...

回答by user8434118

Border.Weight property

Border.Weight 属性

XlBorderWeight can be one of these XlBorderWeight constants: xlHairline xlThin xlMedium xlThick.

XlBorderWeight 可以是以下 XlBorderWeight 常量之一:xlHairline xlThin xlMedium xlThick。

Example for create continuous line on Cells from Bx to Mx, where x is number of line

在从 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;