使用 epplus 创建 .xlsm 文件

Use epplus to create .xlsm file

我正在尝试让网站导出 .xlsm 文件,但我似乎找不到任何有用的东西。这是我用来测试构建 .xlsx 文件(有效)的简单示例。

@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>

将文件扩展名更改为 .xlsm 后生成了文件,但是当我尝试在 Excel 中打开文件时,我收到一条错误消息,指出扩展名不正确。我认为我唯一需要更改的是内容类型 header 但这显然不是问题所在。我还缺少什么???任何指导将不胜感激!

Xiaoy312 解决了这个问题!在 Byte[] bin = p.GetAsByteArray(); 之前添加 p.Workbook.CreateVBAProject(); 解决了我的问题!其他一切都保持不变,但 Excel 现在将实际打开文件!对于遇到相同问题的任何人,这是我的最终代码:

@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>