Exemplo n.º 1
0
        private static void GenerateDocument(string filePath)
        {
            using (FileStream stream = File.OpenWrite(filePath))
            {
                using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream))
                {
                    // Creating a style which would be used later in the code.
                    SpreadCellStyle style = workbook.CellStyles.Add("MyStyle");
                    style.Underline         = SpreadUnderlineType.DoubleAccounting;
                    style.VerticalAlignment = SpreadVerticalAlignment.Top;

                    using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("My sheet"))
                    {
                        // It is mandatory to export the worksheet view state before filling the worksheet with data.
                        using (IWorksheetViewExporter worksheetView = worksheet.CreateWorksheetViewExporter())
                        {
                            worksheetView.SetFirstVisibleCell(3, 0);

                            worksheetView.AddSelectionRange(9, 0, 13, 6);
                            worksheetView.SetActiveSelectionCell(11, 3);
                        }

                        // It is mandatory to export the column setting before exporting the row and cell data.
                        worksheet.SkipColumns(1);
                        using (IColumnExporter column = worksheet.CreateColumnExporter())
                        {
                            column.SetWidthInPixels(80);
                        }

                        worksheet.SkipRows(3);
                        using (IRowExporter row = worksheet.CreateRowExporter())
                        {
                            row.SkipCells(3);
                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetValue("Merged cell.");
                                cell.SetFormat(new SpreadCellFormat()
                                {
                                    CellStyle           = style,
                                    HorizontalAlignment = SpreadHorizontalAlignment.Center,
                                    VerticalAlignment   = SpreadVerticalAlignment.Center
                                });
                            }
                        }

                        using (IRowExporter row = worksheet.CreateRowExporter())
                        {
                            row.SetHeightInPixels(200);
                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetValue(123.456);
                            }

                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                SpreadCellFormat format = new SpreadCellFormat()
                                {
                                    NumberFormat = "dd/mm/yyyy",
                                    IsBold       = true
                                };
                                cell.SetFormat(format);
                                cell.SetValue(42370);
                            }
                        }

                        worksheet.MergeCells(3, 3, 6, 6);
                    }
                }
            }

            Console.WriteLine("Document generated.");

            ProcessStartInfo psi = new ProcessStartInfo()
            {
                FileName        = filePath,
                UseShellExecute = true
            };

            Process.Start(psi);
        }
        private async void GenerateDocument()
        {
            var maxTitleCharCount  = this.Source.Max(p => p.Title.Length);
            var maxAuthorCharCount = this.Source.Max(p => p.Author.Length);

            using (MemoryStream stream = new MemoryStream())
            {
                using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream))
                {
                    using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet1"))
                    {
                        using (IWorksheetViewExporter viewExporter = worksheet.CreateWorksheetViewExporter())
                        {
                            // just moving the selection so the bottom border of the header row is visible
                            viewExporter.AddSelectionRange(0, 3, 0, 3);
                        }

                        using (IColumnExporter titleColumn = worksheet.CreateColumnExporter())
                        {
                            titleColumn.SetWidthInCharacters(maxTitleCharCount);
                        }

                        using (IColumnExporter authorColumn = worksheet.CreateColumnExporter())
                        {
                            authorColumn.SetWidthInCharacters(maxAuthorCharCount);
                        }

                        using (IRowExporter row = worksheet.CreateRowExporter())
                        {
                            SpreadCellFormat headerformat = new SpreadCellFormat();
                            headerformat.CellStyle           = workbook.CellStyles["Heading 1"];
                            headerformat.HorizontalAlignment = SpreadHorizontalAlignment.Center;

                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetValue("Books");
                                cell.SetFormat(headerformat);
                            }

                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetFormat(headerformat);
                            }
                        }

                        using (IRowExporter row = worksheet.CreateRowExporter())
                        {
                            SpreadCellFormat subHeaderformat = new SpreadCellFormat();
                            subHeaderformat.CellStyle = workbook.CellStyles["Heading 2"];

                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetValue("Title");
                                cell.SetFormat(subHeaderformat);
                            }

                            using (ICellExporter cell = row.CreateCellExporter())
                            {
                                cell.SetValue("Author");
                                cell.SetFormat(subHeaderformat);
                            }
                        }

                        for (int i = 0; i < this.Source.Count; i++)
                        {
                            Book book = this.Source[i];

                            string styleName = i % 2 == 0 ? "20% - Accent1" : "20% - Accent2";

                            SpreadCellFormat format = new SpreadCellFormat();
                            format.CellStyle = workbook.CellStyles[styleName];

                            using (IRowExporter row = worksheet.CreateRowExporter())
                            {
                                using (ICellExporter cell = row.CreateCellExporter())
                                {
                                    cell.SetValue(book.Title);
                                    cell.SetFormat(format);
                                }

                                using (ICellExporter cell = row.CreateCellExporter())
                                {
                                    cell.SetValue(book.Author);
                                    cell.SetFormat(format);
                                }
                            }
                        }

                        worksheet.MergeCells(0, 0, 0, 1);
                    }
                }

                await DependencyService.Get <IXlsxFileViewer>().View(stream, "GettingStarted.xlsx");
            }
        }