Ejemplo n.º 1
0
        static void AddTable(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create columns "A", "B" and "C" and set their widths.
                    int[] widths = new int[] { 165, 120, 100 };
                    for (int i = 0; i < 3; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                            column.WidthInPixels = widths[i];
                    }

                    #region #AddTable
                    IXlTable table;
                    // Specify an array containing column headings for a table.
                    string[] columnNames = new string[] { "Product", "Category", "Amount" };

                    // Create the first row in the worksheet from which the table starts.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        // Start generating the table with a header row displayed.
                        table = row.BeginTable(columnNames, true);
                        // Specify the total row label.
                        table.Columns[0].TotalRowLabel = "Total";
                        // Specify the function to calculate the total.
                        table.Columns[2].TotalRowFunction = XlTotalRowFunction.Sum;
                        // Specify the number format for the "Amount" column and its total cell.
                        XlNumberFormat accounting = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";
                        table.Columns[2].DataFormatting     = accounting;
                        table.Columns[2].TotalRowFormatting = accounting;
                    }

                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);

                    // Create the total row and finish the table.
                    using (IXlRow row = sheet.CreateRow())
                        row.EndTable(table, true);
                    #endregion #AddTable
                }
            }
        }
 void GenerateHeaderRow(IXlSheet sheet)
 {
     string[] columnNames = new string[] { "State", "Sales", "Sales vs Target", "Profit", "Market Share" };
     // Create the header row and set its height.
     using (IXlRow row = sheet.CreateRow()) {
         row.HeightInPixels = 25;
         // Create required cells in the header row, assign values from the columnNames array to them and apply specific formatting settings.
         row.BulkCells(columnNames, headerRowFormatting);
     }
 }
Ejemplo n.º 3
0
        public void ExportToExcel()
        {
            SaveFileDialogService.Filter = "Excel files (*.xlsx)|*.xlsx";
            if (SaveFileDialogService.ShowDialog())
            {
                IXlExporter exporter    = XlExport.CreateExporter(XlDocumentFormat.Xlsx);
                string[]    columnNames = new string[] { "Word", "Definition" };
                string      fileName    = SaveFileDialogService.GetFullFileName();

                using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (IXlDocument document = exporter.CreateDocument(stream))
                    {
                        using (IXlSheet sheet = document.CreateSheet())
                        {
                            sheet.Name = "Dictionary";

                            using (IXlColumn column = sheet.CreateColumn())
                            {
                                column.WidthInCharacters    = 25;
                                column.Formatting           = new XlCellFormatting();
                                column.Formatting.Alignment = new XlCellAlignment();
                                column.Formatting.Alignment.VerticalAlignment = XlVerticalAlignment.Center;
                            }

                            using (IXlColumn column = sheet.CreateColumn())
                            {
                                column.WidthInCharacters             = 100;
                                column.Formatting                    = new XlCellFormatting();
                                column.Formatting.Alignment          = new XlCellAlignment();
                                column.Formatting.Alignment.WrapText = true;
                            }

                            IXlTable table;
                            using (IXlRow row = sheet.CreateRow())
                                table = row.BeginTable(columnNames, true);

                            foreach (DictionaryBookmark bookmark in DictionaryBookmarksView)
                            {
                                using (IXlRow row = sheet.CreateRow())
                                    row.BulkCells(new string[] { bookmark.Word, bookmark.Definition }, null);
                            }

                            using (IXlRow row = sheet.CreateRow())
                                row.EndTable(table, false);

                            table.Style.ShowFirstColumn = true;
                        }
                    }
                }

                Process.Start(fileName);
            }
        }
