Click here to Skip to main content
15,569,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
I am trying to create a new excel sheet(xlsx) from existing excel sheet(xlsx) using EPPlus.

My existing excel sheet contains 4 worksheets and I want to update some cells in new excel sheet and save it in new location.

Here is my code


C#
   public string RunSample22(DirectoryInfo outputDir)
{
    FileInfo tempFile = new FileInfo(@"F:\bin\Debug\ReportTemplateFile.xlsx");

    // Making a new file 'Sample2.xlsx'
    FileInfo newFile = new FileInfo(@"E:\Test" + @"\Sample22.xlsx");

    if (newFile.Exists)
    {
        newFile.Delete();
        newFile = new FileInfo(outputDir.FullName + @"\Sample22.xls");
    }

    using (ExcelPackage package = new ExcelPackage(newFile, tempFile )) // getting error in this line
    {
        // Openning first Worksheet of the template file i.e. 'Sample1.xlsx'
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

        worksheet.Cells[1, 1].Value = "Test";

        // Save our new workbook
        package.Save();
    }
    return newFile.FullName;
}


SQL
And Getting below error in my catch block.

"File contains corrupted data."

What I am missing or any suggestions.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900