public class AsposeCellsHelper
    {

        public static void ResponseCell(HttpResponseBase response, string saveFileName, Dictionary<string, object> datas, string templateFileName, string sheetName, Action<WorkbookDesigner, Dictionary<string, object>> action)
        {
            var stream = CellDataToStream(datas, templateFileName, sheetName, action);
            if (stream != null)
                ResponseCell(response, saveFileName, stream.ToArray());
        }
        //因为插件 是没破解 故需要授权
        public AsposeCellsHelper()//无参数的构造函数
        {
            Aspose.Cells.License li = new Aspose.Cells.License();
            li.SetLicense(@"D:\YouFanSCM\UFX.SCM.WebUI\Content\Aspose\License.lic");
        }
           
        public static void ResponseCell(HttpResponseBase response, string saveFileName, byte[] data)
        {
            if (string.IsNullOrEmpty(saveFileName))
                saveFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";//客户端保存的文件名  
            //以字符流的形式下载文件  
            response.ContentType = "application/vnd.ms-excel";
            //通知浏览器下载文件而不是打开
            response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8));
            response.BinaryWrite(data);
            response.Flush();
            response.End();
        }
        public static MemoryStream CellDataToStream(Dictionary<string, object> datas, string templateFileName, string sheetName, Action<WorkbookDesigner, Dictionary<string, object>> action)
        {
         
            WorkbookDesigner designer = new WorkbookDesigner();
          
            designer.Workbook = new Workbook(templateFileName);
            //Aspose.cells 可以绑定数据(DataTable,list 实体集合....Ilist)
            foreach (KeyValuePair<string, object> kvp in datas)
            {
                designer.SetDataSource(kvp.Key, kvp.Value);
            }
            //designer.SetDataSource("ListData", datas);
           

            //对designer做额外操作
            if (action != null)
            {
                action(designer, datas);
            }
            designer.Process();
            if (!string.IsNullOrEmpty(sheetName))
            {
                designer.Workbook.Worksheets[0].Name = sheetName;
            }
            using (MemoryStream men = new MemoryStream())
            {
                designer.Workbook.Save(men, SaveFormat.Xlsx);
                return men;
            }
            //return designer.Workbook.SaveToStream();
        }

        public static void CellDataToFile(Dictionary<string, object> datas, string templateFileName, string sheetName, Action<WorkbookDesigner, Dictionary<string, object>> action, string saveFileName)
        {
            WorkbookDesigner designer = new WorkbookDesigner();
            designer.Workbook = new Workbook(templateFileName);
            foreach (KeyValuePair<string, object> kvp in datas)
            {
                designer.SetDataSource(kvp.Key, kvp.Value);
            }
           // designer.SetDataSource("ListData", datas);

            //对designer做额外操作
            if (action != null)
            {
                action(designer, datas);
            }
            designer.Process();
            if (!string.IsNullOrEmpty(sheetName))
            {
                designer.Workbook.Worksheets[0].Name = sheetName;
            }
            designer.Workbook.Save(saveFileName, SaveFormat.Xlsx);
        }
       
    }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。