Ejemplo n.º 4
0
        static void TableStyle(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create columns "A", "B" and "C" and set their widths.
                    int[] widths = new int[] { 165, 120, 100 };
                    for (int i = 0; i < 3; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                            column.WidthInPixels = widths[i];
                    }

                    #region #TableStyle
                    IXlTable table;
                    // Specify an array containing column headings for a table.
                    string[] columnNames = new string[] { "Product", "Category", "Amount" };

                    // Create the first row in the worksheet from which the table starts.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        // Start generating the table with a header row displayed.
                        table = row.BeginTable(columnNames, true);

                        // Apply the table style.
                        table.Style.Name = XlBuiltInTableStyleId.Dark7;
                    }

                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    // Create the last table row and finish the table.
                    // The total row is not displayed for the table.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);
                        row.EndTable(table, false);
                    }
                    #endregion #TableStyle
                }
            }
        }
Ejemplo n.º 5
0
        static void SetDateRange(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create worksheet columns and set their widths.
                    using (IXlColumn column = sheet.CreateColumn())
                    {
                        column.WidthInPixels = 200;
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                        {
                            column.WidthInPixels = 100;
                            column.ApplyFormatting((XlNumberFormat)@"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)");
                        }
                    }

                    // Specify formatting settings for cells containing data.
                    XlCellFormatting rowFormatting = new XlCellFormatting();
                    rowFormatting.Font = XlFont.CustomFont("Century Gothic", 12.0);

                    // Specify formatting settings for the header row.
                    XlCellFormatting headerRowFormatting = new XlCellFormatting();
                    headerRowFormatting.Font         = XlFont.CustomFont("Century Gothic", 12.0);
                    headerRowFormatting.Font.Bold    = true;
                    headerRowFormatting.Font.Color   = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
                    headerRowFormatting.Fill         = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0));
                    headerRowFormatting.NumberFormat = XlNumberFormat.ShortDate;

                    object[] headerValues = new object[] { "Product", new DateTime(2015, 3, 15), new DateTime(2015, 4, 1), new DateTime(2015, 6, 1), new DateTime(2015, 10, 1), "Date Axis", "General Axis" };

                    // Generate the header row.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(headerValues, headerRowFormatting);
                    }

                    // Generate data for the document.
                    Random   random   = new Random();
                    string[] products = new string[] { "HD Video Player", "SuperLED 42", "SuperLED 50", "DesktopLED 19", "DesktopLED 21", "Projector Plus HD" };

                    foreach (string product in products)
                    {
                        using (IXlRow row = sheet.CreateRow())
                        {
                            using (IXlCell cell = row.CreateCell())
                            {
                                cell.Value = product;
                                cell.ApplyFormatting(rowFormatting);
                            }
                            for (int j = 0; j < 4; j++)
                            {
                                using (IXlCell cell = row.CreateCell())
                                {
                                    cell.Value = Math.Round(random.NextDouble() * 2000 + 3000);
                                    cell.ApplyFormatting(rowFormatting);
                                }
                            }
                        }
                    }
                    #region #SetDateRange
                    // Create a group of line sparklines.
                    XlSparklineGroup group = new XlSparklineGroup(XlCellRange.Parse("B2:E7"), XlCellRange.Parse("F2:F7"));
                    // Specify the date range for the sparkline group.
                    group.DateRange = XlCellRange.Parse("B1:E1");
                    // Set the sparkline weight.
                    group.LineWeight = 1.25;
                    // Display data markers on the sparklines.
                    group.DisplayMarkers = true;
                    sheet.SparklineGroups.Add(group);
                    #endregion #SetDateRange
                    // Create another group of line sparklines with defaul general axis type.
                    XlSparklineGroup group1 = new XlSparklineGroup(XlCellRange.Parse("B2:E7"), XlCellRange.Parse("G2:G7"));
                    group1.LineWeight     = 1.25;
                    group1.DisplayMarkers = true;
                    sheet.SparklineGroups.Add(group1);
                }
            }
        }
