How to clear the contents of the Worksheet but not formatting

Hello,


I Have been generating my excel reports dynamically as above method. i have dictionary object in which i have data and then i write data to excel dynamically same as above described. To do that I have predefined template exists in my machine library. i take that template and write data into it. Now I loose my formatting of all the cells/worksheet when i write data into that template file dynamically. I clear the content of that worksheet before i write data into it. Following is my code.

Workbook workbook = new Workbook(ms);
Worksheet worksheet = workbook.worksheets[0];
if (worksheet.Cells.Count > 0)
{
worksheet.Cells.ClearContents(1, 0, worksheet.Cells.MaxDataRow, worksheet.Cells.MaxDataColumn);
}

int col = 0;
int row = 0;

foreach (KeyValuePair<string, List> entry in reportValues)
{
cell[row, col].PutValue(entry.Key);
foreach (var value in entry.Value)
{
row++;
if (ContainsHTML(Convert.ToString(value)))
cell[row, col].HtmlString = (Convert.ToString(value));
else if (value is DateTime)
{
cell[row, col].PutValue(Convert.ToDateTime(value));
Style style = cell[row, col].GetStyle();
style.Custom = “mm/dd/yyyy”;
cell[row, col].SetStyle(style);
worksheet.AutoFitColumn(col);
}
else if (value is int)
{
cell[row, col].PutValue(Convert.ToInt16(value));
worksheet.AutoFitColumn(col);
}
else if (value is double)
{
cell[row, col].PutValue(Convert.ToDouble(value));
worksheet.AutoFitColumn(col);
}
else if (value is string)
{
cell[row, col].PutValue(Convert.ToString(value));
worksheet.AutoFitColumn(col);
}
else if (value is Boolean)
{
cell[row, col].PutValue(Convert.ToBoolean(value));
worksheet.AutoFitColumn(col);
}

}
row = 0;
col++;
}

My requirement is to not loose the formatting even after clearing and writing data into it. Can someone help me how to maintain formatting of predefined template/worksheet after clearing and writing data into it.


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for contacting Aspose support.

You can use the Cells.ClearContents method to clear the data and to preserve the formatting. Please check the following piece of code and attached spreadsheets for your reference.

C#

var workbook = new Workbook(“D:/rangecells.xlsx”);
var sheet = workbook.Worksheets[0];
var cells = workbook.Worksheets[0].Cells;
var range = sheet.Cells.MaxDisplayRange;
sheet.Cells.ClearContents(range.FirstRow, range.FirstColumn, range.RowCount, range.ColumnCount);
workbook.Save(“D:/output.xlsx”);

Hello Babar raza,


I have tried with above code but still it’s not working. here i am attaching both my input template and output file for your reference. I gave predefined format in some rows of Input files and when output generates then it does not extend format in further rows. it should extend formatting till the end row. I think issue seems something different. it should be something in selecting predefined format of template. I am not getting it exactly. let me know your views.


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for writing back.

I have checked both of your provided spreadsheets and it seems that the output.xlsx is corrupt because Excel application prompts for repair while loading it. This could be due to the reason that your current version of the API may contain bug so I request you to try the scenario against the latest version of Aspose.Cells for .NET (Latest Version) and feed us back with your results. In case the problem persists, please provide us a demo sample application replicating the issue with latest version of the API.

I gave predefined format in some rows of Input files and when output generates then it does not extend format in further rows. it should extend formatting till the end row.


Moreover, I am not sure about the comments as quoted above. Please note, the code shared in my previous response just removed the contents where formatting remains intact, but it does not extent the formatting to other rows. Are you manually extending the formatting in Excel application? If you are extending the formatting in code then a sample application will surely help us understand your scenario better.

Hello Babar raza,


Okay i’ll replace with new API but issue seems different.
Yes we do have predefined templates in which we give custom formatting to some starting rows including header and footer. but we do not know how many rows are going to come in my report dynamically. so it is not extending formatting to increased rows. we need some code from which we can identify predefined formatting applied to template file and then we can give that style to whole range of list object. I have already written my logic in my previous post which renders data into excel sheet. It’s difficult to post whole application.


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for elaborating the scenario.

You are using Excel Tables (ListObjects from Aspose.Cells perspective) in your provided spreadsheet where you wish to extend the row formatting to more rows according to size of your data set. Please note, Aspose.Cells APIs provide very easy to use mechanism to extend/retract the table size while using the ListObject.Resize method. Please check the following piece of code that extends the table size to 10 more rows on the worksheet BuyerList.

