記錄一下WPF開發(三)--- 基於Word書籤的XPS報表

製作報表可有很多方案,這裏記錄一種C#版的報表方案:利用Word做好模板,併爲動態內容添加書籤,然後通過程序另存爲後綴名爲XPS的文件。爲什麼要XPS的報表呢,第一是因爲Win7以上的系統都自帶,XP安裝也比較方便,第二報表效果非常好看,還可設置簽名。

Word製作模板,並添加書籤在此略過。

提供一個Word模板處理工具,具體思路是,

1.根據路徑打開模塊,然後創建一個臨時文件,因爲Word程序是直接操作原模板,會改變模板的結構,所以要先檢查是否存在,如果存在的話先刪掉,再複製原模板與臨時文件中,再進行處理。

2.找到書籤,寫入數據

3.另存爲xps文件

ReportUtil.cs如下

class ReportUtil
    {

        private _Application wordApp = null;
        private _Document wordDoc = null;

               public _Application Application
        {
            get { return wordApp; }
            set 
            {
                wordApp = value;
 
            }
        }
        public _Document Document
        {
            get
            {
                return wordDoc;
            }
            set
            {
                wordDoc = value;
            }

        }

               public void CreateNewDocument(string filePath)
        {
           

            killWinWordProcess();

            wordApp = new ApplicationClass();
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            wordApp.Visible = false;

            object missing = System.Reflection.Missing.Value;

            //String path = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName.ToString();


            string TempTemplateFile = Session.EvenDir + @"/WordTemplate/Temp.doc";  
            if(File.Exists(TempTemplateFile))
            {
                File.Delete(TempTemplateFile);
            }

             File.Copy(filePath, TempTemplateFile);


             object templateName = TempTemplateFile;



            wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing);

           
        }

               public bool SaveDocument(string filePath)
        {
            bool result = true;

            object fileName = filePath;
            object format = WdSaveFormat.wdFormatXPS;         
            object miss = System.Reflection.Missing.Value;

            try
            {
                wordDoc.SaveAs(ref fileName, ref format, ref miss,
                ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss, ref miss, ref miss,
                ref miss);
            }
            catch (Exception e)
            {
                MessageBox.Show("已經開啓一個報表");
                result = false;
            }
            
                                  //object miss1 = System.Reflection.Missing.Value;

                                                        //    ref miss);

            object SaveChanges = WdSaveOptions.wdSaveChanges;
            object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
            object RouteDocument = false;
            wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
            wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

            killWinWordProcess();
            return result;
        }

         public bool InsertValue(string bookmark, string value)
        {
            object bkObj = bookmark;
            if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
            {
                wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
                wordApp.Selection.TypeText(value);
                return true;
            }
            return false;
        }

         public void killWinWordProcess()
        {
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
            foreach (System.Diagnostics.Process process in processes)
            {
                bool b = process.MainWindowTitle == "";
                if (process.MainWindowTitle == "")
                {
                    process.Kill();
                }
            }
        }


    }

調用方式爲

 string filePath = Session.EvenDir + @"/WordTemplate/template.doc";
                ReportUtil report = new ReportUtil();
                report.CreateNewDocument(filePath);
                Dictionary<String, String> d = ClassProperty.GetProperties(t);
                foreach (KeyValuePair<String, String> a in d)
                {
                    report.InsertValue(a.Key, a.Value);
                }

                
                bool b = report.SaveDocument(Session.EvenDir + @"\WordTemplate\report");
                if (b)
                {

                     Process.Start("xpsrchvw.exe", Session.EvenDir + @"\WordTemplate\report.xps");
                }

生成xps文件後,調用系統的xps程序,打開該xps文件。

ps:ClassProperty是一個工具,熟悉獲取器,通過傳人一個泛型類,獲取到該類的所有字段的屬性和名稱,並以hash的方式返回

發佈了46 篇原創文章 · 獲贊 5 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章