Ejemplo n.º 6
0
        static void DisplayXAxis(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create worksheet columns and set their widths.
                    using (IXlColumn column = sheet.CreateColumn())
                    {
                        column.WidthInPixels = 200;
                    }
                    for (int i = 0; i < 9; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                        {
                            column.WidthInPixels = 100;
                            column.ApplyFormatting((XlNumberFormat)@"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)");
                        }
                    }

                    // Specify formatting settings for cells containing data.
                    XlCellFormatting rowFormatting = new XlCellFormatting();
                    rowFormatting.Font = XlFont.CustomFont("Century Gothic", 12.0);

                    // Specify formatting settings for the header row.
                    XlCellFormatting headerRowFormatting = rowFormatting.Clone();
                    headerRowFormatting.Font.Bold  = true;
                    headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
                    headerRowFormatting.Fill       = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0));

                    string[] columnNames = new string[] { "State", "Q1'13", "Q2'13", "Q3'13", "Q4'13", "Q1'14", "Q2'14", "Q3'14", "Q4'14" };

                    // Generate the header row.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(columnNames, headerRowFormatting);
                    }

                    // Generate data for the document.
                    Random   random   = new Random();
                    string[] products = new string[] { "Alabama", "Arizona", "California", "Colorado", "Connecticut", "Florida" };

                    foreach (string product in products)
                    {
                        using (IXlRow row = sheet.CreateRow())
                        {
                            using (IXlCell cell = row.CreateCell())
                            {
                                cell.Value = product;
                                cell.ApplyFormatting(rowFormatting);
                            }
                            for (int j = 0; j < 8; j++)
                            {
                                using (IXlCell cell = row.CreateCell())
                                {
                                    cell.Value = Math.Round((random.NextDouble() + 0.5) * 2000 * Math.Sign(random.NextDouble() - 0.4));
                                    cell.ApplyFormatting(rowFormatting);
                                }
                            }
                        }
                    }

                    #region #DisplayXAxis
                    // Create a sparkline group.
                    XlSparklineGroup group = new XlSparklineGroup(XlCellRange.FromLTRB(1, 1, 8, 6), XlCellRange.FromLTRB(9, 1, 9, 6));
                    // Change the sparkline group type to "Column".
                    group.SparklineType = XlSparklineType.Column;
                    // Display the horizontal axis.
                    group.DisplayXAxis = true;
                    // Set the series color.
                    group.ColorSeries = XlColor.FromTheme(XlThemeColor.Accent1, 0.0);
                    // Highlight negative points on each sparkline in the group.
                    group.ColorNegative     = XlColor.FromTheme(XlThemeColor.Accent2, 0.0);
                    group.HighlightNegative = true;
                    sheet.SparklineGroups.Add(group);
                    #endregion #DisplayXAxis
                }
            }
        }
Ejemplo n.º 7
0
        static void AdjustScaling(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create worksheet columns and set their widths.
                    using (IXlColumn column = sheet.CreateColumn())
                    {
                        column.WidthInPixels = 200;
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                        {
                            column.WidthInPixels = 100;
                            column.ApplyFormatting((XlNumberFormat)@"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)");
                        }
                    }

                    // Specify formatting settings for cells containing data.
                    XlCellFormatting rowFormatting = new XlCellFormatting();
                    rowFormatting.Font = XlFont.CustomFont("Century Gothic", 12.0);

                    // Specify formatting settings for the header row.
                    XlCellFormatting headerRowFormatting = new XlCellFormatting();
                    headerRowFormatting.Font       = XlFont.CustomFont("Century Gothic", 12.0);
                    headerRowFormatting.Font.Bold  = true;
                    headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
                    headerRowFormatting.Fill       = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0));

                    string[] columnNames = new string[] { "Product", "Q1", "Q2", "Q3", "Q4" };

                    // Generate the header row.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(columnNames, headerRowFormatting);
                    }

                    // Generate data for the document.
                    Random   random   = new Random();
                    string[] products = new string[] { "HD Video Player", "SuperLED 42", "SuperLED 50", "DesktopLED 19", "DesktopLED 21", "Projector Plus HD" };

                    foreach (string product in products)
                    {
                        using (IXlRow row = sheet.CreateRow())
                        {
                            using (IXlCell cell = row.CreateCell())
                            {
                                cell.Value = product;
                                cell.ApplyFormatting(rowFormatting);
                            }
                            for (int j = 0; j < 4; j++)
                            {
                                using (IXlCell cell = row.CreateCell())
                                {
                                    cell.Value = Math.Round(random.NextDouble() * 2000 + 1500);
                                    cell.ApplyFormatting(rowFormatting);
                                }
                            }
                        }
                    }

                    #region #AdjustScaling
                    // Create a sparkline group.
                    XlSparklineGroup group = new XlSparklineGroup(XlCellRange.FromLTRB(1, 1, 4, 6), XlCellRange.FromLTRB(5, 1, 5, 6));
                    // Set the sparkline color.
                    group.ColorSeries = XlColor.FromTheme(XlThemeColor.Accent1, 0.0);
                    // Change the sparkline group type to "Column".
                    group.SparklineType = XlSparklineType.Column;
                    // Set the custom minimum value for the vertical axis.
                    group.MinScaling = XlSparklineAxisScaling.Custom;
                    group.ManualMin  = 1000.0;
                    // Set the automatic maximum value for all sparklines in the group.
                    group.MaxScaling = XlSparklineAxisScaling.Group;
                    sheet.SparklineGroups.Add(group);
                    #endregion #AdjustScaling
                }
            }
        }
