zoukankan      html  css  js  c++  java
  • Aspose.cellls 的基本使用方法使用

     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);
            }
           
        }

    //调用
         public ActionResult RecordProcessGuide(Guid id)
            {
                //工艺指示信息
                List<DB_ProductPkgInfo> info = WMFactory.DBProductPkgInfo.FindByConditions(null, f => f.PdtId == id).ToList();
                List<DB_ProductPkgProc> list = WMFactory.DBProductPkgProc.FindByConditions(o => o.OrderBy(x => x.CreateTime), f => f.PdtId == id).ToList();
                List<DB_ProductPkgInfo> info_ = new List<DB_ProductPkgInfo>();
                Dictionary<string, object> dic = new Dictionary<string, object>();

                if (info.Count==0)
                {
                    string key = RuleDefaultCfgKey.B_BOM_CJYQ.ToString();
                    string key1 = RuleDefaultCfgKey.B_BOM_HDGX.ToString();
                    string key2 = RuleDefaultCfgKey.B_BOM_YSBZ.ToString();
                    string[] str = new string[] { key, key1, key2 };
                    DB_ProductPkgInfo dB_ = new DB_ProductPkgInfo();
                    for (int i = 0; i < str.Length; i++)
                    {
                        FX_BasicData defaultVal = WMFactory.FXBasicData.GetDataByGroup(SysBasicDataGroup.通用默认值.ToString()).FirstOrDefault(r => r.CfgKey == str[i]);
                        if (i == 0)
                        {
                            dB_.CutReqInfo = defaultVal.CfgDesc;//裁剪要求
                        }
                        if (i == 1)
                        {
                            dB_.BEOLInfo = defaultVal.CfgDesc;
                        }
                        if (i == 2)
                        {
                            dB_.ACLInfo = defaultVal.CfgDesc;
                        }
                    }
                    info_.Add(dB_);
                    dic.Add("info", info_);
                }
                else
                {
                    dic.Add("info", info);
                }
                dic.Add("list", list);
                //添加序号
                List<int> ls = new List<int>();
                for (int i = 0; i < list.Count; i++)
                {
                    ls.Add(i);
                }
                dic.Add("index", ls);
                string templateFile = HostingEnvironment.MapPath("~/Content/Uploads/ExcelTpl/ProcessGuide.xlsx");
                string saveFileName = $"工艺指示单{DateTime.Today.ToString("yyyy年MM月dd日")}.xlsx";
                AsposeCellsHelper.ResponseCell(Response, saveFileName, dic, templateFile, null, null, ls);

                return View();
            }
  • 相关阅读:
    20155217 2016-2017-2《java程序设计》第一周学习总结
    20155217杨笛-安装虚拟机
    20155217-杨笛-c与java
    我所期望的师生关系
    jQ学习之实现全选全不选操作
    jQ学习之实现表格的隔行换色
    jQ学习之过滤选择器的测试
    jQ学习之层级选择器的测试
    jQ学习之基础选择器的测试
    jQ学习之实现图片的定时弹出
  • 原文地址:https://www.cnblogs.com/cloudcmm/p/10773931.html
Copyright © 2011-2022 走看看