Use epplus to create .xlsm file

6.1k views Asked by At

I'm trying to have a website export a .xlsm file and I can't seem to find anything that helps. Here's a simple example I'm using to test building a .xlsx file (which works).

@using OfficeOpenXml;
<html>
    <body>
        <div id="page-wrapper">
        @{
            // Change file extension to xlsm to test
            string fileExtension = "xlsm";
            ExcelPackage p = new ExcelPackage();
            p.Workbook.Worksheets.Add("Worksheet Name");
            int LatestWorksheetNumber = p.Workbook.Worksheets.Count;
            ExcelWorksheet ws = p.Workbook.Worksheets[LatestWorksheetNumber];

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

            //Generate A File
            Byte[] bin = p.GetAsByteArray();
            string filename = "filename";
            try
            {
                //Download the file as an attachment
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cookies.Clear();

                string ContentType = "";
                if (fileExtension == "xlsx")
                {
                    ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                }
                else
                {
                    ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
                }
                Response.GetType();
                Response.ContentType = ContentType;
                Response.AddHeader("content-disposition", "attachment;  filename=" + filename + "." + fileExtension);
                Response.BinaryWrite(bin);
                Response.End();
                <p>File Created</p>
            }
            catch (Exception e)
            {
                <p>@e.Message</p>
            }

        }
    </div>
</body>
</html>

After changing the file extension to .xlsm the file is generated but when I try to open the file in Excel I get an error saying that the extension isn't correct. I thought the only thing I would have to change would be the content type header but that's obviously not the issue. What else am I missing??? Any guidance would be appreciated!

1

There are 1 answers

1
Michael On BEST ANSWER

Xiaoy312 nailed the issue! Adding p.Workbook.CreateVBAProject(); before Byte[] bin = p.GetAsByteArray(); solved my issue! Everything else stayed the same but Excel will actually open the files now! Here's my final code for anyone who has the same issue:

@using OfficeOpenXml;
<html>
    <body>
        <div id="page-wrapper">
            @{
                // Change file extension to xlsm to test
                string FileExtension = "xlsm";
                ExcelPackage p = new ExcelPackage();
                p.Workbook.Worksheets.Add("Worksheet Name");
                int LatestWorksheetNumber = p.Workbook.Worksheets.Count;
                ExcelWorksheet ws = p.Workbook.Worksheets[LatestWorksheetNumber];
            ws.Cells[1, 1].Value = "Test";

            p.Workbook.CreateVBAProject();

            //Generate A File
            Byte[] bin = p.GetAsByteArray();
            string filename = "filename";
            try
            {
                //Download the file as an attachment
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cookies.Clear();

                string ContentType = "";
                if (FileExtension == "xlsx")
                {
                    ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                }
                else
                {
                    ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
                }
                Response.GetType();
                Response.ContentType = ContentType;
                Response.AddHeader("content-disposition", "attachment;  filename=" + filename + "." + FileExtension);
                Response.BinaryWrite(bin);
                Response.End();
                <p>File Created</p>
            }
            catch (Exception e)
            {
                <p>@e.Message</p>
            }
        }
    </div>
</body>
</html>