2

I am trying to import an xlsm file using EPPlus. I found the link that said EPPlus doesn't support this : Convert XLSM to XLSX . It's very old so I wanted to know if it supports it now ? If not, is there any way to do this with EPPlus by converting to XLSX but the way should be independent on Microsoft updates and just dependent on libraries used at the time of building the program. My current code looks like this :

FileInfo file = new FileInfo(Path.Combine(Program.source_path, s));
   using (ExcelPackage package = new ExcelPackage(file))
   {
       ExcelWorksheet worksheet = package.Workbook.Worksheets[3];
       for (int row = 9; row <= worksheet.Dimension.End.Row; row++)
       {
       }
   }
Ankur Garg
  • 23
  • 1
  • 3

1 Answers1

2

This will copy all content from the source xlsm file to a target xlsx file.

      using (var target = new ExcelPackage(new System.IO.FileInfo("D:\\target.xlsx")))
        {
            using (var source = new ExcelPackage(new System.IO.FileInfo("D:\\source.xlsm")))
            {

                foreach (var worksheet in source.Workbook.Worksheets)
                {
                    target.Workbook.Worksheets.Add(worksheet.Name, worksheet);
                }
            }
            target.Save();
        }
Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
  • You should put the target ExcelPackage in a using statement too. This is the best EPPlus solution so far, but I tried and I lose the Table formating in a range (when I opened the Excel, it says it found a problem and tries to recover, then the table is gone.) – Magnetron Dec 22 '17 at 18:09
  • I am getting the exception : Value cannot be null.\r\nParameter name: encoder .. everytime – Ankur Garg Dec 26 '17 at 14:00
  • Strange, I have tested it, what line is giving you this exception? – Yahya Hussein Dec 26 '17 at 14:16
  • It's really strange. The error just changed when trying to debug: "Root element is missing." My code looks like same. This error is at 'foreach' statement. – Ankur Garg Dec 26 '17 at 14:21
  • seems like source file is corrupted, what version of EPPLUS are u using? – Yahya Hussein Dec 26 '17 at 15:11
  • Probably, the file is corrupted. The latest version for EPPlus on nuget..It's v4.1.1.. I just tried the ExcelDataReader nuget. That one worked. We can still try to figure out this issue..!!! – Ankur Garg Dec 26 '17 at 15:46
  • so it is failing to get source.Workbook.Worksheets? – Yahya Hussein Dec 26 '17 at 15:54
  • Yes. It is failing on that step. – Ankur Garg Dec 26 '17 at 17:23
  • is it still "root element is missing" ? this is an xml error, maybe your file's xml needs to be fixed before you can read it – Yahya Hussein Dec 26 '17 at 17:32