This project has moved. For the latest updates, please go here.

Using Sample 7 as a Guide unable to load a template and save as a workbook

Aug 7, 2012 at 9:19 PM

I am using Sample 7 as a guid. I am loading an excel template .xlt and saving it as an .xlsx

I get an error when trying to open wsing Excel 2007. However, if I renamr the xlsx to a xltx excel will open it. (I tried both save ans saveas)



 OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = "xltx; *.*";
            dlg.FileName = "*.xltx";
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            FileInfo templateFile = new FileInfo(dlg.FileName);

            //FileInfo newFile = new FileInfo(Path.ChangeExtension(dlg.FileName, "xlsx"));
            string fPath = Path.GetDirectoryName(dlg.FileName);
            FileInfo newFile = new FileInfo( (Path.GetDirectoryName(dlg.FileName))+@"\testfile.xlsx");
            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook
                //newFile = new FileInfo(outputDir.FullName + @"\sample5.xlsx");
                newFile = new FileInfo((Path.GetDirectoryName(dlg.FileName)) + @"\testfile.xlsx");
            }
            using (ExcelPackage package = new ExcelPackage(newFile, templateFile))
            {
                //Open worksheet 1
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                // save our new workbook and we are done!
                package.Save();
            }

Aug 8, 2012 at 7:02 AM

Did you get the repair dialog in Excel? Did you loose any information?

Aug 8, 2012 at 3:49 PM

1) I get this error message

Excel cannot open the file 'testfile.xls' because the file format or the file extension is not valid, Verify that the file has not been corrupted and that the file extension matches the format of the file.

2) When renamed to .xltx the file didi not loose any information. In fact in amother run I wrote into some cell and that was in the new file.

Feb 1, 2013 at 6:28 AM
it appears that there is more involved in saving an XLTM file as XLSM than just changing the extension.

http://epplus.codeplex.com/discussions/387657 seems to use the MS OpenXML object, but i dont know how to implement.


I would like the EP+ object to support SaveAs with a format, as the Excel VBA workbook does.

xpression.SaveAs(FileName, FileFormat, ...