void GenerateColumns(IXlSheet sheet)
        {
            XlNumberFormat numberFormat = @"#,##0,,""M""";

            // Create the "State" column and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 140;

            // Create the "Sales" column, adjust its width and set the specific number format for its cells.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 140;
                column.ApplyFormatting(numberFormat);
            }

            // Create the "Sales vs Target" column, adjust its width and format its cells as percentage values.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 120;
                column.ApplyFormatting(XlNumberFormat.Percentage2);
            }

            // Create the "Profit" column, adjust its width and set the specific number format for its cells.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 140;
                column.ApplyFormatting(numberFormat);
            }

            // Create the "Market Share" column, adjust its width and format its cells as percentage values.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 120;
                column.ApplyFormatting(XlNumberFormat.Percentage);
            }
        }
Beispiel #2
0
        void GenerateColumns(IXlSheet sheet)
        {
            // Create the "Employee ID" column and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 110;

            // Create the "Employee Name" column and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 200;

            XlNumberFormat numberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";

            // Create the "Salary" and "Bonus" columns and set the specific number format for their cells.
            for (int i = 0; i < 2; i++)
            {
                using (IXlColumn column = sheet.CreateColumn()) {
                    column.WidthInPixels = 100;
                    column.ApplyFormatting(numberFormat);
                }
            }

            // Create the "Department" column and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 140;
        }
        void GenerateColumns(IXlSheet sheet)
        {
            // Create the column "A" and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 18;

            // Create the column "B" and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 166;

            XlNumberFormat numberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";

            // Begin to group worksheet columns starting from the column "C" to the column "F".
            sheet.BeginGroup(false);

            // Create four successive columns ("C", "D", "E" and "F") and set the specific number format for their cells.
            for (int i = 0; i < 4; i++)
            {
                using (IXlColumn column = sheet.CreateColumn()) {
                    column.WidthInPixels = 117;
                    column.ApplyFormatting(numberFormat);
                }
            }

            // Finalize the group creation.
            sheet.EndGroup();

            // Create the summary column "G", adjust its width and set the specific number format for its cells.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 117;
                column.ApplyFormatting(numberFormat);
            }
        }
Beispiel #4
0
        static void TimePeriod(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()) {
                    // Generate data for the document.
                    using (IXlColumn column = sheet.CreateColumn()) {
                        column.WidthInPixels = 100;
                        column.ApplyFormatting(XlNumberFormat.ShortDate);
                    }
                    for (int i = 0; i < 10; i++)
                    {
                        using (IXlRow row = sheet.CreateRow()) {
                            using (IXlCell cell = row.CreateCell()) {
                                cell.Value = DateTime.Now.AddDays(row.RowIndex - 5);
                            }
                        }
                    }

                    #region #TimePeriodRule
                    // Create an instance of the XlConditionalFormatting class.
                    XlConditionalFormatting formatting = new XlConditionalFormatting();
                    // Specify the cell range to which the conditional formatting rules should be applied (A1:A10).
                    formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 9));
                    // Create the rule to highlight yesterday's dates in the cell range.
                    XlCondFmtRuleTimePeriod rule = new XlCondFmtRuleTimePeriod();
                    rule.TimePeriod = XlCondFmtTimePeriod.Yesterday;
                    // Specify formatting settings to be applied to cells if the condition is true.
                    rule.Formatting = XlCellFormatting.Bad;
                    formatting.Rules.Add(rule);
                    // Create the rule to highlight today's dates in the cell range.
                    rule            = new XlCondFmtRuleTimePeriod();
                    rule.TimePeriod = XlCondFmtTimePeriod.Today;
                    // Specify formatting settings to be applied to cells if the condition is true.
                    rule.Formatting = XlCellFormatting.Good;
                    formatting.Rules.Add(rule);
                    // Create the rule to highlight tomorrows's dates in the cell range.
                    rule            = new XlCondFmtRuleTimePeriod();
                    rule.TimePeriod = XlCondFmtTimePeriod.Tomorrow;
                    // Specify formatting settings to be applied to cells if the condition is true.
                    rule.Formatting = XlCellFormatting.Neutral;
                    formatting.Rules.Add(rule);
                    // Add the specified format options to the worksheet collection of conditional formats.
                    sheet.ConditionalFormattings.Add(formatting);
                    #endregion #TimePeriodRule
                }
            }
        }
Beispiel #5
0
        void ExportColumn(IXlSheet sheet, GridColumn gridColumn)
        {
            // Skip hidden column
            if (!gridColumn.Visible)
            {
                return;
            }

            using (IXlColumn column = sheet.CreateColumn()) {
                // Setup number format
                if (gridColumn.DisplayFormat.FormatType == FormatType.DateTime)
                {
                    column.ApplyFormatting(XlCellFormatting.FromNetFormat(gridColumn.DisplayFormat.FormatString, true));
                }
                else if (gridColumn.DisplayFormat.FormatType != FormatType.None)
                {
                    column.ApplyFormatting(XlCellFormatting.FromNetFormat(gridColumn.DisplayFormat.FormatString, false));
                }
            }
        }
Beispiel #6
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);
                }
            }
        }
Beispiel #7
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
                }
            }
        }
Beispiel #8
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
                }
            }
        }
Beispiel #9
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
                        }
                    }
                }
            }
        }
        void GenerateColumns(IXlSheet sheet)
        {
            XlNumberFormat currencyFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";
            XlNumberFormat discountFormat = @"0.00%;[Red]-0.00%;;@";

            // Create the column "A" and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 21;
            // Create the column "B" and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 21;

            // Create the column "C" containing the "Description" label in the header row and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 120;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Center));
            }

            // Create the column "D" and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 263;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Center));
            }

            // Create the column "E" containing the "QTY" label in the header row and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 102;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
            }

            // Create the column "F" containing the "Unit Price" label in the header row and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 150;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Center));
                // Apply the currency number format to the column.
                column.ApplyFormatting(currencyFormat);
            }

            // Create the column "G" containing the "Discount" label in the header row and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 134;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
                // Apply the custom number format to the column.
                column.ApplyFormatting(discountFormat);
            }

            // Create the column "H" containing the "Amount" label in the header row and adjust its width.
            using (IXlColumn column = sheet.CreateColumn()) {
                column.WidthInPixels = 174;
                // Set the horizontal and vertical alignment of cell content.
                column.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Center));
                // Apply the currency number format to the column.
                column.ApplyFormatting(currencyFormat);
            }

            // Create the column "I" and set its width.
            using (IXlColumn column = sheet.CreateColumn())
                column.WidthInPixels = 21;
        }