【问题标题】:How can I add borders around a spreadsheet range (Aspose Cells)?如何在电子表格范围(Aspose Cells)周围添加边框?
【发布时间】:2017-01-13 13:34:30
【问题描述】:

我想为电子表格的某个范围添加边框。基于一些VB代码here,我尝试了这个:

Range range = locationWorksheet.Cells.CreateRange(7, 0, 93, 6);

range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Red);

...但它会将大部分数据向下推送,如下所示:

...这里:

这是添加这些边框之前工作表的样子。

实际上,我也想要内部边框,不仅仅是边缘,而是首要任务。

顺便说一句,这似乎也是一个非常“昂贵”的操作 - 添加边界代码后生成报告需要更长的时间。

更新

我能够让它更好地工作,但它仍然弄乱了我的格式。使用此代码:

private void BorderizeDataPortionOfLocationSheet()
{
    int FUDGE_FACTOR = 5;
    int rowsUsed = locationWorksheet.Cells.Rows.Count + FUDGE_FACTOR;
    int colsUsed = locationWorksheet.Cells.Columns.Count; //QTY_COL; // last column
    string rangeBegin = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(1, 8);
    string rangeEnd = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(6, rowsUsed);
    Range entireSheetRange = locationWorksheet.Cells.CreateRange(rangeBegin, rangeEnd);

    CellsFactory cf = new CellsFactory();
    Style style = cf.CreateStyle();
    entireSheetRange.SetStyle(style);
    entireSheetRange.SetOutlineBorders(CellBorderType.Thin, Color.Black);
}

...我得到的边框不会将数据推送到工作表中:

但是当范围是无边界时,它 86ed 我的漂亮格式,你可以在这里看到:

如何获得边框并保留我的格式?

【问题讨论】:

    标签: excel border aspose aspose-cells


    【解决方案1】:

    您将轮廓边框应用于范围的代码是正确的,因为我已经针对 .NET 17.1.0 的最新版本 Aspose.Cells(可通过 NuGet 和 Aspose 下载部分获得)对其进行了测试。请注意,设置轮廓边框不应干扰单元格的现有格式,因为 Range.SetOutlineBorder 仅对边框起作用,但是,如果您希望将边框应用于范围内的每个单独的单元格,则可以覆盖现有格式.

    我将在您在 Aspose.Cells 支持论坛中创建的线程上发布示例代码以及输入和输出电子表格,并恳请您分享您的输入电子表格以及 Aspose.Cells 支持中的可执行代码片段如果问题仍然存在,请进行进一步调查。

    var book = new Workbook(dataDir + "book1.xlsx");
    var sheet = book.Worksheets[0];
    var range = sheet.Cells.MaxDisplayRange;
    //Setting outline border to range
    range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
    range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
    range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
    range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Red);
    
    book.Save(dataDir + "output.xlsx");
    

    注意:我在 Aspose 担任开发人员宣传员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2022-01-24
      相关资源
      最近更新 更多