Ejemplo n.º 8
0
        static void AddSparkline(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create worksheet columns and set their widths.
                    using (IXlColumn column = sheet.CreateColumn())
                    {
                        column.WidthInPixels = 200;
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                        {
                            column.WidthInPixels = 100;
                            column.ApplyFormatting((XlNumberFormat)@"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)");
                        }
                    }

                    // Specify formatting settings for cells containing data.
                    XlCellFormatting rowFormatting = new XlCellFormatting();
                    rowFormatting.Font = XlFont.CustomFont("Century Gothic", 12.0);

                    // Specify formatting settings for the header row.
                    XlCellFormatting headerRowFormatting = new XlCellFormatting();
                    headerRowFormatting.Font       = XlFont.CustomFont("Century Gothic", 12.0);
                    headerRowFormatting.Font.Bold  = true;
                    headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
                    headerRowFormatting.Fill       = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0));

                    string[] columnNames = new string[] { "Product", "Q1", "Q2", "Q3", "Q4" };

                    // Generate the header row.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(columnNames, headerRowFormatting);
                    }

                    // Create a group of line sparklines.
                    XlSparklineGroup group = new XlSparklineGroup();
                    // Set the sparkline color.
                    group.ColorSeries = XlColor.FromTheme(XlThemeColor.Accent1, -0.2);
                    // Set the sparkline weight.
                    group.LineWeight = 1.25;
                    sheet.SparklineGroups.Add(group);

                    // Generate data for the document.
                    Random   random   = new Random();
                    string[] products = new string[] { "HD Video Player", "SuperLED 42", "SuperLED 50", "DesktopLED 19", "DesktopLED 21", "Projector Plus HD" };

                    foreach (string product in products)
                    {
                        using (IXlRow row = sheet.CreateRow())
                        {
                            using (IXlCell cell = row.CreateCell())
                            {
                                cell.Value = product;
                                cell.ApplyFormatting(rowFormatting);
                            }
                            for (int j = 0; j < 4; j++)
                            {
                                using (IXlCell cell = row.CreateCell())
                                {
                                    cell.Value = Math.Round(random.NextDouble() * 2000 + 3000);
                                    cell.ApplyFormatting(rowFormatting);
                                }
                            }

                            #region #AddSparkline
                            // Add one more sparkline to the existing group.
                            int rowIndex = row.RowIndex;
                            group.Sparklines.Add(new XlSparkline(XlCellRange.FromLTRB(1, rowIndex, 4, rowIndex), XlCellRange.FromLTRB(5, rowIndex, 5, rowIndex)));
                            #endregion #AddSparkline
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
        static void CalculatedColumn(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create worksheet columns (A:F) and set their widths.
                    int[] widths = new int[] { 165, 100, 100, 100, 100, 110 };
                    for (int i = 0; i < 6; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                            column.WidthInPixels = widths[i];
                    }

                    #region #CalculatedColumn
                    IXlTable table;
                    // Specify an array containing column headings for a table.
                    string[] columnNames = new string[] { "Product", "Q1", "Q2", "Q3", "Q4", "Yearly Total" };

                    // Create the first row in the worksheet from which the table starts.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        // Start generating the table with a header row displayed.
                        table = row.BeginTable(columnNames, true);
                        // Specify the total row label.
                        table.Columns[0].TotalRowLabel = "Total";
                        // Specify the function to calculate the total.
                        table.Columns[5].TotalRowFunction = XlTotalRowFunction.Sum;
                        // Specify the number format for numeric values in the table and the total cell of the "Yearly Total" column.
                        XlNumberFormat accounting = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";
                        table.DataFormatting = accounting;
                        table.Columns[5].TotalRowFormatting = accounting;
                        // Set the formula to calculate annual sales of each product
                        // and display results in the "Yearly Total" column.
                        table.Columns[5].SetFormula(XlFunc.Sum(table.GetRowReference("Q1", "Q4")));
                    }

                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", 17000, 18500, 17500, 18000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", 15500, 14500, 15000, 14000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", 15000, 15750, 16000, 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Ravioli Angelo", 12500, 11000, 13500, 12000 }, null);

                    // Create the total row and finish the table.
                    using (IXlRow row = sheet.CreateRow())
                        row.EndTable(table, true);
                    #endregion #CalculatedColumn
                }
            }
        }
