private MemoryStream CommonPreview(int renderType, [FromBody] Template model, out string fileName) { string attachmentId = model.AttachmentId; var views = model.sheets; string errorMessage = ""; var ret = CommonExcelValidate(views, out errorMessage); if (!ret) { throw new BizException(errorMessage); //throw new HttpResponseException(new HttpResponseMessage() //{ // StatusCode = System.Net.HttpStatusCode.Conflict, // Content = new StringContent(errorMessage) //}); } var responseMessage = new HttpResponseMessage(HttpStatusCode.OK); var template = new Lib.Model.Template(); template.ID = Guid.NewGuid().ToString(); var tuple = TemplateOperator.Instance.ReadSheetDataFromBizModel(template, renderType, views); Lib.Model.Attachment attachment = null; if (!string.IsNullOrEmpty(attachmentId)) { attachment = AttachmentOperator.Instance.GetModel(attachmentId); } var ext = attachment == null ? ".xlsx" : attachment.FileExt; fileName = string.Format("PREVIEW-{0}({1}){2}", WebHelper.GetCurrentUser().CNName, WebHelper.GetCurrentUser().LoginName, ext); template.TemplateName = fileName; ExcelTempleteBuilder builder = new ExcelTempleteBuilder(); //初始化出excel对象 var wb = builder.InitExcel(renderType == 2, attachment, template, tuple.Item1, tuple.Item2, tuple.Item3); wb = TemplateOperator.Instance.WriteSampleData(wb, views, renderType); //获取文档格式 var format = TemplateOperator.Instance.GetFormatType(attachment); //写到内存流中 MemoryStream stream = new MemoryStream(); wb.Save(stream, format); return(stream); }
/// <summary> /// 通用保存方法 /// </summary> /// <param name="attachmentId">附件ID</param> /// <param name="renderType">呈现模式</param> /// <param name="viewModel">业务数据</param> /// <param name="isAdd">是否为新增</param> /// <returns></returns> private IHttpActionResult CommonSave(string attachmentId, int renderType, Template viewModel, bool isAdd) { if (string.IsNullOrEmpty(viewModel.TemplateName)) { throw new BizException("请填写模板名称"); } if (isAdd) { var exist = TemplateOperator.Instance.CheckExist(WebHelper.GetCurrentUser().LoginName, viewModel.TemplateName); if (exist) { throw new BizException("模板名称已存在,无法保存"); } } string errorMessage = ""; var views = viewModel.sheets; var ret = CommonExcelValidate(views, out errorMessage); if (!ret) { throw new BizException(errorMessage); } var userInfo = WebHelper.GetCurrentUser();; Lib.Model.Attachment attachment = null; if (!string.IsNullOrEmpty(attachmentId)) { attachment = AttachmentOperator.Instance.GetModel(attachmentId); if (attachment == null) { throw new BizException("文件查找失败,请确认文件正确已正确上传"); } } var template = new Lib.Model.Template(); if (isAdd) { template.ID = Guid.NewGuid().ToString(); template.ActualUnitID = userInfo.actualUnitID; template.ActualUnitName = userInfo.ActualUnitName; template.CreatorTime = DateTime.Now; template.CreatorLoginName = userInfo.LoginName; template.CreatorName = userInfo.CNName; template.IsDeleted = false; template.IsPrivate = 1; template.ModifierLoginName = userInfo.LoginName; template.ModifierName = userInfo.CNName; template.ModifyTime = DateTime.Now; template.OrgID = userInfo.OrgID; template.OrgName = userInfo.OrgName; template.Range = null; template.TemplatePath = ""; template.UnitID = userInfo.UnitID; template.UnitName = userInfo.UnitName; template.IsImport = renderType == 2 ? 1 : 0; template.Status = 0; } else { template = TemplateOperator.Instance.GetModel(viewModel.TemplateID); } template.TemplateName = viewModel.TemplateName; var tuple = TemplateOperator.Instance.ReadSheetDataFromBizModel(template, renderType, views); ExcelTempleteBuilder builder = new ExcelTempleteBuilder(); //初始化出excel对象 var wb = builder.InitExcel(renderType == 2, attachment, template, tuple.Item1, tuple.Item2, tuple.Item3); wb = TemplateOperator.Instance.WriteSampleData(wb, views, renderType); //获取文档格式 var format = TemplateOperator.Instance.GetFormatType(attachment); //写到内存流中 MemoryStream stream = new MemoryStream(); wb.Save(stream, format); //保存内存流到文件系统中 string templateFileName = ""; if (attachment != null) { templateFileName = attachment.Name; } else { templateFileName = string.Format("{0}.xlsx", template.TemplateName); } var model = AttachmentOperator.Instance.CommonUpload(template.ID, "UploadModelAttach", templateFileName, stream); template.TemplatePath = string.Format("{0}|{1}", model.AttachmentPath, model.FileExt); if (!isAdd) { TemplateOperator.Instance.UpdateRalationByID(template.ID); } AttachmentOperator.Instance.AddModel(model); //入库所有对象 TemplateOperator.Instance.AddModel(template); tuple.Item1.ForEach(x => { TemplateSheetOperator.Instance.AddModel(x); }); tuple.Item2.ForEach(x => { TemplateConfigOperator.Instance.AddModel(x); }); tuple.Item3.ForEach(x => { TemplateConfigSelectOperator.Instance.AddModel(x); }); //返回模板ID return(BizResult(template.ID)); }