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