This project has moved and is read-only. For the latest updates, please go here.

Need to open and save file in excel before the data is right

Apr 26, 2014 at 12:16 PM
public void CreateAnnuityExcelSheet(List<Calculation> cList, FormCollection form, int DTCyear)
        {
            byte[] bytes = null;
            var stream = new MemoryStream();
            List<Calculation> newList = new List<Calculation>();
            newList.Add(cList.First()); //Getting the values for the first row
            var StartValue = newList[0].StartValue;
            var radio = form["advanceOrArrears"];
            string fileName = newList[0].CalculationName;
            string path = @"C:\ExcelFiles\" + fileName + ".xlsx"; //Path for the file
            FileInfo info = new FileInfo(path);
            info.Directory.Create(); //If C:\ExcelFiles does not exist, create it
            if (!info.Exists)
            {
                using (ExcelPackage package = new ExcelPackage(info))
                {
                    ExcelWorksheet ws = package.Workbook.Worksheets.Add(fileName);
                    //Styles for the sheet

                    package.Save();
                }
            }
        }
When I try to open the saved file I need to open it in Excel and then save it, before that it's something wrong with the data. (for example. The file is 6kb when not saved, and after it's 12-13kb).

I have also tried using a stream and package.SaveAs(stream) but the result is the same
Apr 26, 2014 at 8:55 PM
Edited Apr 26, 2014 at 8:56 PM
krillezzz wrote:
When I try to open the saved file I need to open it in Excel and then save it, before that it's something wrong with the data. (for example. The file is 6kb when not saved, and after it's 12-13kb).
Are you sure something is actually wrong with the data?

The 6kb added to the file size may well be some standard styles and stuff that Excel just likes to add to any file it touches.

I've seen something like that before, but the data was unaffected: the file just contained all standard styles after being saved with Excel.

Furthermore, if you later open the file with a copy of Excel in another language, it re-adds all standard styles again, but with names in the other language, further adding to the file size, but certainly not affecting data in any way.

If this is not your case, can you clarify, maybe with an actual example of which data are affected and how?