Ejemplo n.º 10
0
        static void CustomFormatting(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create columns "A", "B" and "C" and set their widths.
                    int[] widths = new int[] { 165, 120, 100 };
                    for (int i = 0; i < 3; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                            column.WidthInPixels = widths[i];
                    }

                    #region #CustomFormatting
                    // Create the first row in the worksheet from which the table starts.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        XlNumberFormat accounting = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";

                        // Create objects containing information about table columns (their names and formatting).
                        List <XlTableColumnInfo> columns = new List <XlTableColumnInfo>();
                        columns.Add(new XlTableColumnInfo("Product"));
                        columns.Add(new XlTableColumnInfo("Category"));
                        columns.Add(new XlTableColumnInfo("Amount"));

                        // Specify formatting settings for the last column of the table.
                        columns[2].HeaderRowFormatting             = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent6, -0.3));
                        columns[2].DataFormatting                  = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Dark1, 0.9));
                        columns[2].DataFormatting.NumberFormat     = accounting;
                        columns[2].TotalRowFormatting              = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Dark1, 0.8));
                        columns[2].TotalRowFormatting.NumberFormat = accounting;

                        // Specify formatting settings for the header row of the table.
                        XlCellFormatting headerRowFormatting = new XlCellFormatting();
                        headerRowFormatting.Fill                   = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent6, 0.0));
                        headerRowFormatting.Border                 = new XlBorder();
                        headerRowFormatting.Border.BottomColor     = XlColor.FromArgb(0, 0, 0);
                        headerRowFormatting.Border.BottomLineStyle = XlBorderLineStyle.Dashed;

                        // Start generating the table with a header row displayed.
                        IXlTable table = row.BeginTable(columns, true, headerRowFormatting);
                        // Apply the table style.
                        table.Style.Name = XlBuiltInTableStyleId.Medium16;
                        // Disable banded row formatting for the table.
                        table.Style.ShowRowStripes = false;
                        // Disable the filtering functionality for the table.
                        table.HasAutoFilter = false;

                        // Specify formatting settings for the total row of the table.
                        table.TotalRowFormatting        = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Dark1, 0.9));
                        table.TotalRowFormatting.Border = new XlBorder()
                        {
                            BottomColor     = XlColor.FromTheme(XlThemeColor.Accent6, 0.0),
                            BottomLineStyle = XlBorderLineStyle.Thick,
                            TopColor        = XlColor.FromArgb(0, 0, 0),
                            TopLineStyle    = XlBorderLineStyle.Dashed
                        };

                        // Specify the total row label.
                        table.Columns[0].TotalRowLabel = "Total";
                        // Specify the function to calculate the total.
                        table.Columns[2].TotalRowFunction = XlTotalRowFunction.Sum;
                    }

                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);

                    // Create the total row and finish the table.
                    using (IXlRow row = sheet.CreateRow())
                        row.EndTable(sheet.Tables[0], true);
                    #endregion #CustomFormatting
                }
            }
        }
