1

I am generating several excel copies from a template (its really big). For that First I am taking the template from a file location, then based on a loop for every iteration I am creating a new ExcelPackage(newFile,Template). After that I am taking the exact ExcelWorksheet that I have to edit. Then after editing I am Saving as the file as newFile. The time of opening the saved file Two problem is occurring:

  1. If there is no Excel instance is running on the PC then the saved file is opening but with no data.
  2. If the Excel instance is running then the saved file is opening with Warning message but working. "Problem with some content with Excel. Do you want us to recover?" and "Excel was able to recover some unreadable content "

    string templateExcel = @"Location\template.xlsx";
    FileInfo templateFile = new FileInfo(@"Location\newFile.xlsx");
    using (FileStream templateExcelStream = File.OpenRead(templateExcel))
    {
        using (ExcelPackage copyExcel = new ExcelPackage(templateExcelStream))
        {
            ExcelWorksheet presentWorkSheet = copyExcel.Workbook.Worksheets["Name"];
            presentWorkSheet.Cells[4, 2].Value = Value from condition;
            copyExcel.SaveAs(templateFile);
        }
    }
    
ikerbera
  • 195
  • 1
  • 3
  • 15
  • You need to provide a [mcve]. There's not much to go on here, any answer would be a guess. – Charles Mager Feb 15 '19 at 10:24
  • @CharlesMager The first Picture is for my problem point 2 for a saved excel: https://stackoverflow.com/questions/51652745/epplus-saving-an-excel-created-in-memory-and-opening-it-on-excel-gives-an-erro/51655063 Using the attached code I am creating The excel from a existing template .. – Mahfuzur Rahman Feb 15 '19 at 10:41
  • @MahfuzurRahman that's a 6 months old question about something completely different - that was about *returing* a file from an MVC action. As that question says at the end, the problem was in the project itself. – Panagiotis Kanavos Feb 15 '19 at 12:50
  • @MahfuzurRahman you have to post code that actually reproduces the problem. Is the template OK? Does that `Name` sheet exist? What values do you store in the cell? It's impossible to guess what's wrong right now – Panagiotis Kanavos Feb 15 '19 at 12:52
  • You say you're creating a new file with `ExcelPackage(newFile,Template)` and yet your code does something different. Is this the actual code that produces the problem or something like it? – Panagiotis Kanavos Feb 15 '19 at 12:54
  • @PanagiotisKanavos Thank you for your support , sorry for my mistake, I posted the different code , But i tried both the way like using (FileStream templateExcelStream = File.OpenRead(templateExcel)) using (ExcelPackage copyExcel = new ExcelPackage(templateExcelStream)) And using (ExcelPackage copyExcel = new ExcelPackage(templateFile, intialInfo)) Both are creating the same problem. Yes, the ExcelWorksheet presentWorkSheet = copyExcel.Workbook.Worksheets["Name"]; is exist. – Mahfuzur Rahman Feb 19 '19 at 08:32
  • https://stackoverflow.com/questions/35450653/epplus-copy-worksheet-from-template-to-another-excelpackage-doesnt-work-c The link inside this post for the image is same as my problem – Mahfuzur Rahman Feb 19 '19 at 08:48

1 Answers1

1

Thanks all of you for your valuable time. I got the solution. For me the issue was in the template itself as it contained invalid references to lookup tables. I found this in Formula -> Name Manager.

I suggest that you check the template if you face this issue.