13

我正在开发一个创建 excel 文件的项目。

我无法在多个单元格上放置边框来组织 excel 文件。

假设我想要一个从单元格 B5 到 B10 的边框。B5,B6,B7,...之间不应该有边界

目前,我有这个代码:

workSheet_range = worksheet.get_Range("B5", "B10");
workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();

它制作了边框,但是它在每个单元格周围放置了一个边框,而不是为所有单元格放置一个大边框。

我怎样才能做到这一点?

4

8 回答 8

14

您需要单独设置这些

.Borders[Excel.XlBordersIndex.xlEdgeBottom] 
.Borders[Excel.XlBordersIndex.xlEdgeRight]
.Borders[Excel.XlBordersIndex.xlEdgeLeft]  
.Borders[Excel.XlBordersIndex.xlEdgeTop]
于 2012-07-31T16:00:09.147 回答
11

也许这可以帮助:

workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick);
于 2014-04-09T20:06:57.410 回答
4

这是在每个单元格周围设置边框的代码:

xlWS.get_Range("C9", "N9").Cells.Borders.Weight = XL.XlBorderWeight.xlMedium;
于 2015-08-07T21:00:08.680 回答
3

我在不影响性能的情况下这样做了。我正在使用一个简单的excel来格式化:

之前

在此处输入图像描述

我设法将范围作为A1:C4变量动态存储在exRange中,并使用下面的代码给出边框

((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;


在此处输入图像描述

于 2016-09-10T11:22:51.397 回答
0
// ** - 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)));        
于 2014-06-13T09:43:45.920 回答
0

此代码在 (row1,col1) 到 (row2,col2) 的区域周围设置了一个边框。单个单元格没有边框。变量颜色是一个整数颜色索引号。有关索引号及其相应颜色的列表,请参阅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 );
于 2014-07-02T17:47:46.853 回答
0
ws.UsedRange.BorderAround(
                        Xl.XlLineStyle.xlDash,
                        Xl.XlBorderWeight.xlThick,
                        Xl.XlColorIndex.xlColorIndexAutomatic,
                        ColorTranslator.ToOle(Color.Blue));
于 2017-09-20T12:02:54.477 回答
0

这是我的解决方案,只需使用 UsedRange() 函数

Excel.Range tRange = oSheet.UsedRange;
            tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
            tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;        
于 2017-10-14T17:55:13.363 回答