使用C#EPPLUS库填充SERFF模板XLSM文件?

时间:2015-05-04 11:48:59

标签: c# .net excel excel-vba epplus vba

如何使用EPPLUS Library填充包含MACROS的SERFF模板。 当我尝试使用此代码编辑模板时,SERFF模板崩溃 这里是我用于编辑和导出模板的代码,我们可以从http://www.serff.com/plan_management_data_templates.htm

获取SERFF模板
FileInfo newFile = new FileInfo(@"D:\FFM Sharred\SERFF-PlansBenefits.xlsm");

ExcelPackage pck = new ExcelPackage(newFile);

var ws = pck.Workbook.Worksheets.First();

ws.SetValue("B2", "33333");

Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";           
Response.AddHeader("content-disposition", "attachment;  filename=Sample4.xlsm"); 

1 个答案:

答案 0 :(得分:1)

这对我有用。

  1. 使用已保存的宏创建xlsm模板
  2. 制作副本
  3. 使用所需数据填写副本
  4. 在复制的Excel中填写数据后执行宏。

    FileInfo templateFile = new FileInfo(@"D:\DOTNET\navigatetest.xlsm");
    FileInfo newFile = new FileInfo(@"D:\DOTNET\navigatetest11.xlsm");
    if (newFile.Exists)
    {
        newFile.Delete();
        newFile = new FileInfo(@"D:\DOTNET\navigatetest11.xlsm");
    }
    using (ExcelPackage package = new ExcelPackage(newFile, templateFile))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        String ConnectionString = "SERVER=localhost;DATABASE=excelmaps;UID=root;PASSWORD=;";
        MySqlConnection connection = new MySqlConnection(ConnectionString);
        connection.Open();
        MySqlCommand cmd = new MySqlCommand("Select * FROM `sheet1`", connection);
        MySqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(reader);
        DataRow row1 = dt.NewRow();
        worksheet.Cells["A1"].LoadFromDataTable(dt, true);       
        package.Save();
    

    }