示例#1
0
        public void CreateFile(Window owner)
        {
            try
            {
                // Set license key to use GemBox.Spreadsheet in Free mode.
                SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

                _ef = new ExcelFile();
                ExcelWorksheet ws = _ef.Worksheets.Add("Report");

                DataTable data = GetData();

                var headers = data.AsEnumerable()
                              .Select(s => s.Field <string>("Name"))
                              .ToArray();

                var cellsData = data.AsEnumerable()
                                .Select(s => s.Field <int>("Type"))
                                .ToArray();

                CellStyle tmpStyle = new CellStyle();
                tmpStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center;
                tmpStyle.VerticalAlignment   = VerticalAlignmentStyle.Center;
                tmpStyle.Font.Weight         = ExcelFont.BoldWeight;
                tmpStyle.Font.Color          = System.Drawing.Color.Black;
                tmpStyle.Rotation            = 60;

                int i = 1;
                // Write header data to Excel cells.
                foreach (var header in headers)
                {
                    ws.Cells[1, i].Value = header;
                    ws.Columns[i].AutoFit(1, ws.Rows[1], ws.Rows[2]);
                    ws.Cells[1, i].Style = tmpStyle;
                    ws.Cells[1, i].Style.Borders.SetBorders(MultipleBorders.All, SpreadsheetColor.FromName(ColorName.Black), LineStyle.Thin);
                    i++;
                }

                tmpStyle = new CellStyle();
                tmpStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center;
                tmpStyle.VerticalAlignment   = VerticalAlignmentStyle.Center;
                tmpStyle.Font.Weight         = ExcelFont.NormalWeight;

                i = 1;
                // Write data to Excel cells.
                foreach (var cell in cellsData)
                {
                    ws.Cells[2, i].Value = cell;
                    ws.Cells[2, i].Style = tmpStyle;
                    ws.Cells[2, i].Style.Borders.SetBorders(MultipleBorders.All, SpreadsheetColor.FromName(ColorName.Black), LineStyle.Thin);
                    ws.Columns[i].Width = 4 * 256;
                    i++;
                }

                ws.Rows[1].AutoFit();
                ws.PrintOptions.FitWorksheetWidthToPages = 1;
                ws.PrintOptions.Portrait = false;

                _ef.Save(FileName);
                System.Windows.MessageBox.Show(owner, "File has been created successfully.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                ExcelViewer ev = new ExcelViewer(_ef.ConvertToImageSource(ImageSaveOptions.ImageDefault));
                ev.Show();

                PrintFile(owner, string.Empty);
            } catch (Exception ex)
            {
                System.Windows.MessageBox.Show(owner, string.Format("An error has occurred while creating document: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally
            {
                if (!object.Equals(null, _ef))
                {
                    _ef = null;
                }
            }
        }
        public void LoadRateCards()
        {
            if (RateCards.Any())
            {
                return;
            }

            var files = _rootFolder.GetLocalFiles().ToList();

            foreach (var rateCardFile in files.Select(f => new FileInfo(f.LocalPath)))
            {
                IRateCardViewer rateCard;
                switch (rateCardFile.Extension.ToLower())
                {
                case ".pdf":
                    try
                    {
                        rateCard = new PDFViewer(rateCardFile);
                    }
                    catch
                    {
                        rateCard = new DefaultViewer(rateCardFile);
                    }
                    break;

                case ".xls":
                case ".xlsx":
                    try
                    {
                        rateCard = new ExcelViewer(rateCardFile);
                    }
                    catch
                    {
                        rateCard = new DefaultViewer(rateCardFile);
                    }
                    break;

                case ".doc":
                case ".docx":
                    try
                    {
                        rateCard = new WordViewer(rateCardFile);
                    }
                    catch
                    {
                        rateCard = new DefaultViewer(rateCardFile);
                    }
                    break;

                case ".txt":
                    try
                    {
                        rateCard = new WebViewer(rateCardFile);
                    }
                    catch
                    {
                        rateCard = new DefaultViewer(rateCardFile);
                    }
                    break;

                default:
                    rateCard = new DefaultViewer(rateCardFile);
                    break;
                }
                RateCards.Add(rateCard);
            }
            RateCardContainer.xtraTabControlRateCards.TabPages.AddRange(RateCards.Select(x => x as XtraTabPage).ToArray());
        }