コード例 #1
0
        private void _wallRptItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            List <Model.IReport> newItems;

            #region test output for graph

            #region preparing job, unknown
            if (e.NewItems != null)
            {
                newItems = e.NewItems.Cast <Model.IReport>().ToList();
            }
            else
            {
                newItems = new List <Model.IReport>();
            }
            List <Model.IReport> oldItems;
            if (e.OldItems != null)
            {
                oldItems = e.OldItems.Cast <Model.IReport>().ToList();
            }
            else
            {
                oldItems = new List <Model.IReport>();
            }

            WallReportContent    tmpContent;
            WallMtbReportContent tmpMtbContent;
            #endregion

            #region reflection to action
            switch (e.Action)
            {
            case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                for (int i = 0; i < newItems.Count; i++)
                {
                    //WallMtbContent.Clear();
                    tmpContent    = new WallReportContent();
                    tmpMtbContent = new WallMtbReportContent();
                    #region oxyone
                    if (newItems[i] is Model.IOxyReport)
                    {     //handle the oxy type output
                        Model.IOxyReport rpt = newItems[i] as Model.IOxyReport;
                        for (int j = 0; j < rpt.OxyContents.Count; j++)
                        {
                            Model.IOxyRptOutput output = rpt.OxyContents[j];
                            switch (output.OxyOType)
                            {
                            case Dashboard.Model.OxyOType.GRAPH:
                                switch (output.Tag.ToString())
                                {
                                case "Bar":
                                    tmpContent.BarChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    break;

                                case "Pie":
                                    tmpContent.PieChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    break;

                                case "Individual":
                                    tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    break;

                                case "MR":
                                    tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    break;

                                default:
                                    break;
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        tmpContent.Title   = rpt.Title;
                        tmpContent.RawData = rpt.RawData;
                        WallContent.Add(tmpContent);
                    }
                    #endregion

                    #region mtb one
                    else     // handle the MTB type rpt
                    {
                        Model.IReport rpt = newItems[i];
                        for (int j = 0; j < rpt.Contents.Count; j++)
                        {
                            Model.IRptOutput output = rpt.Contents[j];
                            switch (output.OType)
                            {
                            case Dashboard.Model.MtbOType.GRAPH:
                                switch (output.Tag.ToString())
                                {
                                case "TSPlot":
                                    tmpContent.TSPlot    = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    tmpMtbContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
                                    break;

                                default:
                                    break;
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        tmpContent.Title   = rpt.Title;
                        tmpContent.RawData = rpt.RawData;
                        WallContent.Add(tmpContent);

                        tmpMtbContent.Title   = rpt.Title;
                        tmpMtbContent.RawData = rpt.RawData;
                        WallMtbContent.Add(tmpMtbContent);
                    }
                    #endregion
                }
                break;

            case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                WallContent.Clear();
                break;

            case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                //need to add some Code for handling oxy and mtb, need testing => no need?
                break;

            case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
            case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
            default:
                break;
            }
            #endregion

            #endregion

            #region successful one(close)
            //List<Model.IOxyReport> newItems;

            //if (e.NewItems != null)
            //{
            //    newItems = e.NewItems.Cast<Model.IOxyReport>().ToList();
            //}
            //else
            //{
            //    newItems = new List<Model.IOxyReport>();
            //}
            //List<Model.IOxyReport> oldItems;
            //if (e.OldItems != null)
            //{
            //    oldItems = e.OldItems.Cast<Model.IOxyReport>().ToList();
            //}
            //else
            //{
            //    oldItems = new List<Model.IOxyReport>();
            //}
            //WallReportContent tmpContent;
            //switch (e.Action)
            //{
            //    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
            //        for (int i = 0; i < newItems.Count; i++)
            //        {
            //            Model.IOxyReport rpt = newItems[i];
            //            tmpContent = new WallReportContent();
            //            for (int j = 0; j < rpt.OxyContents.Count; j++)
            //            {
            //                Model.IOxyRptOutput output = rpt.OxyContents[j];
            //                switch (output.OxyOType)
            //                {
            //                    case Dashboard.Model.OxyOType.GRAPH:
            //                        switch (output.Tag.ToString())
            //                        {
            //                            case "Bar":
            //                                tmpContent.BarChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //                                break;
            //                            case "Pie":
            //                                tmpContent.PieChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //                                break;
            //                            case "Individual":
            //                                tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //                                break;
            //                            case "MR":
            //                                tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //                                break;
            //                            default:
            //                                break;
            //                        }
            //                        break;
            //                    default:
            //                        break;
            //                }
            //            }
            //            tmpContent.Title = rpt.Title;
            //            tmpContent.RawData = rpt.RawData; //?
            //            WallContent.Add(tmpContent);
            //        }
            //        break;
            //    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
            //        WallContent.Clear();
            //        break;
            //    case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
            //        #region after testing add, could be open
            //        //for (int i = 0; i < newItems.Count; i++)
            //        //{
            //        //    Model.IOxyReport rpt = newItems[i];
            //        //    tmpContent = new WallReportContent();
            //        //    for (int j = 0; j < rpt.Contents.Count; j++)
            //        //    {
            //        //        Model.IOxyRptOutput output = rpt.OxyContents[j];
            //        //        switch (output.OxyOType)
            //        //        {
            //        //            case Dashboard.Model.OxyOType.GRAPH:
            //        //                switch (output.Tag.ToString())
            //        //                {
            //        //                    case "Bar":
            //        //                        tmpContent.BarChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //        //                        break;
            //        //                    case "Pie":
            //        //                        tmpContent.PieChart = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //        //                        break;
            //        //                    case "Individual":
            //        //                        tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //        //                        break;
            //        //                    case "MR":
            //        //                        tmpContent.TSPlot = Tool.BinaryToWPFImage(output.OutputInByteArr);
            //        //                        break;
            //        //                    default:
            //        //                        break;
            //        //                }
            //        //                break;
            //        //            default:
            //        //                break;
            //        //        }
            //        //    }
            //        //    tmpContent.Title = rpt.Title;
            //        //    tmpContent.RawData = rpt.RawData; //?
            //        //    WallContent[e.OldStartingIndex + i] = tmpContent;
            //        //}
            //        #endregion
            //        break;
            //    case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
            //    case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
            //    default:
            //        break;
            //}
            #endregion

            RaisePropertyChanged("_wallRptItems");
        }
コード例 #2
0
        /// <summary>
        /// 將可簡單列舉的報表匯出成 MS Word 檔
        /// </summary>
        /// <param name="rpt"></param>
        /// <returns></returns>
        public static bool ExportMSWordReport(IEnumerable <Model.IReport> rpt)
        {
            #region rawversion
            //Word.Application wdApp = null;
            //try
            //{
            //    wdApp = new Word.Application();
            //    wdApp.Visible = true;
            //    Word.Document doc = wdApp.Documents.Add();
            //    Word.Range rng;
            //    foreach (Model.IReport item in rpt)
            //    {
            //        rng = doc.Paragraphs.Add().Range;
            //        rng.InsertAfter(item.Title);
            //        rng.Font.Size = 14;
            //        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //        foreach (Model.IRptOutput output in item.Contents)
            //        {
            //            rng.InsertParagraphAfter();
            //            rng = doc.Paragraphs[doc.Paragraphs.Count].Range;
            //            rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //            rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            //            rng.Font.Size = 10;
            //            if (output.OutputInByteArr == null) { continue; }
            //            switch (output.OType)
            //            {
            //                case Dashboard.Model.MtbOType.GRAPH:
            //                    Image img = Tool.BinaryToImage(output.OutputInByteArr);
            //                    System.Windows.Clipboard.SetDataObject(img);
            //                    rng.Paste();
            //                    var shape = rng.InlineShapes[rng.InlineShapes.Count];
            //                    shape.Width = doc.PageSetup.PageWidth * 0.7f;
            //                    break;
            //                case Dashboard.Model.MtbOType.PARAGRAPH:

            //                    rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
            //                    rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
            //                    rng.ParagraphFormat.LineSpacing = 12f;
            //                    rng.InsertAfter(System.Text.Encoding.UTF8.GetString(output.OutputInByteArr));
            //                    break;
            //                case Dashboard.Model.MtbOType.TABLE:
            //                    DataTable dt = Tool.BinaryToDataTable(output.OutputInByteArr);
            //                    Word.Table tbl = CreateWordTableWithDataTable(dt, rng);
            //                    break;
            //                case Dashboard.Model.MtbOType.TITLE:
            //                    break;
            //                default:
            //                    break;
            //            }
            //        }


            //    }
            //}
            //catch (Exception ex)
            //{
            //    System.Windows.MessageBox.Show( ex.HResult.ToString() + "-" +ex.Message,"", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
            //    return false;
            //}
            //return true;
            #endregion

            #region revised version
            //Word.Application wdApp = null;
            //try
            //{
            //    wdApp = new Word.Application();
            //    wdApp.Visible = true;
            //    Word.Document doc = wdApp.Documents.Add();
            //    Word.Range rng;
            //    if (rpt.ElementAt(0) is Model.IOxyReport)
            //    {
            //        foreach (Model.IOxyReport item in rpt)
            //        {
            //            rng = doc.Paragraphs.Add().Range;
            //            //rng.InsertAfter(item.Title);
            //            rng.Font.Size = 14;
            //            rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //            foreach (Model.IOxyRptOutput output in item.OxyContents)
            //            {
            //                rng.InsertParagraphAfter();
            //                rng = doc.Paragraphs[doc.Paragraphs.Count].Range;
            //                rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //                rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            //                rng.Font.Size = 10;
            //                if (output.OutputInByteArr == null) { continue; }
            //                switch (output.OxyOType)
            //                {
            //                    case Dashboard.Model.OxyOType.GRAPH:
            //                        Image img = Tool.BinaryToImage(output.OutputInByteArr);
            //                        System.Windows.Clipboard.SetDataObject(img);
            //                        rng.Paste();
            //                        var shape = rng.InlineShapes[rng.InlineShapes.Count];
            //                        shape.Width = doc.PageSetup.PageWidth * 0.7f;
            //                        break;
            //                    case Dashboard.Model.OxyOType.TABLE:
            //                        DataTable dt = Tool.BinaryToDataTable(output.OutputInByteArr);
            //                        Word.Table tbl = CreateWordTableWithDataTable(dt, rng);
            //                        break;
            //                    default:
            //                        break;
            //                }
            //            }
            //        }
            //    }
            //    else //here is the original code for Mtb
            //    {
            //        foreach (Model.IReport item in rpt)
            //        {
            //            rng = doc.Paragraphs.Add().Range;
            //            rng.InsertAfter(item.Title);
            //            rng.Font.Size = 14;
            //            rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //            foreach (Model.IRptOutput output in item.Contents)
            //            {
            //                rng.InsertParagraphAfter();
            //                rng = doc.Paragraphs[doc.Paragraphs.Count].Range;
            //                rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //                rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            //                rng.Font.Size = 10;
            //                if (output.OutputInByteArr == null) { continue; }
            //                switch (output.OType)
            //                {
            //                    case Dashboard.Model.MtbOType.GRAPH:
            //                        Image img = Tool.BinaryToImage(output.OutputInByteArr);
            //                        System.Windows.Clipboard.SetDataObject(img);
            //                        rng.Paste();
            //                        var shape = rng.InlineShapes[rng.InlineShapes.Count];
            //                        shape.Width = doc.PageSetup.PageWidth * 0.7f;
            //                        break;
            //                    case Dashboard.Model.MtbOType.PARAGRAPH:

            //                        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
            //                        rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
            //                        rng.ParagraphFormat.LineSpacing = 12f;
            //                        rng.InsertAfter(System.Text.Encoding.UTF8.GetString(output.OutputInByteArr));
            //                        break;
            //                    case Dashboard.Model.MtbOType.TABLE:
            //                        DataTable dt = Tool.BinaryToDataTable(output.OutputInByteArr);
            //                        Word.Table tbl = CreateWordTableWithDataTable(dt, rng);
            //                        break;
            //                    case Dashboard.Model.MtbOType.TITLE:
            //                        break;
            //                    default:
            //                        break;
            //                }
            //            }
            //        }
            //    }

            //}
            //catch (Exception ex)
            //{
            //    System.Windows.MessageBox.Show(ex.HResult.ToString() + "-" + ex.Message, "", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
            //    return false;
            //}
            //return true;
            #endregion

            #region revised version II
            //write a method which could handle both mtb and oxy reports in ReportItems, not only one kinds
            Word.Application wdApp = null;
            try
            {
                wdApp         = new Word.Application();
                wdApp.Visible = true;
                Word.Document doc = wdApp.Documents.Add();
                Word.Range    rng;
                foreach (var item in rpt)
                {
                    #region if item is Model.IOxyReport
                    if (item is Model.IOxyReport)
                    {
                        Model.IOxyReport itemTrans = item as Model.IOxyReport;
                        rng = doc.Paragraphs.Add().Range;
                        //rng.InsertAfter(item.Title);
                        rng.Font.Size = 14;
                        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        foreach (Model.IOxyRptOutput output in itemTrans.OxyContents)
                        {
                            rng.InsertParagraphAfter();
                            rng = doc.Paragraphs[doc.Paragraphs.Count].Range;
                            rng.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
                            rng.Font.Size = 10;
                            if (output.OutputInByteArr == null)
                            {
                                continue;
                            }
                            switch (output.OxyOType)
                            {
                            case Dashboard.Model.OxyOType.GRAPH:
                                Image img = Tool.BinaryToImage(output.OutputInByteArr);
                                System.Windows.Clipboard.SetDataObject(img);
                                rng.Paste();
                                var shape = rng.InlineShapes[rng.InlineShapes.Count];
                                shape.Width = doc.PageSetup.PageWidth * 0.7f;
                                break;

                            case Dashboard.Model.OxyOType.TABLE:
                                DataTable  dt  = Tool.BinaryToDataTable(output.OutputInByteArr);
                                Word.Table tbl = CreateWordTableWithDataTable(dt, rng);
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    #endregion
                    #region if item is Model.IReport
                    else
                    {
                        Model.IReport itemTrans = item as Model.IReport;
                        rng = doc.Paragraphs.Add().Range;
                        rng.InsertAfter(item.Title);
                        rng.Font.Size = 14;
                        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        foreach (Model.IRptOutput output in itemTrans.Contents)
                        {
                            rng.InsertParagraphAfter();
                            rng = doc.Paragraphs[doc.Paragraphs.Count].Range;
                            rng.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
                            rng.Font.Size = 10;
                            if (output.OutputInByteArr == null)
                            {
                                continue;
                            }
                            switch (output.OType)
                            {
                            case Dashboard.Model.MtbOType.GRAPH:
                                Image img = Tool.BinaryToImage(output.OutputInByteArr);
                                System.Windows.Clipboard.SetDataObject(img);
                                rng.Paste();
                                var shape = rng.InlineShapes[rng.InlineShapes.Count];
                                shape.Width = doc.PageSetup.PageWidth * 0.7f;
                                break;

                            case Dashboard.Model.MtbOType.PARAGRAPH:

                                rng.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                                rng.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
                                rng.ParagraphFormat.LineSpacing     = 12f;
                                rng.InsertAfter(System.Text.Encoding.UTF8.GetString(output.OutputInByteArr));
                                break;

                            case Dashboard.Model.MtbOType.TABLE:
                                DataTable  dt  = Tool.BinaryToDataTable(output.OutputInByteArr);
                                Word.Table tbl = CreateWordTableWithDataTable(dt, rng);
                                break;

                            case Dashboard.Model.MtbOType.TITLE:
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.HResult.ToString() + "-" + ex.Message, "", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                return(false);
            }
            return(true);

            #endregion
        }