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); } }
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) { 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); } }
static void CreateColumns(Stream stream, XlDocumentFormat documentFormat) { // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat); // Create a new document. using (IXlDocument document = exporter.CreateDocument(stream)) { // Specify the document culture. document.Options.Culture = CultureInfo.CurrentCulture; #region #CreateColumns // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create the column A and set its width to 100 pixels. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; } // Hide the column B in the worksheet. using (IXlColumn column = sheet.CreateColumn()) { column.IsHidden = true; } // Create the column D and set its width to 24.5 characters. using (IXlColumn column = sheet.CreateColumn(3)) { column.WidthInCharacters = 24.5f; } } #endregion #CreateColumns } }
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 } } }
static void SimpleFormula(Stream stream, XlDocumentFormat documentFormat) { #region #SimpleFormula // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat, new XlFormulaParser()); // 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. for (int i = 0; i < 4; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 80; } } // Generate data for the document. string[] header = new string[] { "Description", "QTY", "Price", "Amount" }; string[] product = new string[] { "Camembert", "Gorgonzola", "Mascarpone", "Mozzarella" }; int[] qty = new int[] { 12, 15, 25, 10 }; double[] price = new double[] { 23.25, 15.50, 12.99, 8.95 }; double discount = 0.2; // Create the header row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = header[i]; } } } // Create data rows using string formulas. for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = product[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = qty[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = price[i]; } using (IXlCell cell = row.CreateCell()) { // Set the formula to calculate the amount // applying 20% quantity discount on orders more than 15 items. cell.SetFormula(String.Format("=IF(B{0}>15,C{0}*B{0}*(1-{1}),C{0}*B{0})", i + 2, discount)); } } } } } #endregion #SimpleFormula }
static void Hyperlinks(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; #region #Hyperlinks // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 300; } // Create a hyperlink to a cell in the current workbook. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Local link"; cell.Formatting = XlCellFormatting.Hyperlink; XlHyperlink hyperlink = new XlHyperlink(); hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex)); hyperlink.TargetUri = "#Sheet1!C5"; sheet.Hyperlinks.Add(hyperlink); } } // Create a hyperlink to a cell located in the external workbook. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "External file link"; cell.Formatting = XlCellFormatting.Hyperlink; XlHyperlink hyperlink = new XlHyperlink(); hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex)); hyperlink.TargetUri = "linked.xlsx#Sheet1!C5"; sheet.Hyperlinks.Add(hyperlink); } } // Create a hyperlink to a web page. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "External URI"; cell.Formatting = XlCellFormatting.Hyperlink; XlHyperlink hyperlink = new XlHyperlink(); hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex)); hyperlink.TargetUri = "http://www.devexpress.com"; sheet.Hyperlinks.Add(hyperlink); } } } #endregion #Hyperlinks } }
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); } }
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 } } }
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 } } }
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)); } } }
static void RichTextFormatting(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; #region #RichTextFormatting // Create a new worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create the first column and set its width. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 180; } // Create the first row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A1. using (IXlCell cell = row.CreateCell()) { // Create an XlRichTextString instance. XlRichTextString richText = new XlRichTextString(); // Add three text runs to the collection. richText.Runs.Add(new XlRichTextRun("Formatted ", XlFont.CustomFont("Arial", 14.0, XlColor.FromArgb(0x53, 0xbb, 0xf4)))); richText.Runs.Add(new XlRichTextRun("cell ", XlFont.CustomFont("Century Gothic", 14.0, XlColor.FromArgb(0xf1, 0x77, 0x00)))); richText.Runs.Add(new XlRichTextRun("text", XlFont.CustomFont("Consolas", 14.0, XlColor.FromArgb(0xe3, 0x2c, 0x2e)))); // Add the rich formatted text to the cell. cell.SetRichText(richText); } } } #endregion #RichTextFormatting } }
static void FitPicture(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()) { sheet.SkipColumns(1); // Create the column "B" and set its width. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 300; } sheet.SkipRows(1); // Create the second row and set its height. using (IXlRow row = sheet.CreateRow()) { row.HeightInPixels = 154; } #region #FitPicture // Insert a picture from a file to fit in the cell B2. using (IXlPicture picture = sheet.CreatePicture()) { picture.Image = Image.FromFile(Path.Combine(imagesPath, "image1.jpg")); picture.FitToCell(new XlCellPosition(1, 1), 300, 154, true); } } #endregion #FitPicture } }
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 } } }
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 } } } } }
static void NumberFormat(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; // Specify options for exporting the document to CSV format. CsvDataAwareExporterOptions csvOptions = document.Options as CsvDataAwareExporterOptions; if (csvOptions != null) { csvOptions.WritePreamble = true; } // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create six successive columns and set their widths. for (int i = 0; i < 6; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 180; } } #region #ExcelNumberFormat // Create the header row for the "Excel number formats" category. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Excel number formats"; // Apply the "Heading 4" predefined formatting to the cell. cell.Formatting = XlCellFormatting.Heading4; } } // Use the predefined Excel number formats to display data in cells. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Predefined formats:"; } using (IXlCell cell = row.CreateCell()) { // Display 123.456 as 123.46. cell.Value = 123.456; cell.Formatting = XlNumberFormat.Number2; } using (IXlCell cell = row.CreateCell()) { // Display 12345 as 12,345. cell.Value = 12345; cell.Formatting = XlNumberFormat.NumberWithThousandSeparator; } using (IXlCell cell = row.CreateCell()) { // Display 0.33 as 33%. cell.Value = 0.33; cell.Formatting = XlNumberFormat.Percentage; } using (IXlCell cell = row.CreateCell()) { // Display the current date as "mm-dd-yy". cell.Value = DateTime.Now; cell.Formatting = XlNumberFormat.ShortDate; } using (IXlCell cell = row.CreateCell()) { // Display the current time as "h:mm AM/PM". cell.Value = DateTime.Now; cell.Formatting = XlNumberFormat.ShortTime12; } } // Use custom number formats to display data in cells. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Custom formats:"; } using (IXlCell cell = row.CreateCell()) { // Display 4310.45 as $4,310.45. cell.Value = 4310.45; cell.Formatting = new XlCellFormatting(); cell.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } using (IXlCell cell = row.CreateCell()) { // Display 3426.75 as €3,426.75. cell.Value = 3426.75; cell.Formatting = new XlCellFormatting(); cell.Formatting.NumberFormat = @"_-[$€-2] * #,##0.00_-;-[$€-2] * #,##0.00_-;_-[$€-2] * "" - ""??_-;_-@_-"; } using (IXlCell cell = row.CreateCell()) { // Display 0.333 as 33.3%. cell.Value = 0.333; cell.Formatting = new XlCellFormatting(); cell.Formatting.NumberFormat = "0.0%"; } using (IXlCell cell = row.CreateCell()) { // Apply the custom number format to the date value. // Display days as Sunday–Saturday, months as January–December, days as 1–31 and years as 1900–9999. cell.Value = DateTime.Now; cell.Formatting = new XlCellFormatting(); cell.Formatting.NumberFormat = "dddd, mmmm d, yyyy"; } using (IXlCell cell = row.CreateCell()) { // Display 0.6234 as 341/547. cell.Value = 0.6234; cell.Formatting = new XlCellFormatting(); cell.Formatting.NumberFormat = "# ???/???"; } } #endregion #ExcelNumberFormat sheet.SkipRows(1); #region #NETNumberFormat // Create the header row for the ".NET number formats" category. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = ".NET number formats"; // Apply the "Heading 4" predefined formatting to the cell. cell.Formatting = XlCellFormatting.Heading4; } } // Use the standard .NET-style format strings to display data in cells. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Standard formats:"; } using (IXlCell cell = row.CreateCell()) { // Display 123.45 as 123. cell.Value = 123.45; cell.Formatting = XlCellFormatting.FromNetFormat("D", false); } using (IXlCell cell = row.CreateCell()) { // Display 12345 as 1.234500E+004. cell.Value = 12345; cell.Formatting = XlCellFormatting.FromNetFormat("E", false); } using (IXlCell cell = row.CreateCell()) { // Display 0.33 as 33.00%. cell.Value = 0.33; cell.Formatting = XlCellFormatting.FromNetFormat("P", false); } using (IXlCell cell = row.CreateCell()) { // Display the current date using the short date pattern. cell.Value = DateTime.Now; cell.Formatting = XlCellFormatting.FromNetFormat("d", true); } using (IXlCell cell = row.CreateCell()) { // Display the current time using the short time pattern. cell.Value = DateTime.Now; cell.Formatting = XlCellFormatting.FromNetFormat("t", true); } } // Use custom format strings to display data in cells. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Custom formats:"; } using (IXlCell cell = row.CreateCell()) { // Display 123.456 as 123.46. cell.Value = 123.45; cell.Formatting = XlCellFormatting.FromNetFormat("#0.00", false); } using (IXlCell cell = row.CreateCell()) { // Display 12345 as 1.235E+04. cell.Value = 12345; cell.Formatting = XlCellFormatting.FromNetFormat("0.0##e+00", false); } using (IXlCell cell = row.CreateCell()) { // Display 0.333 as Max=33.3%. cell.Value = 0.333; cell.Formatting = XlCellFormatting.FromNetFormat("Max={0:#.0%}", false); } using (IXlCell cell = row.CreateCell()) { // Apply the custom format string to the current date. // Display days as 01–31, months as 01-12 and years as a four-digit number. cell.Value = DateTime.Now; cell.Formatting = XlCellFormatting.FromNetFormat("dd-MM-yyyy", true); } using (IXlCell cell = row.CreateCell()) { // Apply the custom format string to the current time. // Display hours as 01-12, minutes as 00-59, and add the AM/PM designator. cell.Value = DateTime.Now; cell.Formatting = XlCellFormatting.FromNetFormat("hh:mm tt", true); } } #endregion #NETNumberFormat } } }
static void PredefinedFormatting(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; #region #PredefinedFormatting // Create a new worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create six successive columns and set their widths. for (int i = 0; i < 6; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; } } // Specify the "Good, Bad and Neutral" formatting category. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Good, Bad and Neutral"; } } using (IXlRow row = sheet.CreateRow()) { // Create a cell with the default "Normal" formatting. using (IXlCell cell = row.CreateCell()) { cell.Value = "Normal"; } // Create a cell and apply the "Bad" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Bad"; cell.Formatting = XlCellFormatting.Bad; } // Create a cell and apply the "Good" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Good"; cell.Formatting = XlCellFormatting.Good; } // Create a cell and apply the "Neutral" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Neutral"; cell.Formatting = XlCellFormatting.Neutral; } } sheet.SkipRows(1); // Specify the "Data and Model" formatting category. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Data and Model"; } } using (IXlRow row = sheet.CreateRow()) { // Create a cell and apply the "Calculation" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Calculation"; cell.Formatting = XlCellFormatting.Calculation; } // Create a cell and apply the "Check Cell" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Check Cell"; cell.Formatting = XlCellFormatting.CheckCell; } // Create a cell and apply the "Explanatory..." predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Explanatory"; cell.Formatting = XlCellFormatting.Explanatory; } // Create a cell and apply the "Input" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Input"; cell.Formatting = XlCellFormatting.Input; } // Create a cell and apply the "Linked Cell" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Linked Cell"; cell.Formatting = XlCellFormatting.LinkedCell; } // Create a cell and apply the "Note" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Note"; cell.Formatting = XlCellFormatting.Note; } } using (IXlRow row = sheet.CreateRow()) { // Create a cell and apply the "Output" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Output"; cell.Formatting = XlCellFormatting.Output; } // Create a cell and apply the "Warning Text" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Warning Text"; cell.Formatting = XlCellFormatting.WarningText; } } sheet.SkipRows(1); // Specify the "Titles and Headings" formatting category. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Titles and Headings"; } } using (IXlRow row = sheet.CreateRow()) { // Create a cell and apply the "Heading 1" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Heading 1"; cell.Formatting = XlCellFormatting.Heading1; } // Create a cell and apply the "Heading 2" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Heading 2"; cell.Formatting = XlCellFormatting.Heading2; } // Create a cell and apply the "Heading 3" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Heading 3"; cell.Formatting = XlCellFormatting.Heading3; } // Create a cell and apply the "Heading 4" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Heading 4"; cell.Formatting = XlCellFormatting.Heading4; } // Create a cell and apply the "Title" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Title"; cell.Formatting = XlCellFormatting.Title; } // Create a cell and apply the "Total" predefined formatting to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "Total"; cell.Formatting = XlCellFormatting.Total; } } } #endregion #PredefinedFormatting } }
static void Font(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; #region #Font // Create a new worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create five successive columns and set their widths. for (int i = 0; i < 5; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; } } // Create the first row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A1. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Body font"; // Apply the theme body font to the cell content. cell.ApplyFormatting(XlFont.BodyFont()); } // Create the cell B1. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Headings font"; // Apply the theme heading font to the cell content. cell.ApplyFormatting(XlFont.HeadingsFont()); } // Create the cell C1. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Custom font"; // Specify the custom font attributes. XlFont font = new XlFont(); font.Name = "Century Gothic"; font.SchemeStyle = XlFontSchemeStyles.None; // Apply the custom font to the cell content. cell.ApplyFormatting(font); } } // Create an array that stores different values of font size. int[] fontSizes = new int[] { 11, 14, 18, 24, 36 }; // Skip one row in the worksheet. sheet.SkipRows(1); // Create the third row. using (IXlRow row = sheet.CreateRow()) { // Create five successive cells (A3:E3) with different font sizes. for (int i = 0; i < 5; i++) { using (IXlCell cell = row.CreateCell()) { // Set the cell value that displays the applied font size. cell.Value = string.Format("{0}pt", fontSizes[i]); // Create a font instance of the specified size. XlFont font = new XlFont(); font.Size = fontSizes[i]; // Apply font settings to the cell content. cell.ApplyFormatting(font); } } } // Skip one row in the worksheet. sheet.SkipRows(1); // Create the fifth row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A5. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Red"; // Create a font instance and set its color. XlFont font = new XlFont() { Color = Color.Red }; // Apply the font color to the cell content. cell.ApplyFormatting(font); } // Create the cell B5. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Bold"; // Create a font instance and set its style to bold. XlFont font = new XlFont() { Bold = true }; // Apply the font style to the cell content. cell.ApplyFormatting(font); } // Create the cell C5. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Italic"; // Create a font instance and set its style to italic. XlFont font = new XlFont() { Italic = true }; // Italicize the cell text. cell.ApplyFormatting(font); } // Create the cell D5. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Underline"; // Create a font instance and set the underline type to double. XlFont font = new XlFont() { Underline = XlUnderlineType.Double }; // Underline the cell text. cell.ApplyFormatting(font); } // Create the cell E5. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "StrikeThrough"; // Create a font instance and turn the strikethrough formatting on. XlFont font = new XlFont() { StrikeThrough = true }; // Strike the cell text through. cell.ApplyFormatting(font); } } } #endregion #Font } }
static void Alignment(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; #region #Alignment // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create three successive columns and set their widths. for (int i = 0; i < 3; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 130; } } // Create the first row in the worksheet. using (IXlRow row = sheet.CreateRow()) { // Set the row height. row.HeightInPixels = 40; // Create the first cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Left and Top"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Top)); } // Create the second cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Center and Top"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Top)); } // Create the third cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Right and Top"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Top)); } } // Create the second row in the worksheet. using (IXlRow row = sheet.CreateRow()) { // Set the row height. row.HeightInPixels = 40; // Create the first cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Left and Center"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Center)); } // Create the second cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Center and Center"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center)); } // Create the third cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Right and Center"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Center)); } } // Create the third row in the worksheet. using (IXlRow row = sheet.CreateRow()) { // Set the row height. row.HeightInPixels = 40; // Create the first cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Left and Bottom"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Bottom)); } // Create the second cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Center and Bottom"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Bottom)); } // Create the third cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Right and Bottom"; // Specify the horizontal and vertical alignment of the cell content. cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Bottom)); } } sheet.SkipRows(1); // Create the fifth row in the worksheet. using (IXlRow row = sheet.CreateRow()) { // Create the first cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "The WrapText property is applied to wrap the text within a cell"; // Wrap the text within the cell. cell.Formatting = new XlCellAlignment() { WrapText = true }; } // Create the second cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Indented text"; // Set the indentation of the cell content. cell.Formatting = new XlCellAlignment() { Indent = 2 }; } // Create the third cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = "Rotated text"; // Rotate the text within the cell. cell.Formatting = new XlCellAlignment() { TextRotation = 90 }; } } } #endregion #Alignment } }
static void ThemedFormatting(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; #region #ThemedFormatting // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create six successive columns and set their widths. for (int i = 0; i < 6; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; } } // Specify an array that stores six accent colors of the document theme. XlThemeColor[] themeColors = new XlThemeColor[] { XlThemeColor.Accent1, XlThemeColor.Accent2, XlThemeColor.Accent3, XlThemeColor.Accent4, XlThemeColor.Accent5, XlThemeColor.Accent6 }; // Specify the "20% - AccentN" themed cell formatting. // Create a worksheet row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 6; i++) { // Create a new cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = string.Format("Accent{0} 20%", i + 1); // Apply the themed formatting to the cell using one of the predefined accent colors lightened by 80%. cell.Formatting = XlCellFormatting.Themed(themeColors[i], 0.8); } } } // Specify the "40% - AccentN" themed cell formatting. // Create a worksheet row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 6; i++) { // Create a new cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = string.Format("Accent{0} 40%", i + 1); // Apply the themed formatting to the cell using one of the predefined accent colors lightened by 60%. cell.Formatting = XlCellFormatting.Themed(themeColors[i], 0.6); } } } // Specify the "60% - AccentN" themed cell formatting. // Create a worksheet row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 6; i++) { // Create a new cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = string.Format("Accent{0} 60%", i + 1); // Apply the themed formatting to the cell using one of the predefined accent colors lightened by 40%. cell.Formatting = XlCellFormatting.Themed(themeColors[i], 0.4); } } } // Specify the "AccentN" themed cell formatting. // Create a worksheet row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 6; i++) { // Create a new cell in the row. using (IXlCell cell = row.CreateCell()) { // Set the cell value. cell.Value = string.Format("Accent{0}", i + 1); // Apply the themed formatting to the cell using one of the predefined accent colors. cell.Formatting = XlCellFormatting.Themed(themeColors[i], 0.0); } } } } #endregion #ThemedFormatting } }
static void ComplexFormulas(Stream stream, XlDocumentFormat documentFormat) { // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat, new XlFormulaParser()); // 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; } using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 50; } for (int i = 0; i < 2; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 80; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.Font = XlFont.BodyFont(); headerRowFormatting.Font.Bold = true; headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0); headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0)); // Specify formatting settings for the total row. XlCellFormatting totalRowFormatting = new XlCellFormatting(); totalRowFormatting.Font = XlFont.BodyFont(); totalRowFormatting.Font.Bold = true; // Generate data for the document. string[] header = new string[] { "Description", "QTY", "Price", "Amount" }; string[] product = new string[] { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni" }; int[] qty = new int[] { 12, 15, 25, 10 }; double[] price = new double[] { 23.25, 15.50, 12.99, 8.95 }; // Create the header row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = header[i]; cell.ApplyFormatting(headerRowFormatting); } } } #region #Formula_String // Create data rows using string formulas. for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = product[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = qty[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = price[i]; } using (IXlCell cell = row.CreateCell()) { // Set the formula to calculate the amount per product. cell.SetFormula(String.Format("B{0}*C{0}", i + 2)); } } } #endregion #Formula_String #region #Formula_IXlFormulaParameter // Create the total row using IXlFormulaParameter. using (IXlRow row = sheet.CreateRow()) { row.SkipCells(2); using (IXlCell cell = row.CreateCell()) { cell.Value = "Total:"; cell.ApplyFormatting(totalRowFormatting); } using (IXlCell cell = row.CreateCell()) { // Set the formula to calculate the total amount plus 10 handling fee. // =SUM($D$2:$D$5)+10 IXlFormulaParameter const10 = XlFunc.Param(10); IXlFormulaParameter sumAmountFunction = XlFunc.Sum(XlCellRange.FromLTRB(cell.ColumnIndex, 1, cell.ColumnIndex, row.RowIndex - 1).AsAbsolute()); cell.SetFormula(XlOper.Add(sumAmountFunction, const10)); cell.ApplyFormatting(totalRowFormatting); } } #endregion #Formula_IXlFormulaParameter #region #Formula_XlExpression // Create a formula using XlExpression. using (IXlRow row = sheet.CreateRow()) { row.SkipCells(2); using (IXlCell cell = row.CreateCell()) { cell.Value = "Mean value:"; cell.ApplyFormatting(totalRowFormatting); } using (IXlCell cell = row.CreateCell()) { // Set the formula to calculate the mean value. // =$D$6/4 XlExpression expression = new XlExpression(); expression.Add(new XlPtgRef(new XlCellPosition(cell.ColumnIndex, row.RowIndex - 1, XlPositionType.Absolute, XlPositionType.Absolute))); expression.Add(new XlPtgInt(row.RowIndex - 2)); expression.Add(new XlPtgBinaryOperator(XlPtgTypeCode.Div)); cell.SetFormula(expression); cell.ApplyFormatting(totalRowFormatting); } } #endregion #Formula_XlExpression } } }
static void DataBar(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. for (int i = 0; i < 3; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; } } for (int i = 0; i < 11; i++) { using (IXlRow row = sheet.CreateRow()) { for (int j = 0; j < 3; j++) { using (IXlCell cell = row.CreateCell()) { int rowIndex = cell.RowIndex; int columnIndex = cell.ColumnIndex; if (columnIndex == 0) { cell.Value = rowIndex + 1; } else if (columnIndex == 1) { cell.Value = rowIndex - 5; } else { cell.Value = (rowIndex < 5) ? rowIndex + 1 : 11 - rowIndex; } } } } } #region #DataBarRule // Create an instance of the XlConditionalFormatting class. XlConditionalFormatting formatting = new XlConditionalFormatting(); // Specify the cell range to which the conditional formatting rule should be applied (A1:A11). formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10)); // Create the rule to compare values in the cell range using data bars. XlCondFmtRuleDataBar rule = new XlCondFmtRuleDataBar(); // Specify the bar color. rule.FillColor = XlColor.FromTheme(XlThemeColor.Accent1, 0.2); // Specify the solid fill type. rule.GradientFill = false; formatting.Rules.Add(rule); // Add the specified format options to the worksheet collection of conditional formats. sheet.ConditionalFormattings.Add(formatting); // Create an instance of the XlConditionalFormatting class. formatting = new XlConditionalFormatting(); // Specify the cell range to which the conditional formatting rule should be applied (B1:B11). formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10)); // Create the rule to compare values in the cell range using data bars. rule = new XlCondFmtRuleDataBar(); // Set the positive bar color to green. rule.FillColor = Color.Green; // Set the border color of positive bars to green. rule.BorderColor = Color.Green; // Set the axis color to brown. rule.AxisColor = Color.Brown; // Use the gradient fill type rule.GradientFill = true; formatting.Rules.Add(rule); // Add the specified format options to the worksheet collection of conditional formats. sheet.ConditionalFormattings.Add(formatting); // Create an instance of the XlConditionalFormatting class. formatting = new XlConditionalFormatting(); // Specify the cell range to which the conditional formatting rule should be applied (C1:C11). formatting.Ranges.Add(XlCellRange.FromLTRB(2, 0, 2, 10)); // Create the rule to compare values in the cell range using data bars. rule = new XlCondFmtRuleDataBar(); // Specify the bar color. rule.FillColor = XlColor.FromTheme(XlThemeColor.Accent4, 0.2); // Set the minimum length of the data bar. rule.MinLength = 10; // Set the maximum length of the data bar. rule.MaxLength = 90; // Set the value corresponding to the shortest bar. rule.MinValue.ObjectType = XlCondFmtValueObjectType.Number; rule.MinValue.Value = 3; // Set the direction of data bars. rule.Direction = XlDataBarDirection.RightToLeft; // Hide values of cells to which the rule is applied. rule.ShowValues = false; formatting.Rules.Add(rule); // Add the specified format options to the worksheet collection of conditional formats. sheet.ConditionalFormattings.Add(formatting); #endregion #DataBarRule } } }
static void HeadersFooters(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()) { #region #HeaderAndFooters // Specify different headers and footers for the odd-numbered and even-numbered pages. sheet.HeaderFooter.DifferentOddEven = true; // Add the bold text to the header left section, // and insert the workbook name into the header right section. sheet.HeaderFooter.OddHeader = XlHeaderFooter.FromLCR(XlHeaderFooter.Bold + "Sample report", null, XlHeaderFooter.BookName); // Insert the current page number into the footer right section. sheet.HeaderFooter.OddFooter = XlHeaderFooter.FromLCR(null, null, XlHeaderFooter.PageNumber); // Insert the workbook file path into the header left section, // and add the worksheet name to the header right section. sheet.HeaderFooter.EvenHeader = XlHeaderFooter.FromLCR(XlHeaderFooter.BookPath, null, XlHeaderFooter.SheetName); // Insert the current page number into the footer left section // and add the current date to the footer right section. sheet.HeaderFooter.EvenFooter = XlHeaderFooter.FromLCR(XlHeaderFooter.PageNumber, null, XlHeaderFooter.Date); #endregion #HeaderAndFooters // Generate data for the document. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 250; } for (int i = 0; i < 4; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } // Specify formatting settings for cells containing data. XlCellFormatting rowFormatting = new XlCellFormatting(); rowFormatting.Font = new XlFont(); rowFormatting.Font.Name = "Century Gothic"; rowFormatting.Font.SchemeStyle = XlFontSchemeStyles.None; // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.CopyFrom(rowFormatting); headerRowFormatting.Font.Bold = true; // Generate the header row. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Product"; cell.ApplyFormatting(headerRowFormatting); } for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = string.Format("Q{0}", i + 1); cell.ApplyFormatting(headerRowFormatting); } } } // Generate data rows. string[] products = new string[] { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni", "Gnocchi di nonna Alice", "Gudbrandsdalsost", "Gustaf's Knäckebröd", "Queso Cabrales", "Queso Manchego La Pastora", "Raclette Courdavault", "Singaporean Hokkien Fried Mee", "Wimmers gute Semmelknödel" }; Random random = new Random(); for (int i = 0; i < 12; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = products[i]; 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); } } } } } } }
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 } } }
static void PrintTitles(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()) { #region #PrintTitles // Print the first column and the first row on every page. sheet.PrintTitles.SetColumns(0, 0); sheet.PrintTitles.SetRows(0, 0); #endregion #PrintTitles // Generate data for the document. // Create worksheet columns and set their widths. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 250; } for (int i = 0; i < 4; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } // Specify formatting settings for cells containing data. XlCellFormatting rowFormatting = new XlCellFormatting(); rowFormatting.Font = new XlFont(); rowFormatting.Font.Name = "Century Gothic"; rowFormatting.Font.SchemeStyle = XlFontSchemeStyles.None; // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.CopyFrom(rowFormatting); headerRowFormatting.Font.Bold = true; // Generate the header row. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Product"; cell.ApplyFormatting(headerRowFormatting); } for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = string.Format("Q{0}", i + 1); cell.ApplyFormatting(headerRowFormatting); } } } // Generate data rows. string[] products = new string[] { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni", "Gnocchi di nonna Alice", "Gudbrandsdalsost", "Gustaf's Knäckebröd", "Queso Cabrales", "Queso Manchego La Pastora", "Raclette Courdavault", "Singaporean Hokkien Fried Mee", "Wimmers gute Semmelknödel" }; Random random = new Random(); for (int i = 0; i < 12; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = products[i]; 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); } } } } } } }
static void PrintArea(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()) { #region #PrintArea // Set the print area to cells A1:E5. sheet.PrintArea = XlCellRange.FromLTRB(0, 0, 4, 4); #endregion #PrintArea // Create worksheet columns and set their widths. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 110; column.Formatting = XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Bottom); } using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 190; } for (int i = 0; i < 2; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 90; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 130; } sheet.SkipColumns(1); using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 130; } // Specify formatting settings for cells containing data. XlCellFormatting rowFormatting = new XlCellFormatting(); rowFormatting.Font = new XlFont(); rowFormatting.Font.Name = "Century Gothic"; rowFormatting.Font.SchemeStyle = XlFontSchemeStyles.None; // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.CopyFrom(rowFormatting); headerRowFormatting.Font.Bold = true; headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0); headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0)); // Generate the header row. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Employee ID"; cell.ApplyFormatting(headerRowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = "Employee name"; cell.ApplyFormatting(headerRowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = "Salary"; cell.ApplyFormatting(headerRowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = "Bonus"; cell.ApplyFormatting(headerRowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = "Department"; cell.ApplyFormatting(headerRowFormatting); } row.SkipCells(1); using (IXlCell cell = row.CreateCell()) { cell.Value = "Departments"; cell.ApplyFormatting(headerRowFormatting); } } // Generate data for the document. int[] id = new int[] { 10115, 10709, 10401, 10204 }; string[] name = new string[] { "Augusta Delono", "Chris Cadwell", "Frank Diamond", "Simon Newman" }; int[] salary = new int[] { 1100, 2000, 1750, 1250 }; int[] bonus = new int[] { 50, 180, 100, 80 }; int[] deptid = new int[] { 0, 2, 3, 3 }; string[] department = new string[] { "Accounting", "IT", "Management", "Manufacturing" }; for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = id[i]; cell.ApplyFormatting(rowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = name[i]; cell.ApplyFormatting(rowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = salary[i]; cell.ApplyFormatting(rowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = bonus[i]; cell.ApplyFormatting(rowFormatting); } using (IXlCell cell = row.CreateCell()) { cell.Value = department[deptid[i]]; cell.ApplyFormatting(rowFormatting); } row.SkipCells(1); using (IXlCell cell = row.CreateCell()) { cell.Value = department[i]; cell.ApplyFormatting(rowFormatting); } } } // Restrict data entry in the cell range E2:E5 to values in a drop-down list obtained from the cells G2:G5. XlDataValidation validation = new XlDataValidation(); validation.Ranges.Add(XlCellRange.FromLTRB(4, 1, 4, 4)); validation.Type = XlDataValidationType.List; validation.Criteria1 = XlCellRange.FromLTRB(6, 1, 6, 4).AsAbsolute(); sheet.DataValidations.Add(validation); } } }
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); } } }
static void SharedFormulas(Stream stream, XlDocumentFormat documentFormat) { // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat, new XlFormulaParser()); // 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; } using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 50; } for (int i = 0; i < 2; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 80; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.Font = XlFont.BodyFont(); headerRowFormatting.Font.Bold = true; headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0); headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0)); // Specify formatting settings for the total row. XlCellFormatting totalRowFormatting = new XlCellFormatting(); totalRowFormatting.Font = XlFont.BodyFont(); totalRowFormatting.Font.Bold = true; // Generate data for the document. string[] header = new string[] { "Description", "QTY", "Price", "Amount" }; string[] product = new string[] { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni" }; int[] qty = new int[] { 12, 15, 25, 10 }; double[] price = new double[] { 23.25, 15.50, 12.99, 8.95 }; // Create the header row. using (IXlRow row = sheet.CreateRow()) { for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = header[i]; cell.ApplyFormatting(headerRowFormatting); } } } #region #SharedFormulas // Create data rows. for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = product[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = qty[i]; } using (IXlCell cell = row.CreateCell()) { cell.Value = price[i]; } using (IXlCell cell = row.CreateCell()) { // Use the shared formula to calculate the amount per product. if (i == 0) { cell.SetSharedFormula("B2*C2", XlCellRange.FromLTRB(3, 1, 3, 4)); } else { cell.SetSharedFormula(new XlCellPosition(3, 1)); } } } } #endregion #SharedFormulas // Create the total row. using (IXlRow row = sheet.CreateRow()) { row.SkipCells(2); using (IXlCell cell = row.CreateCell()) { cell.Value = "Total:"; cell.ApplyFormatting(totalRowFormatting); } using (IXlCell cell = row.CreateCell()) { // Set the formula to calculate the total amount. cell.SetFormula("SUM(D2:D5)"); cell.ApplyFormatting(totalRowFormatting); } } } } }
static void CreateCells(Stream stream, XlDocumentFormat documentFormat) { // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat); // Create a new document. using (IXlDocument document = exporter.CreateDocument(stream)) { // Specify the document culture. document.Options.Culture = CultureInfo.CurrentCulture; #region #CreateCells // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { // Create the column A and set its width. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 150; } // Create the first row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A1 and set its value. using (IXlCell cell = row.CreateCell()) { cell.Value = "Numeric value:"; } // Create the cell B1 and assign the numeric value to it. using (IXlCell cell = row.CreateCell()) { cell.Value = 123.45; } } // Create the second row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A2 and set its value. using (IXlCell cell = row.CreateCell()) { cell.Value = "Text value:"; } // Create the cell B2 and assign the text value to it. using (IXlCell cell = row.CreateCell()) { cell.Value = "abc"; } } // Create the third row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A3 and set its value. using (IXlCell cell = row.CreateCell()) { cell.Value = "Boolean value:"; } // Create the cell B3 and assign the boolean value to it. using (IXlCell cell = row.CreateCell()) { cell.Value = true; } } // Create the fourth row. using (IXlRow row = sheet.CreateRow()) { // Create the cell A4 and set its value. using (IXlCell cell = row.CreateCell()) { cell.Value = "Error value:"; } // Create the cell B4 and assign an error value to it. using (IXlCell cell = row.CreateCell()) { cell.Value = XlVariantValue.ErrorName; } } } #endregion #CreateCells } }
static void Subtotals(Stream stream, XlDocumentFormat documentFormat) { // Declare a variable that indicates the start of the data rows to calculate grand totals. int startDataRowForGrandTotal; // 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 the column "A" and set its width. using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 200; } // Create five successive columns and set the specific number format for their cells. for (int i = 0; i < 5; i++) { using (IXlColumn column = sheet.CreateColumn()) { column.WidthInPixels = 100; column.Formatting = new XlCellFormatting(); column.Formatting.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"; } } // Specify formatting settings for cells containing data. XlCellFormatting rowFormatting = new XlCellFormatting(); rowFormatting.Font = XlFont.BodyFont(); rowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light1, 0.0)); // Specify formatting settings for the header row. XlCellFormatting headerRowFormatting = new XlCellFormatting(); headerRowFormatting.Font = XlFont.BodyFont(); headerRowFormatting.Font.Bold = true; headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0); headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0)); // Specify formatting settings for the total row. XlCellFormatting totalRowFormatting = new XlCellFormatting(); totalRowFormatting.Font = XlFont.BodyFont(); totalRowFormatting.Font.Bold = true; totalRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, 0.0)); // Generate data for the document. Random random = new Random(); string[] productsDairy = new string[] { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni" }; string[] productsCereals = new string[] { "Gnocchi di nonna Alice", "Gustaf's Knäckebröd", "Ravioli Angelo", "Singaporean Hokkien Fried Mee" }; // Create the header row. using (IXlRow row = sheet.CreateRow()) { startDataRowForGrandTotal = row.RowIndex + 1; using (IXlCell cell = row.CreateCell()) { cell.Value = "Product"; cell.ApplyFormatting(headerRowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0)); } for (int i = 0; i < 4; i++) { using (IXlCell cell = row.CreateCell()) { cell.Value = string.Format("Q{0}", i + 1); cell.ApplyFormatting(headerRowFormatting); cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Bottom)); } } using (IXlCell cell = row.CreateCell()) { cell.Value = "Yearly total"; cell.ApplyFormatting(headerRowFormatting); cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Bottom)); } } // Create data rows for Dairy products. for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = productsDairy[i]; cell.ApplyFormatting(rowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.8)); } for (int j = 0; j < 4; j++) { using (IXlCell cell = row.CreateCell()) { cell.Value = Math.Round(random.NextDouble() * 2000 + 3000); cell.ApplyFormatting(rowFormatting); } } using (IXlCell cell = row.CreateCell()) { // Use the SUM function to calculate annual sales for each product. cell.SetFormula(XlFunc.Sum(XlCellRange.FromLTRB(1, row.RowIndex, 4, row.RowIndex))); cell.ApplyFormatting(rowFormatting); cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, 0.0))); } } } // Create the total row for Dairies. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Subtotal"; cell.ApplyFormatting(totalRowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.6)); } for (int j = 0; j < 5; j++) { using (IXlCell cell = row.CreateCell()) { // Use the SUBTOTAL function to calculate total sales for each quarter and the entire year. cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(j + 1, row.RowIndex - 4, j + 1, row.RowIndex - 1), XlSummary.Sum, false)); cell.ApplyFormatting(totalRowFormatting); } } } // Create data rows for Cereals. for (int i = 0; i < 4; i++) { using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = productsCereals[i]; cell.ApplyFormatting(rowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.8)); } for (int j = 0; j < 4; j++) { using (IXlCell cell = row.CreateCell()) { cell.Value = Math.Round(random.NextDouble() * 2000 + 3000); cell.ApplyFormatting(rowFormatting); } } using (IXlCell cell = row.CreateCell()) { // Use the SUM function to calculate annual sales for each product. cell.SetFormula(XlFunc.Sum(XlCellRange.FromLTRB(1, row.RowIndex, 4, row.RowIndex))); cell.ApplyFormatting(rowFormatting); cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, 0.0))); } } } // Create the total row for Cereals. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Subtotal"; cell.ApplyFormatting(totalRowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.6)); } for (int j = 0; j < 5; j++) { using (IXlCell cell = row.CreateCell()) { // Use the SUBTOTAL function to calculate total sales for each quarter and the entire year. cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(j + 1, row.RowIndex - 4, j + 1, row.RowIndex - 1), XlSummary.Sum, false)); cell.ApplyFormatting(totalRowFormatting); } } } #region #SubtotalFunction // Create the grand total row. using (IXlRow row = sheet.CreateRow()) { using (IXlCell cell = row.CreateCell()) { cell.Value = "Grand Total"; cell.ApplyFormatting(totalRowFormatting); cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.6)); } for (int j = 0; j < 5; j++) { using (IXlCell cell = row.CreateCell()) { // Use the SUBTOTAL function to calculate grand total sales for each quarter and the entire year. cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(j + 1, startDataRowForGrandTotal, j + 1, row.RowIndex - 1), XlSummary.Sum, false)); cell.ApplyFormatting(totalRowFormatting); } } } #endregion #SubtotalFunction } } }