简体   繁体   English

如何在电子表格范围(Aspose Cells)周围添加边框?

[英]How can I add borders around a spreadsheet range (Aspose Cells)?

I want to add borders to a range on a spreadsheet. 我想在电子表格上的范围内添加边框。 Based on some VB code here , I tried this: 根据此处的一些VB代码,我尝试了以下操作:

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

...but it pushes most of the data down the sheet, as can be seen here: ...但是它将大多数数据推到表的下方,如下所示:

在此处输入图片说明

...and here: ...和这里:

在此处输入图片说明

This is what the sheet looked like prior to adding those borders. 这是表格添加这些边框之前的外观。

在此处输入图片说明

Actually, I want internal borders, too, not just around the edge, but first things first. 实际上,我也想要内部边界,不仅是边缘,而且首先是第一要务。

BTW, it also seemed like a very "expensive" operation - the report took much longer to generate with that borderizing code added. 顺便说一句,这似乎也是一个非常“昂贵”的操作-在添加边界化代码后生成报告的时间要长得多。

UPDATE 更新

I was able to get it to work better, but it's still messing up my formatting. 我能够使它更好地工作,但是仍然困扰着我的格式。 With this code: 使用此代码:

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

...I am getting a border that doesn't shove the data down the sheet: ...我遇到了一个边界,该边界不会将数据推到表外:

在此处输入图片说明

But it 86ed my beautiful formatting, which you can see here, when the range was borderless: 但是,当范围是无边界的时候,它使我的格式漂亮了86,您可以在这里看到它:

在此处输入图片说明

How can I get my border and retain my formatting, too? 如何获得边框并保留格式?

Your code to apply the outline borders to a range is correct as I have tested it against the latest version of Aspose.Cells for .NET 17.1.0 (available via NuGet and Aspose download section). 您将轮廓边框应用于某个范围的代码是正确的,因为我已针对.NET 17.1.0的最新版本的Aspose.Cells(通过NuGet和Aspose下载部分提供)对其进行了测试。 Please note, setting the outline borders should not disturb the existing formatting of the cells because the Range.SetOutlineBorder operates only on the borders, however, if you wish to apply the border to each individual cell in the range, the existing formatting can be overwritten. 请注意,设置轮廓边框不会干扰单元格的现有格式,因为Range.SetOutlineBorder仅在边框上运行,但是,如果您希望将边框应用于范围中的每个单元格,则现有格式可以被覆盖。

I am going to post the sample code along with the input & output spreadsheets on your thread created in Aspose.Cells support forum, and I humbly request you to share your input spreadsheet along with executable piece of code in Aspose.Cells support forum for further investigation in case the problem persists. 我将在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");

Note: I am working as Developer Evangelist at Aspose. 注意:我是Aspose的开发人员布道者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我范围的子部分无法添加边框(Aspose Cells)? - Why is a subsection of my range failing to add borders (Aspose Cells)? 将图像放在电子表格(Aspose Cells)上时,如何防止图像尺寸改变? - How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)? 如何将电子表格中的列宽设置为特定的像素数(Aspose Cells)? - How can I set the column width in a spreadsheet to a specific pixel count (Aspose Cells)? 如何在一系列合并和未合并的单元格周围放置边框 - How to put borders around a range of merged and non-merged cells 如何在计算出的像元(Aspose Cells)中防止“#Div / 0!”? - How can I prevent “#Div/0!” in calculated cells (Aspose Cells)? 如何使用C#在Excel范围周围添加边框? - How to add borders around Excel range using C#? 在Excel C#上的多个单元格周围添加边框 - add borders around multiple cells on excel c# 当电子表格 window 最小化时,如何获取我的 Excel VSTO 插件以更新单元格? - How can I get my Excel VSTO Add-in to update cells when the spreadsheet window is minimized? 如何确定数据透视表生成多少行(Aspose Cells)? - How can I determine how many rows a PivotTable generates (Aspose Cells)? 如何用不同的值代替自动生成的总计值(Aspose Cells)? - How can I replace automatically-generated grand total values with different ones (Aspose Cells)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM