【C#】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();        
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值