I have written some code using ClosedXML that should open a spreadsheet, change the range of a table, and save it, and when I debug the methods everything looks great. But when I open the spreadsheet after saving, the contents of several columns have vanished.
Here are two versions of my method I've written:
Can anybody think of a reason this might be happening? Is there some way data might be defined in a worksheet such that opening the spreadsheet in ClosedXML and saving it, without making changes, could nevertheless cause the contents of some columns to disappear? If it helps, the columns whose content vanishes are all non-numeric: dates, strings, etc.
Here are two versions of my method I've written:
private void UpdateTableRange(string filePath)
{
var spreadsheet = new XLWorkbook(filePath);
var worksheet = spreadsheet.Worksheets.FirstOrDefault(s => s.Name == "Usage");
var range = worksheet.Range(worksheet.FirstCell(), worksheet.LastCellUsed());
worksheet.Tables.Remove("Usage");
var table = range.AsTable("Usage");
worksheet.Tables.Add(table);
spreadsheet.Save();
}
private byte[] UpdateTableRange(byte[] file)
{
var stream = new MemoryStream();
stream.Write(file, 0, file.Length);
var spreadsheet = new XLWorkbook(stream);
var worksheet = spreadsheet.Worksheets.FirstOrDefault(s => s.Name == "Usage");
var range = worksheet.Range(worksheet.FirstCell(), worksheet.LastCellUsed());
worksheet.Tables.Remove("Usage");
var table = range.AsTable("Usage");
worksheet.Tables.Add(table);
spreadsheet.Save();
return stream.ToArray();
}
I thought that perhaps I was messing up the data by removing the table and adding a new one, but if I do nothing more than open the workbook and save it, having first made no changes, whatosever, I still lose the contents of some of the columns.Can anybody think of a reason this might be happening? Is there some way data might be defined in a worksheet such that opening the spreadsheet in ClosedXML and saving it, without making changes, could nevertheless cause the contents of some columns to disappear? If it helps, the columns whose content vanishes are all non-numeric: dates, strings, etc.