C#

var workbook = new Workbook(“D:/InputTemplate.xlsx”);
var worksheet = workbook.Worksheets[“BuyerList”];
var cells = worksheet.Cells;
var table = worksheet.ListObjects[0] ;
//Adding 10 more rows to the table with formatting intact
table.Resize(table.StartRow, table.StartColumn, table.EndRow + 10, table.EndColumn, true);
workbook.Save(“D:/result.xlsx”);

Hello Babar Raza


I have tried same code as you described above to find predefined template or table in my template file. but it throws error as it is not able to find any listobject/table i guess.

var table = worksheet.ListObjects[0];
if (table != null)
{
table.Resize(table.StartRow, table.StartColumn, cell.MaxDataRow, cell.MaxDataColumn, true);
}

here by i am attaching my input template file for your reference. can you tell me what kind of template this excel
file having? how can i find this template to resize it if no of rows increases using code?


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for writing back.

I have quickly checked your recently shared spreadsheet, and I am able to access the ListObject from the worksheet Pipeline. Please note, I am using the latest version of Aspose.Cells for .NET (Latest Version) for my testing so I suggest you to use the same revision on your end as well.

That being said, when I performed the ListObject re-size operation on your provided XLSM file, the resultant spreadsheet becomes corrupted and Excel had to remove the QueryTable1.xml to repair the spreadsheet, This problem is probably caused due to the database connection associated with the spreadsheet because the problem does not happen with your previous sample InputTemplate.xlsx which as no database connection. I am performing more tests on my side to find a workaround for the said situation. In the meanwhile, please give a try to the latest version of Aspose.Cells for .NET API (link shared above) on your side and let us know if you are able to generate a valid spreadsheet as you can access the associated database and execute the embedded query on your end.

C#

var book = new Workbook(“D:/inputtemplate.xlsm”);
Console.WriteLine("Number of worksheets: " + book.Worksheets.Count);
var sheet = book.Worksheets[“Pipeline”];
var listObjects = sheet.ListObjects;
var table = listObjects[0];
if (table != null)
{
Console.WriteLine("Number of rows in table: " + table.EndRow);
table.Resize(table.StartRow, table.StartColumn, table.EndRow + 10, table.EndColumn, true);
Console.WriteLine("Number of rows in table after resize: " + table.EndRow);
}
book.Save(“D:/result.xlsm”, SaveFormat.Xlsm);

Hello Babar Raza,


Firstly let me tell you that we work on sharepoint 2010 environment so we need to use all aspose dlls which works in .net 3.5 or lower. If the API Aspose.Cells for .NET (Latest Version) which you are saying is upper version then we cannot use that. we need to use dll which works in .Net 3.5.

And again i have tried same code with my old Aspose Cell API which is having version 7.7.0.3 and i am still facing same issue. it is not able to find any listobjects so it is throwing index out of bound error from following line.
var table = listObjects[0];

Can you tell me what could be the issue at my end? why i am not getting any listobjects
in that template file?


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for writing back.

If you are not able to retrieve the ListObject at 0 index of your recent sample while using the version 7.7.0.3 then it must be a bug of that release because I am able to correctly access the said ListObject with latest revision on my side. I am afraid, there is no other way but to upgrade your project to use the latest revision because no such issue (IndexOutOfBound exception) was observed.

Regarding the assembly compiled for .NET framework 3.5, the link shared earlier contains only the assembly compiled for .NET 2.0 that can be used with higher revision sof the .NET framework. However, if you need the specific assembly then you have to wait a little as we are currently preparing the next official release of Aspose.Cells for .NET 8.5.0 for publication. The said release will contain the specific DLL to be used with .NET framework 3.5.

Hello Babar Raza,


Thank you for the prompt replies. I really appreciate it. now I have tested with your latest API 8.4.2.7. it seems very weird issue. As when i tested it with fresh console application it works but same code it is not working in my sharepoint application. it’s a same template file i am using. i am not getting exact issue. Can you tell me why it is throwing “Index was out of range. Must be non-negative and less than the size of the collection.” error with my console application which is made with sharepoint objects? Why it’s not getting that listobject which i am able to get in new fresh console application?


Thanks & Regards
Ashish Rajguru

