Unable to apply GridLines for the Excel

Hi,


I am trying to add grid lines for a given excel…

#1. When I re-load a given file, it fails to apply for a particular column
"New_19122016100920 On Reload.xlsx"


#2. When I add new lines (using InsertRows), it fails to apply for all the newly inserted lines…
"New_19122016100109 On Insert Rows.xlsx"

Can you please help me understand what are the exact steps that we have to follow for getting the grid lines applied?
public override void AddRows(string sheetName, dynamic index, dynamic count)
{
if (string.IsNullOrEmpty(sheetName))
sheetName = _workbook.Worksheets[_workbook.Worksheets.ActiveSheetIndex].Name;
       <span style="color:blue;">var</span> worksheet = _workbook.Worksheets[sheetName];

       worksheet.Cells.InsertRows(index - 1, count, <span style="color:blue;">true</span>);
       _workbook.CalculateFormula(<span style="color:blue;">true</span>);
   }</pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> ApplyGridLines(<span style="color:blue;">string</span> sheetName, <span style="color:blue;">bool</span> flag) {
        <span style="color:blue;">if</span> (<span style="color:blue;">string</span>.IsNullOrEmpty(sheetName))
            sheetName = _workbook.Worksheets[_workbook.Worksheets.ActiveSheetIndex].Name;

        <span style="color:blue;">var</span> worksheet = _workbook.Worksheets[sheetName];

        worksheet.IsGridlinesVisible = flag;
    }</pre></pre></div>

Hi,


Thanks for your posting and using Aspose.Cells.

It is not gridlines issue, it is actually your cells like C15, C16 etc. does not have borders applied. So please apply the borders to those cells and your issue will be fixed. Please see the attached screenshot for your reference.

Please see the following sample code and its output excel file and the screenshot named top-left-borders-applied.png for your reference. The code explains how to set the borders of the cell.

C#
Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

Cell c6 = ws.Cells[“C6”];

Style st = c6.GetStyle();

//Setting left border
Border bord = st.Borders[BorderType.LeftBorder];
bord.Color = Color.Yellow;
bord.LineStyle = CellBorderType.Thick;

//Setting top border
bord = st.Borders[BorderType.TopBorder];
bord.Color = Color.Red;
bord.LineStyle = CellBorderType.DashDot;

c6.SetStyle(st);

wb.Save(“output.xlsx”);

Thanks for the information. I am able to apply the borders, I have been already doing it. But I have to support a feature for Grid On/Off which is not working on these cells and I am not sure Apply Borders and Applying Grid Lines both are same…


Can you please elaborate on what we are trying over here in the sample that has been provided by you?

Thanks,
Anil

Hi,


Thanks for your posting and using Aspose.Cells.

Gridlines do not mean borders. Please see the following sample code, its source excel file and output excel file as well as the screenshot that illustrates how gridlines are different from borders.

In the screenshot, you can see, gridlines are not visible but borders are still visible. So borders are not affected by gridlines visibility.

C#
Workbook wb = new Workbook(“sample.xlsx”);

Worksheet ws = wb.Worksheets[0];
ws.IsGridlinesVisible = false;

wb.Save(“IsGridlinesVisible-false.xlsx”);

Thanks…


Yes, if you see the methods that I have attached above, “ApplyGridLines”, it is doing the exact same thing… Setting the grid line based on the flag value…

However, even when i set the above flag value the gridlines dont become visible, which is why I am trying to look out what is the reason for this behavior…

Thanks,
Anil
PS: I have already been through all the samples, I have already been working with Aspose.Cells for a good two years… So, it looks like there is something weird which is happening whenever we insert rows, the grid lines just doesn’ttake any effect…

Hi,


Thanks for your posting and using Aspose.Cells.

Actually, your cells are filled with white color, so if you fill it with no color, then gridlines will become visible. Please see the following sample code, it sets the fill color to none of the range A10:H25 and now you see, gridlines are visible in the output excel file. Also attached the screenshot for your reference.

C#
Workbook wb = new Workbook(“samp.xlsx”);

Worksheet ws = wb.Worksheets[“Report Set Definition”];

//Remove fill setting from this range
Range rng = ws.Cells.CreateRange(“A10:H25”);

StyleFlag flag = new StyleFlag();
flag.CellShading = true;

Style st = wb.CreateStyle();
st.Pattern = BackgroundType.None;

rng.ApplyStyle(st, flag);

wb.Save(“output.xlsx”);