Beispiel #1
0
        public string GetDocumentLocation(Guid id)
        {
            DocumentEntity doc = GetDocument(id);

            if (doc != null && doc.Status == (int)DocumentStatus.Processed)
            {
                switch (doc.OutputType)
                {
                case (int)OutputType.Word:
                    return(string.Format("{0}.docx", doc.DocumentAddress));

                case (int)OutputType.PDF:
                    return(string.Format("{0}.pdf", doc.DocumentAddress));

                case (int)(OutputType.PDF | OutputType.Word):
                    return(string.Format("{0}.docx,{0}.pdf", doc.DocumentAddress));

                default:
                    break;
                }
            }
            return(string.Empty);
        }
 protected abstract string ConvertDocument(DocumentEntity doc);
 /// <summary>
 /// 转换方法
 /// </summary>
 /// <param name="doc"></param>
 /// <returns></returns>
 public virtual string Convert(DocumentEntity doc)
 {
     return(ConvertDocument(doc));
 }
Beispiel #4
0
        protected override string ConvertDocument(DocumentEntity doc)
        {
            object missing      = Type.Missing,
                   visible      = false,
                   documentType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument,
                   fileName     = Path.Combine(DocumentConvertConfig.TemplatePath, doc.TemplateName),
                   objFalse     = false;

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            WriteLog("启动word程序!");
            try
            {
                WriteLog(string.Format("加载模板文件!文件名为{0}!", fileName));
                Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Open(
                    ref fileName,
                    ref missing,
                    ref objFalse,
                    ref missing /* Readonly */,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref documentType /* DocumentEntity Type */,
                    ref missing,
                    ref visible /* Visible */,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing);
                if (document == null)
                {
                    WriteLog("目标文档打开失败!");
                }

                DataSource dataSource = null;
                var        serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Converters.Add(new DataSourceJsonConverter());
                serializer.Converters.Add(new DataItemJsonConverter());
                using (var sReader = new StringReader(doc.DataSource))
                {
                    using (JsonReader reader = new JsonTextReader(sReader))
                    {
                        dataSource = serializer.Deserialize <DataSource>(reader);
                    }
                }

                using (var target = new Model.Document(document))
                {
                    target.Instantiate(dataSource);
                    string path = Path.Combine(DocumentConvertConfig.OutputPath, doc.ID.ToString());
                    WriteLog("保存文件!");
                    target.Save(path, (OutputType)doc.OutputType);
                    doc.DocumentAddress = path;
                }

                doc.Status      = (int)DocumentStatus.Processed;
                doc.InfoMessage = string.Empty;

                //var result = DocumentService.Instance.UpdateDocument(doc);
                //if (result == null)
                //{
                //    WriteLog("更新数据库状态失败!");
                //}
            }
            catch (Exception ex)
            {
                doc.InfoMessage = ex.ToString();
                doc.Status      = (int)DocumentStatus.Failed;
                WriteLog(ex.ToString());
                //DocumentService.Instance.UpdateDocument(doc);
            }
            finally
            {
                wordApp.Quit(ref objFalse, ref missing, ref missing);
                WriteLog("文档处理完毕,退出word程序!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                wordApp = null;
            }

            return(string.Empty);
        }
Beispiel #5
0
        public static Guid Insert(DocumentEntity doc)
        {
            if (Exception != null)
            {
                throw Exception;
            }

            const string sql = @"INSERT INTO dbo.DocumentEntity (ID,TemplateName,OutputType,DataSource,DocumentAddress,
                                                     [Status],InfoMessage,CreateTime)Values(@ID,@TemplateName,
                                                      @OutputType,@DataSource,@DocumentAddress,@Status,@InfoMessage,
                                                     @CreateTime)";

            doc.ID = Guid.NewGuid();
            int result = 0;

            using (var conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                using (var cmd = new SqlCommand(sql, conn))
                {
                    var parameter = new SqlParameter("@ID", DbType.Guid)
                    {
                        Value = doc.ID
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@TemplateName", DbType.String)
                    {
                        Value = doc.TemplateName
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@OutputType", DbType.Int32)
                    {
                        Value = doc.OutputType
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@DataSource", DbType.String)
                    {
                        Value = doc.DataSource
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@DocumentAddress", DbType.String)
                    {
                        Value = string.Empty
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@Status", DbType.Int32)
                    {
                        Value = (int)DocumentStatus.New
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@InfoMessage", DbType.String)
                    {
                        Value = string.Empty
                    };
                    cmd.Parameters.Add(parameter);
                    parameter = new SqlParameter("@CreateTime", DbType.DateTime)
                    {
                        Value = DateTime.Now
                    };
                    cmd.Parameters.Add(parameter);
                    result = cmd.ExecuteNonQuery();
                }
            }
            if (result > 0)
            {
                return(doc.ID);
            }
            throw new ApplicationException("文档持久化失败!");
        }
Beispiel #6
0
 public Guid AddDocument(DocumentEntity model)
 {
     model.CreateTime = DateTime.Now;
     return(DocumentDal.Insert(model));
 }