private static void SetImageSource(Image image)
    {
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();

        var worksheet = workbook.Worksheets.Add("Sheet1");

        worksheet.Cells[0, 0].Value = "English:";
        worksheet.Cells[0, 1].Value = "Hello";

        worksheet.Cells[1, 0].Value = "Russian:";
        worksheet.Cells[1, 1].Value = new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' });

        worksheet.Cells[2, 0].Value = "Chinese:";
        worksheet.Cells[2, 1].Value = new string(new char[] { '\u4f60', '\u597d' });

        worksheet.Cells[4, 0].Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC.";
        worksheet.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = true;

        worksheet.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To ImageSource / Image Control Example";

        worksheet.PrintOptions.PrintGridlines = true;

        image.Source = workbook.ConvertToImageSource(SaveOptions.ImageDefault);
    }
Exemple #2
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;
                }
            }
        }