Hello Babar Raza,

I found the issue. Actually i was clearing worksheet data with following code before finding listobjects.

if (worksheet.Cells.Count > 0)
{
//worksheet.Cells.ClearContents(1, 0, worksheet.Cells.MaxDataRow, worksheet.Cells.MaxDataColumn); 
var range = worksheet.Cells.MaxDisplayRange;
worksheet.Cells.ClearContents(range.FirstRow, range.FirstColumn, range.RowCount, range.ColumnCount);
}
var listObjects = worksheet.ListObjects;
var table = listObjects[0]; 
if (table != null)
{
table.Resize(table.StartRow, table.StartColumn, cell.MaxDataRow, cell.MaxDataColumn, true);
} 

So after applying ClearContents code i am trying to find list object so it is not able to find as with clearing data it is also clearing table object as well in worksheet. how can i prevent to do that? what should be the work around on this?

Thanks & Regards

Ashish Rajguru

Hi Ashish,

Please note, you are clearing the MaxDisplayRange that will clear everything on the worksheet including the ListObjects . You can workaround this situation in two ways, that are as follow.

  • While clearing the MaxDisplayRange , do not clear the ListObject’s header. It will retain the ListObject and it’s formatting.
  • Do not clear the MaxDisplayRange , instead clear the ListObject’s data range.

Please check the below provided code snippet that uses the both above mentioned approaches where first approach is commented out at the moment. You may use one option at a time.

C#

var book = new Workbook(“D:/inputtemplate.xlsx”);
var worksheet = book.Worksheets[“BuyerList”];
var listObjects = worksheet.ListObjects;
var table = listObjects[0];
if (worksheet.Cells.Count > 0)
{
//var range = worksheet.Cells.MaxDisplayRange;
//worksheet.Cells.ClearContents(range.FirstRow + 1, range.FirstColumn, range.RowCount, range.ColumnCount);
var range = table.DataRange;
worksheet.Cells.ClearContents(range.FirstRow, range.FirstColumn, range.RowCount, range.ColumnCount);
}
book.Save(“D:/clearcontent.xlsx”);
if (table != null)
{
table.Resize(table.StartRow, table.StartColumn, table.EndRow + 10, table.EndColumn, true);
}
book.Save(“D:/output.xlsx”, SaveFormat.Xlsx);

 Save Editcancel
<span class="kwrd">if</span> (table != <span class="kwrd">null</span>)
{
      table.Resize(table.StartRow, table.StartColumn, table.EndRow + 10, table.EndColumn, <span class="kwrd">true</span>);
}
book.Save("D:/output.xlsx", SaveFormat.Xlsx);

Hi again,


This is to inform you that I have logged the scenario discussed in this post in our database for product team’s review. Please note, re-sizing the ListObject in the template XLSM with external data range generates the corrupted spreadsheet that cannot be opened with Excel application without repairing whereas the repair process removes the QueryTable1.xml file. This scenario needs to be investigated therefore has been logged under the ticket CELLSNET-43725. Please allow us some time for proper analysis and get back to you with updates in this regard.

Hello Babar Raza,


This is to inform you that using listobject table range my previous issue of Index out of range has been fixed now and as discussed i am facing same issue " repair process removes the QueryTable1.xml file" which you have been also facing at your end. so my template is not properly generated with extended data. Thank you for taking this issue in your priority. it will would be highly appreciated if you and your team could resolve the issue asap. Waiting for your prompt response on this.


Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for the confirmation on IndexOutOfRange exception. Regarding the ticket logged earlier as CELLSNET-43725, we have completed the preliminary investigation, and we believe that the presented behavior is caused due to some error in Excel application that all empty cells must be initial if the data source of the table is QureyTable. We are looking into this scenario further to see if Aspose.Cells APIs work same as of Excel because initializing all cells will consume time and memory. We will keep you posted with updates in this regard.

Hi again,


This is to update you regarding the ticket logged earlier as CELLSNET-43725. Please note, Aspose.Cells APIs currently do not support the table creation with external data source, however, you can re-size an existing table whose data source is external but you have to fill in the data for the extended range before saving the resultant spreadsheet, that is; you cannot save the empty extended table.

Hi,

Please try our latest version/fix:

Your issue should be fixed in it.

Let us know your feedback.

Thank you.

The issues you have found earlier (filed as CELLSNET-43725) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.