I have a small app that collects data from a device and then writes that data to an Excel spreadsheet. I have found that when the Excel file is too large, a corrupt Excel file is created.
The following example code fails in a Xamarin.Forms app for Android. I tried both Infragistics 17.2 and 18.1. The xslx file is generated, but it fails to open in Excel. Note that if I change the number of rows to 70,000 then the app works.
var wb = new Workbook(WorkbookFormat.Excel2007); var ws = wb.Worksheets.Add("Sheet 1"); ws.Rows[0].Cells[0].Value = "Key"; ws.Rows[0].Cells[1].Value = "Value"; for (int ii = 1; ii < 120000; ++ii) { ws.Rows[ii].Cells[0].Value = "Key" + ii.ToString(); ws.Rows[ii].Cells[1].Value = ii; }
var path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Alpha/"; string filename = Path.Combine(path, "test.xlsx"); var fs = File.Create(filename); wb.Save(fs);
Hello Arthur,
I have been investigating the behavior you are reporting, and currently, I am unable to see the behavior you are seeing. I have used the same code that you have provided on a Button click, and the Excel file is able to open in Microsoft Excel for mobile. I am attaching the sample project that I used to test the code you provided. It was built against version 18.1.5 of Infragistics for Xamarin controls.
I would like to take a look at the Excel file that is generated on your end, as it sounds like the application might be running out of memory or something, and perhaps the wb.Save() is only happening partially, creating an incomplete, corrupt workbook? This is essentially all speculation though, and I cannot be certain of this. Would it be possible for you to please provide the workbook that is saved so I may examine it?
Please let me know if you have any other questions or concerns on this matter.
ExcelTest.zip
Thank you for the quick response. I changed my code to use the FileStream and then close that FileStream when done. Now, it all seems to work well. It was odd that it worked for smaller datasets, but failed for larger ones.