Ejemplo n.º 11
0
        static void TableStyleOptions(Stream stream, XlDocumentFormat documentFormat)
        {
            // Create an exporter instance.
            IXlExporter exporter = XlExport.CreateExporter(documentFormat);

            // Create a new document.
            using (IXlDocument document = exporter.CreateDocument(stream))
            {
                document.Options.Culture = CultureInfo.CurrentCulture;

                // Create a worksheet.
                using (IXlSheet sheet = document.CreateSheet())
                {
                    // Create columns "A", "B" and "C" and set their widths.
                    int[] widths = new int[] { 165, 120, 100 };
                    for (int i = 0; i < 3; i++)
                    {
                        using (IXlColumn column = sheet.CreateColumn())
                            column.WidthInPixels = widths[i];
                    }

                    #region #TableStyleOptions
                    IXlTable table;
                    // Specify an array containing column headings for tables.
                    string[] columnNames = new string[] { "Product", "Category", "Amount" };

                    // Create the row containing the table title.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Disable banded rows" }, XlCellFormatting.Title);
                    sheet.SkipRows(1);

                    // Start generating the table with a header row displayed.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        table = row.BeginTable(columnNames, true);
                        // Disable banded row formatting for the table.
                        table.Style.ShowRowStripes = false;
                    }
                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    // Create the last table row and finish the table.
                    // The total row is not displayed for the table.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);
                        row.EndTable(table, false);
                    }
                    sheet.SkipRows(1);

                    // Create the row containing the table title.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Enable banded columns" }, XlCellFormatting.Title);
                    sheet.SkipRows(1);

                    // Start generating the table with a header row displayed.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        table = row.BeginTable(columnNames, true);
                        // Apply banded column formatting to the table.
                        table.Style.ShowRowStripes    = false;
                        table.Style.ShowColumnStripes = true;
                    }
                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    // Create the last table row and finish the table.
                    // The total row is not displayed for the table.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);
                        row.EndTable(table, false);
                    }
                    sheet.SkipRows(1);

                    // Create the row containing the table title.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Highlight first column" }, XlCellFormatting.Title);
                    sheet.SkipRows(1);

                    // Start generating the table with a header row displayed.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        table = row.BeginTable(columnNames, true);
                        // Display special formatting for the first column of the table.
                        table.Style.ShowFirstColumn = true;
                    }
                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    // Create the last table row and finish the table.
                    // The total row is not displayed for the table.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);
                        row.EndTable(table, false);
                    }
                    sheet.SkipRows(1);

                    // Create the row containing the table title.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Highlight last column" }, XlCellFormatting.Title);
                    sheet.SkipRows(1);

                    // Start generating the table with a header row displayed.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        table = row.BeginTable(columnNames, true);
                        // Display special formatting for the last column of the table.
                        table.Style.ShowLastColumn = true;
                    }
                    // Generate table rows and populate them with data.
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Camembert Pierrot", "Dairy Products", 17000 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Gnocchi di nonna Alice", "Grains/Cereals", 15500 }, null);
                    using (IXlRow row = sheet.CreateRow())
                        row.BulkCells(new object[] { "Mascarpone Fabioli", "Dairy Products", 15000 }, null);
                    // Create the last table row and finish the table.
                    // The total row is not displayed for the table.
                    using (IXlRow row = sheet.CreateRow())
                    {
                        row.BulkCells(new object[] { "Ravioli Angelo", "Grains/Cereals", 12500 }, null);
                        row.EndTable(table, false);
                    }
                    #endregion #TableStyleOptions
                }
            }
        }