如何使用C#在excel中的多个单元格周围设置边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11743809/
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
How can a border be set around multiple cells in excel using C#
提问by Arnout
I am working on a project that creates excel files.
我正在开发一个创建 excel 文件的项目。
I am having trouble placing a border on multiple cells to organize the excel file.
我无法在多个单元格上放置边框来组织 excel 文件。
Let's say I want a border from cell B5 to B10. There shouldn't be borders between B5, B6, B7,...
假设我想要从单元格 B5 到 B10 的边框。B5、B6、B7、...之间不应该有边界
Currently, I have this code:
目前,我有这个代码:
workSheet_range = worksheet.get_Range("B5", "B10");
workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
It makes the borders, however it places a border around every cell instead of one big border for all cells.
它制作边框,但是它在每个单元格周围放置一个边框,而不是所有单元格的一个大边框。
How can I accomplish this?
我怎样才能做到这一点?
采纳答案by Tim Williams
You need to individually set these
您需要单独设置这些
.Borders[Excel.XlBordersIndex.xlEdgeBottom]
.Borders[Excel.XlBordersIndex.xlEdgeRight]
.Borders[Excel.XlBordersIndex.xlEdgeLeft]
.Borders[Excel.XlBordersIndex.xlEdgeTop]
回答by Simon
Maybe this can help :
也许这可以帮助:
workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick);
回答by E Coder
// ** - You Should do it in all Cells
//BorderAround: Medium**
worksheet.get_Range("b5", "b5").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
worksheet.get_Range("b6", "b6").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
worksheet.get_Range("b10", "b10").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
回答by SaulN
This code puts a border around the area from (row1,col1) to (row2,col2). Individual cells do not get a border. The variable color is an integer color index number. See http://www.databison.com/excel-color-palette-and-color-index-change-using-vba/for a list of index numbers and their corresponding colors.
此代码在从 (row1,col1) 到 (row2,col2) 的区域周围放置一个边框。单个单元格没有边框。变量 color 是一个整数颜色索引号。有关索引号及其相应颜色的列表,请参阅http://www.databison.com/excel-color-palette-and-color-index-change-using-vba/。
Range cell1 = worksheet.Cells[row1,col1];
Range cell2 = worksheet.Cells[row2,col2];
Range range = worksheet.get_Range(cell1,cell2);
range.BorderAround(
Type.Missing, XlBorderWeight.xlThick, (XlColorIndex)color, Type.Missing );
回答by Santosh Artham
This is the code that sets a border around each cell:
这是在每个单元格周围设置边框的代码:
xlWS.get_Range("C9", "N9").Cells.Borders.Weight = XL.XlBorderWeight.xlMedium;
回答by Sarath Avanavu
I did this without impacting the performance. I am taking a simple excel to format :
Before
我这样做没有影响性能。我正在使用一个简单的 excel 进行格式化:
之前
I managed to store the range as A1:C4in a variable dynamically in exRangeand used the below code to give border
我设法A1:C4在exRange 中动态地将范围存储在一个变量中,并使用下面的代码来给出边框
((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
After
后
回答by Hamid
ws.UsedRange.BorderAround(
Xl.XlLineStyle.xlDash,
Xl.XlBorderWeight.xlThick,
Xl.XlColorIndex.xlColorIndexAutomatic,
ColorTranslator.ToOle(Color.Blue));
回答by MORFEE89
Here is my solution, use simply UsedRange() function
这是我的解决方案,只需使用 UsedRange() 函数
Excel.Range tRange = oSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;


