### Complete Minimal Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/README.md A C# code example demonstrating the creation, manipulation, and saving of an Excel workbook using Spire.XLS. ```APIDOC ## Complete Minimal Example This example shows how to create a simple Excel file with data, formatting, and a table. ```csharp using Spire.Xls; using System.Drawing; // Create workbook Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Add data sheet.Range["A1"].Text = "Name"; sheet.Range["B1"].Text = "Sales"; sheet.Range["A2"].Text = "Product A"; sheet.Range["B2"].Value = 1000; // Format header sheet.Range["A1:B1"].Style.Font.IsBold = true; sheet.Range["A1:B1"].Style.Color = Color.LightGray; // Create table ListObject table = sheet.ListObjects.Create("Data", sheet.Range["A1:B2"]); table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium2; // Save workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` ``` -------------------------------- ### Complete PageSetup Configuration Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/09-pagesetup-class.md A comprehensive example demonstrating how to configure various PageSetup properties including orientation, paper size, margins, headers, footers, print settings, and repeating rows. The output is saved as PDF and Excel files. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Add data sheet.Range["A1"].Text = "Sales Report"; sheet.Range["A1"].Style.Font.Size = 16; sheet.Range["A1"].Style.Font.IsBold = true; // Setup page PageSetup pageSetup = sheet.PageSetup; // Set orientation and paper pageSetup.Orientation = PageOrientationType.Landscape; pageSetup.PaperSize = PaperSizeType.PaperA4; // Set margins pageSetup.TopMargin = 0.5; pageSetup.BottomMargin = 0.5; pageSetup.LeftMargin = 0.75; pageSetup.RightMargin = 0.75; // Configure headers pageSetup.CenterHeader = "Q3 2024 Sales Report"; pageSetup.RightHeader = "&D"; // Date // Configure footers pageSetup.LeftFooter = "Page &P of &N"; pageSetup.CenterFooter = "CONFIDENTIAL"; pageSetup.RightFooter = "&T"; // Time // Print settings pageSetup.PrintGridlines = true; pageSetup.CenterHorizontally = true; pageSetup.CenterVertically = false; pageSetup.FitToPagesWide = 1; pageSetup.FitToPagesTall = 2; // Repeat first row on every page pageSetup.PrintTitleRows = "$1:$1"; // Save as PDF for preview workbook.SaveToFile("report.pdf", FileFormat.PDF); // Save as Excel workbook.SaveToFile("report.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Configure Excel Page Setup for Printing Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example covers various Excel page setup options for printing, including print area, title rows/columns, gridlines, headings, black and white mode, comments, quality, error display, and printing order. ```csharp // Specifying the print area PageSetup pageSetup = worksheet.PageSetup; pageSetup.PrintArea = "A1:E19"; // Define column A & E as title columns pageSetup.PrintTitleColumns = "$A:$E"; // Define row numbers 1 as title rows pageSetup.PrintTitleRows = "$1:$2"; // Allow to print with gridlines pageSetup.IsPrintGridlines = true; // Allow to print with row/column headings pageSetup.IsPrintHeadings = true; // Allow to print worksheet in black & white mode pageSetup.BlackAndWhite = true; // Allow to print comments as displayed on worksheet pageSetup.PrintComments = PrintCommentType.InPlace; // Set printing quality pageSetup.PrintQuality = 150; // Allow to print cell errors as N/A pageSetup.PrintErrors = PrintErrorsType.NA; // Set the printing order pageSetup.Order = OrderType.OverThenDown; // Print file workbook.PrintDocument.Print(); ``` -------------------------------- ### Complete Data Sorting Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/13-datasort-filtering.md This example demonstrates creating sample data, configuring the DataSorter to sort by 'Category' ascending and 'Sales' descending, performing the sort, and saving the result. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Create sample data sheet.Range["A1"].Text = "Product"; sheet.Range["B1"].Text = "Category"; sheet.Range["C1"].Text = "Sales"; sheet.Range["D1"].Text = "Date"; // Sample data rows object[][] data = new object[][] { new object[] { "Widget", "Hardware", 1000, "2024-01-15" }, new object[] { "Gadget", "Software", 1500, "2024-01-10" }, new object[] { "Widget", "Hardware", 800, "2024-01-20" }, new object[] { "Gadget", "Software", 1200, "2024-01-05" }, new object[] { "Part", "Components", 500, "2024-01-25" } }; // Insert data int row = 2; foreach (var dataRow in data) { sheet.Range[row, 1].Text = dataRow[0].ToString(); sheet.Range[row, 2].Text = dataRow[1].ToString(); sheet.Range[row, 3].Value = dataRow[2]; sheet.Range[row, 4].Text = dataRow[3].ToString(); row++; } // Sort by Category ascending, then Sales descending DataSorter sorter = workbook.DataSorter; sorter.IsIncludeTitle = true; sorter.SortColumns.Add(2, OrderBy.Ascending); // Category (A-Z) sorter.SortColumns.Add(3, OrderBy.Descending); // Sales (high to low) sorter.Sort(sheet.Range["A1:D6"]); workbook.SaveToFile("sorted_data.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Complete Table Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/08-table-listobject-class.md A comprehensive example showing the creation of a workbook, worksheet, sample data insertion, table creation, configuration of table properties (style, visibility, stripes), adding a totals row with functions, and adjusting column widths before saving. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Create sample data sheet.Range["A1"].Text = "Product"; sheet.Range["B1"].Text = "Category"; sheet.Range["C1"].Text = "Units"; sheet.Range["D1"].Text = "Price"; object[][] sampleData = new object[][] { new object[] { "Widget A", "Hardware", 100, 29.99 }, new object[] { "Widget B", "Hardware", 150, 39.99 }, new object[] { "Gadget X", "Software", 75, 49.99 }, new object[] { "Gadget Y", "Software", 200, 59.99 }, new object[] { "Part Z", "Components", 500, 9.99 } }; // Insert data int row = 2; foreach (var data in sampleData) { sheet.Range[row, 1].Text = data[0].ToString(); sheet.Range[row, 2].Text = data[1].ToString(); sheet.Range[row, 3].Value = data[2]; sheet.Range[row, 4].Value = data[3]; row++; } // Create table ListObject table = sheet.ListObjects.Create("ProductTable", sheet.Range["A1:D6"]); // Configure table properties table.DisplayName = "Products"; table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium2; table.ShowFirstColumn = false; table.ShowLastColumn = false; table.ShowRowStripes = true; table.ShowColumnStripes = false; table.DisplayFilterButton = true; table.DisplayHeaderRow = true; // Add totals row table.DisplayTotalRow = true; table.Columns[0].TotalsRowLabel = "TOTAL"; table.Columns[1].TotalsRowLabel = string.Empty; table.Columns[2].TotalsRowFunction = TotalsRowFunction.Sum; table.Columns[3].TotalsRowFunction = TotalsRowFunction.Sum; // Adjust column widths sheet.AutoFitColumn(1); sheet.AutoFitColumn(2); sheet.AutoFitColumn(3); sheet.AutoFitColumn(4); // Save workbook.SaveToFile("table_example.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Configure Page Setup Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/QUICK-REFERENCE.md Sets basic page setup properties like orientation, paper size, and margins for the worksheet. ```csharp PageSetup ps = sheet.PageSetup; ps.Orientation = PageOrientationType.Landscape; ps.PaperSize = PaperSizeType.PaperA4; ps.TopMargin = 0.5; ps.BottomMargin = 0.5; ps.LeftMargin = 0.75; ps.RightMargin = 0.75; ``` -------------------------------- ### Complete Cell Styling Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/04-cellstyle-class.md A comprehensive example showing how to create and apply multiple custom styles for headers, data, and alternating rows, including borders and number formatting. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Create a custom style CellStyle headerStyle = workbook.Styles.Add("HeaderStyle"); headerStyle.Font.FontName = "Calibri"; headerStyle.Font.Size = 14; headerStyle.Font.IsBold = true; headerStyle.Font.Color = Color.White; headerStyle.Color = Color.DarkBlue; headerStyle.HorizontalAlignment = HorizontalAlignType.Center; headerStyle.VerticalAlignment = VerticalAlignType.Center; headerStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; headerStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; headerStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; headerStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; // Apply header style CellRange header = sheet.Range["A1:D1"]; header.CellStyleName = "HeaderStyle"; header.Text = "Sales Report"; // Create data style CellStyle dataStyle = workbook.Styles.Add("DataStyle"); dataStyle.NumberFormat = "$#,##0.00"; dataStyle.HorizontalAlignment = HorizontalAlignType.Right; dataStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; // Apply to data sheet.Range["B2"].CellStyleName = "DataStyle"; sheet.Range["B2"].NumberValue = 1234.56; // Alternate row styling CellStyle oddStyle = workbook.Styles.Add("OddRow"); oddStyle.Color = Color.White; CellStyle evenStyle = workbook.Styles.Add("EvenRow"); evenStyle.Color = Color.LightGray; for (int i = 2; i <= 10; i++) { if (i % 2 == 0) sheet.Range[i, 1].CellStyleName = "EvenRow"; else sheet.Range[i, 1].CellStyleName = "OddRow"; } workbook.SaveToFile("styled.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Complete Chart Creation and Configuration Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/05-chart-class.md Demonstrates creating, configuring, and saving multiple chart types (Column, Line, Pie) within a workbook. This example covers setting data ranges, titles, positions, and formatting for chart areas and legends. ```csharp Workbook workbook = new Workbook(); workbook.LoadFromFile("ChartData.xlsx"); Worksheet sheet = workbook.Worksheets[0]; // Create column chart Chart columnChart = sheet.Charts.Add(ExcelChartType.Column); columnChart.ChartTitle = "Quarterly Sales"; columnChart.DataRange = sheet.Range["A1:D5"]; columnChart.SeriesDataFromRange = false; columnChart.ChartTitleArea.IsBold = true; columnChart.ChartTitleArea.Size = 14; columnChart.ChartTitleArea.FontName = "Arial"; // Position chart columnChart.LeftColumn = 1; columnChart.TopRow = 10; columnChart.RightColumn = 10; columnChart.BottomRow = 25; // Format chart area columnChart.ChartArea.ForeGroundColor = Color.White; columnChart.ChartArea.Shadow.SoftEdge = 15; // Configure legend columnChart.HasLegend = true; columnChart.Legend.Position = LegendPosition.Bottom; // Create line chart Chart lineChart = sheet.Charts.Add(ExcelChartType.Line); lineChart.ChartTitle = "Trend Analysis"; lineChart.ChartType = ExcelChartType.Line3D; lineChart.DataRange = sheet.Range["A1:B15"]; lineChart.LeftColumn = 11; lineChart.TopRow = 10; lineChart.RightColumn = 20; lineChart.BottomRow = 25; // Create pie chart Chart pieChart = sheet.Charts.Add(ExcelChartType.Pie); pieChart.ChartTitle = "Market Share"; pieChart.DataRange = sheet.Range["A1:B8"]; pieChart.ChartTitleArea.Color = Color.DarkBlue; pieChart.LeftColumn = 1; pieChart.TopRow = 26; pieChart.RightColumn = 10; pieChart.BottomRow = 35; // Save workbook workbook.SaveToFile("charts.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Complete Enum Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/12-types-and-enums.md Demonstrates the comprehensive usage of various Spire.XLS enums and features to configure a workbook, including setting cell styles, creating charts, and defining page setup options before saving the file. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Use various enums to configure worksheet CellRange header = sheet.Range["A1"]; header.Text = "Sales Report"; header.Style.Font.Underline = UnderlineType.Double; header.Style.Font.Color = Color.FromKnownColor(ExcelColors.DarkBlue); header.Style.HorizontalAlignment = HorizontalAlignType.Center; header.Style.VerticalAlignment = VerticalAlignType.Center; header.Style.Color = Color.LightGray; // Create a chart Chart chart = sheet.Charts.Add(ExcelChartType.Column); chart.DataRange = sheet.Range["A1:C10"]; // Configure page setup sheet.PageSetup.Orientation = PageOrientationType.Landscape; sheet.PageSetup.PaperSize = PaperSizeType.PaperA4; // Save with specific format workbook.SaveToFile("report.xlsx", ExcelVersion.Version2013); workbook.SaveToFile("report.pdf", FileFormat.PDF); workbook.Dispose(); ``` -------------------------------- ### Page Setup Properties Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/09-pagesetup-class.md Access and modify page setup properties like orientation, paper size, margins, headers, and footers. ```APIDOC ## PageSetup Class Controls printing and page layout settings for worksheets. ### Accessing PageSetup ```csharp Worksheet sheet = workbook.Worksheets[0]; PageSetup pageSetup = sheet.PageSetup; ``` ### Page Orientation #### Property: Orientation - **Type**: PageOrientationType - **Description**: Gets or sets page orientation. - **Values**: `PageOrientationType.Portrait` (Vertical), `PageOrientationType.Landscape` (Horizontal) ### Paper Size #### Property: PaperSize - **Type**: PaperSizeType - **Description**: Gets or sets the paper size. - **Supported Sizes**: `PaperSizeType.PaperLetter`, `PaperSizeType.PaperA4`, `PaperSizeType.PaperA3`, `PaperSizeType.PaperA5`, `PaperSizeType.PaperB4`, `PaperSizeType.PaperB5`, `PaperSizeType.PaperLedger`, `PaperSizeType.PaperLegal` #### Method: SetCustomPaperSize - **Description**: Sets custom paper dimensions. - **Parameters**: - **width** (float) - Required - Width in inches - **height** (float) - Required - Height in inches ### Margins #### Properties: - **TopMargin** (double) - Top margin in inches - **BottomMargin** (double) - Bottom margin in inches - **LeftMargin** (double) - Left margin in inches - **RightMargin** (double) - Right margin in inches - **HeaderMargin** (double) - Header margin from top - **FooterMargin** (double) - Footer margin from bottom ### Headers and Footers #### Header Properties: - **LeftHeader** (string) - Left section of header - **CenterHeader** (string) - Center section of header - **RightHeader** (string) - Right section of header - **LeftHeaderImage** (Image) - Image for left header - **CenterHeaderImage** (Image) - Image for center header - **RightHeaderImage** (Image) - Image for right header #### Footer Properties: - **LeftFooter** (string) - Left section of footer - **CenterFooter** (string) - Center section of footer - **RightFooter** (string) - Right section of footer #### Header/Footer Codes: - `&P`: Page number - `&G`: Image placeholder - `&D`: Current date - `&T`: Current time - `&F`: File name - `&A`: Worksheet name - `&"FontName"`: Font specification - `&##`: Font size - `&B`: Bold - `&I`: Italic - `&U`: Underline ### Scaling and Fit Options #### Property: Scale - **Type**: int - **Description**: Gets or sets the print scale percentage. #### Fit to Pages - **FitToPagesWide** (int) - Number of pages wide (1 means fit to one page wide) - **FitToPagesTall** (int) - Number of pages tall (1 means fit to one page tall) ``` -------------------------------- ### Complete Comment Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/10-comment-class.md Demonstrates how to add comments to cells, format them with rich text, and control their properties like visibility, size, and alignment. This example covers adding comments to headers, data cells, and applying different styling. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Create cells with data sheet.Range["A1"].Text = "Product"; sheet.Range["A1"].Style.Font.IsBold = true; sheet.Range["B1"].Text = "Price"; sheet.Range["B1"].Style.Font.IsBold = true; sheet.Range["A2"].Text = "Widget"; sheet.Range["B2"].Value = 29.99; sheet.Range["A3"].Text = "Gadget"; sheet.Range["B3"].Value = 49.99; // Add comment to Product header Comment prodComment = sheet.Range["A1"].Comment; prodComment.RichText.Text = "Spire.XLS for .NET:\nStandalone Excel component for conversion, data manipulation, charts in workbook etc."; prodComment.IsVisible = true; prodComment.Width = 300; prodComment.Height = 150; prodComment.Left = 40; prodComment.Top = 20; prodComment.HAlignment = CommentHAlignType.Justified; prodComment.VAlignment = CommentVAlignType.Center; // Format comment text with rich text ExcelFont boldFont = new ExcelFont(); boldFont.FontName = "Arial"; boldFont.Size = 11; boldFont.IsBold = true; boldFont.Color = Color.Blue; prodComment.RichText.SetFont(0, 11, boldFont); // "Spire.XLS..." in bold blue // Add comment to price Comment priceComment = sheet.Range["B1"].Comment; priceComment.RichText.Text = "Price in USD\nSubject to change without notice"; priceComment.IsVisible = true; priceComment.Width = 250; priceComment.Height = 100; priceComment.Left = 100; priceComment.Top = 70; priceComment.HAlignment = CommentHAlignType.Left; priceComment.VAlignment = CommentVAlignType.Top; // Add comment to data cell Comment dataComment = sheet.Range["B2"].Comment; dataComment.RichText.Text = "Updated 2024"; dataComment.IsVisible = false; // Hidden comment, shown on hover dataComment.Width = 150; dataComment.Height = 50; // Add comment with different styling to another cell Comment comment3 = sheet.Range["A3"].Comment; RichText rt = comment3.RichText; rt.Text = "Best Seller"; ExcelFont redFont = new ExcelFont(); redFont.FontName = "Calibri"; redFont.Size = 10; redFont.Color = Color.Red; redFont.IsItalic = true; comment3.RichText.SetFont(0, 11, redFont); comment3.IsVisible = true; comment3.Width = 200; comment3.Height = 80; comment3.HAlignment = CommentHAlignType.Center; comment3.VAlignment = CommentVAlignType.Center; // Save workbook workbook.SaveToFile("with_comments.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Complete Workbook Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/01-workbook-class.md Demonstrates the complete lifecycle of a workbook, from creation and data manipulation to saving in different formats and releasing resources. ```csharp // Create a new workbook Workbook workbook = new Workbook(); // Add worksheets workbook.CreateEmptySheets(3); // Access first worksheet Worksheet sheet = workbook.Worksheets[0]; sheet.Name = "Sales Data"; // Add data to cells sheet.Range["A1"].Text = "Product"; sheet.Range["B1"].Text = "Q1"; sheet.Range["A2"].Text = "Widget"; sheet.Range["B2"].Value = 1000; // Save the workbook workbook.SaveToFile("sales.xlsx", ExcelVersion.Version2013); workbook.SaveToFile("sales.pdf", FileFormat.PDF); // Release resources workbook.Dispose(); ``` -------------------------------- ### Insert Rows and Columns Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Provides examples for inserting single or multiple rows and columns at a specified index. The index is zero-based. ```csharp // Insert a row into the worksheet at index 2 worksheet.InsertRow(2); // Insert a column into the worksheet at index 2 worksheet.InsertColumn(2); // Insert multiple rows into the worksheet starting at index 5, with a count of 2 worksheet.InsertRow(5, 2); // Insert multiple columns into the worksheet starting at index 5, with a count of 2 worksheet.InsertColumn(5, 2); ``` -------------------------------- ### Create Bubble Chart Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example demonstrates how to create a bubble chart, set its title, data range, bubble values, and position on the worksheet. ```csharp // Add a Bubble chart to the worksheet Chart chart = sheet.Charts.Add(ExcelChartType.Bubble); // Set the title of the chart chart.ChartTitle = "Bubble"; chart.ChartTitleArea.IsBold = true; chart.ChartTitleArea.Size = 12; // Specify the range of data for the chart chart.DataRange = sheet.Range["A1:C5"]; chart.SeriesDataFromRange = false; // Set the range of values for the bubbles in the chart chart.Series[0].Bubbles = sheet.Range["C2:C5"]; // Set the position of the chart on the worksheet chart.LeftColumn = 7; chart.TopRow = 6; chart.RightColumn = 16; chart.BottomRow = 29; ``` -------------------------------- ### Implement Array Formulas in Excel Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Shows how to implement array formulas using Spire.XLS in C#. This example uses the LINEST function. ```csharp // Create a workbook Workbook workbook = new Workbook(); // Get the first sheet Worksheet sheet = workbook.Worksheets[0]; // Set number values for cells A1:C3 sheet.Range["A1"].NumberValue = 1; sheet.Range["A2"].NumberValue = 2; sheet.Range["A3"].NumberValue = 3; sheet.Range["B1"].NumberValue = 4; sheet.Range["B2"].NumberValue = 5; sheet.Range["B3"].NumberValue = 6; sheet.Range["C1"].NumberValue = 7; sheet.Range["C2"].NumberValue = 8; sheet.Range["C3"].NumberValue = 9; // Write array formula sheet.Range["A5:C6"].FormulaArray="=LINEST(A1:A3,B1:C3,TRUE,TRUE)"; // Calculate Formulas workbook.CalculateAllValue(); ``` -------------------------------- ### Creating and Initializing a Cell Comment Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/10-comment-class.md Demonstrates how to get or create a comment on a cell and set its initial text and visibility. ```csharp Worksheet sheet = workbook.Worksheets[0]; // Get or create a comment on a cell Comment comment = sheet.Range["A1"].Comment; comment.RichText.Text = "This is a comment"; comment.IsVisible = true; ``` -------------------------------- ### LeftColumn and TopRow Properties Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/05-chart-class.md Get or set the starting column and row for the chart's position on the worksheet. ```APIDOC ## LeftColumn and TopRow Properties ### Description Gets or sets the starting position (column and row). ### Method ```csharp public int LeftColumn { get; set; } public int TopRow { get; set; } ``` ### Example ```csharp chart.LeftColumn = 5; chart.TopRow = 2; ``` ``` -------------------------------- ### Example of a URL Hyperlink Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/11-hyperlink-class.md Sets a URL hyperlink for a cell, using the cell's existing text as the display text. ```csharp CellRange urlCell = sheet.Range["D10"]; urlCell.HyperLink.Address = "https://en.wikipedia.org/wiki/Chicago"; urlCell.HyperLink.TextToDisplay = sheet.Range["D10"].Text; urlCell.HyperLink.Type = HyperLinkType.Url; ``` -------------------------------- ### Minimal Example: Create and Save Excel File Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/QUICK-REFERENCE.md Demonstrates the basic steps to create a workbook, add data, format a header, create a table, save the file, and dispose of resources. ```csharp using Spire.Xls; using System.Drawing; Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Add data sheet.Range["A1"].Text = "Name"; sheet.Range["B1"].Text = "Value"; sheet.Range["A2"].Text = "Item A"; sheet.Range["B2"].Value = 100; // Format header sheet.Range["A1:B1"].Style.Font.IsBold = true; sheet.Range["A1:B1"].Style.Color = Color.LightGray; // Create table ListObject table = sheet.ListObjects.Create("Data", sheet.Range["A1:B2"]); table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium2; // Save and cleanup workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Retrieve Built-in and Custom Properties from Excel Workbook in C# Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example shows how to retrieve both built-in and custom document properties from an Excel workbook and save them to a text file. It iterates through the properties collections to get names and values. ```csharp // Create a workbook Workbook workbook = new Workbook(); // Load the document from disk workbook.LoadFromFile("WorksheetSample1.xlsx"); // Get the general excel properties BuiltInDocumentProperties properties1 = workbook.DocumentProperties; StringBuilder sb = new StringBuilder(); sb.AppendLine("Excel Properties:"); for (int i = 0; i < properties1.Count; i++) { // Get property name string name = properties1[i].Name; // Get property vaule string value = properties1[i].Value.ToString(); sb.AppendLine(name + ": " + value); } sb.AppendLine(); //Get the custom properties ICustomDocumentProperties properties2 = workbook.CustomDocumentProperties; sb.AppendLine("Custom Properties:"); for (int i = 0; i < properties2.Count; i++) { // Get property name string name = properties2[i].Name; // Get property vaule string value = properties2[i].Value.ToString(); sb.AppendLine(name + ": " + value); } //Save the document string output = "GetProperties.txt"; File.WriteAllText(output, sb.ToString()); // Dispose of the workbook object to release resources workbook.Dispose(); ``` -------------------------------- ### Basic Excel File Creation with Text and Auto-Sizing Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Demonstrates creating a new workbook, setting text in cell A1 to 'Hello World', and auto-fitting the columns to adjust to the content. ```csharp // Create a new workbook object Workbook workbook = new Workbook(); // Get the reference to the first sheet in the workbook Worksheet sheet = workbook.Worksheets[0]; // Set the cell value of cell A1 to "Hello World" sheet.Range["A1"].Text = "Hello World"; // Auto-fit the columns to adjust their width based on the content sheet.Range["A1"].AutoFitColumns(); ``` -------------------------------- ### Get Excel Paper Dimensions Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Retrieves the width and height dimensions for various standard paper sizes (A2, A3, A4, Letter) from an Excel worksheet's page setup. Ensures resources are released by disposing the workbook. ```csharp // Create a new workbook Workbook workbook = new Workbook(); // Get the first worksheet in the workbook Worksheet sheet = workbook.Worksheets[0]; // Get the dimensions of A2 paper sheet.PageSetup.PaperSize = PaperSizeType.A2Paper; float a2Width = sheet.PageSetup.PageWidth; float a2Height = sheet.PageSetup.PageHeight; // Get the dimensions of A3 paper sheet.PageSetup.PaperSize = PaperSizeType.PaperA3; float a3Width = sheet.PageSetup.PageWidth; float a3Height = sheet.PageSetup.PageHeight; // Get the dimensions of A4 paper sheet.PageSetup.PaperSize = PaperSizeType.PaperA4; float a4Width = sheet.PageSetup.PageWidth; float a4Height = sheet.PageSetup.PageHeight; // Get the dimensions of letter-sized paper sheet.PageSetup.PaperSize = PaperSizeType.PaperLetter; float letterWidth = sheet.PageSetup.PageWidth; float letterHeight = sheet.PageSetup.PageHeight; // Dispose of the workbook object to release resources workbook.Dispose(); ``` -------------------------------- ### Complete Pivot Table Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/06-pivottable-class.md Demonstrates the creation and configuration of a pivot table from source data. This includes preparing data, creating a pivot cache, defining fields, applying styles, and saving the workbook. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Prepare source data sheet.Range["A1"].Value = "Product"; sheet.Range["B1"].Value = "Month"; sheet.Range["C1"].Value = "Region"; sheet.Range["D1"].Value = "Sales"; // Sample data rows object[][] data = new object[][] { new object[] { "Widget", "January", "North", 1000 }, new object[] { "Widget", "January", "South", 800 }, new object[] { "Widget", "February", "North", 1200 }, new object[] { "Widget", "February", "South", 900 }, new object[] { "Gadget", "January", "North", 1500 }, new object[] { "Gadget", "January", "South", 1100 }, new object[] { "Gadget", "February", "North", 1600 }, new object[] { "Gadget", "February", "South", 1200 } }; // Insert data into worksheet int row = 2; foreach (var dataRow in data) { sheet.Range[row, 1].Value = dataRow[0]; sheet.Range[row, 2].Value = dataRow[1]; sheet.Range[row, 3].Value = dataRow[2]; sheet.Range[row, 4].Value = dataRow[3]; row++; } // Create pivot cache from source data CellRange sourceRange = sheet.Range["A1:D9"]; PivotCache cache = workbook.PivotCaches.Add(sourceRange); // Create pivot table PivotTable pivotTable = sheet.PivotTables.Add("SalesAnalysis", sheet.Range["F2"], cache); // Configure row fields PivotField productField = pivotTable.PivotFields["Product"] as PivotField; productField.Axis = AxisTypes.Row; PivotField monthField = pivotTable.PivotFields["Month"] as PivotField; monthField.Axis = AxisTypes.Row; // Configure column fields PivotField regionField = pivotTable.PivotFields["Region"] as PivotField; regionField.Axis = AxisTypes.Column; // Configure data field PivotField salesField = pivotTable.PivotFields["Sales"] as PivotField; pivotTable.DataFields.Add(salesField, "Total Sales", SubtotalTypes.Sum); // Apply style pivotTable.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium2; // Calculate and auto-fit pivotTable.CalculateData(); for (int i = 6; i <= 10; i++) { sheet.AutoFitColumn(i); } // Save workbook workbook.SaveToFile("pivot_table.xlsx", ExcelVersion.Version2010); workbook.Dispose(); ``` -------------------------------- ### Retrieve Data Point Values from Excel Chart Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example demonstrates how to extract data point values from a chart's series. It iterates through the `Values` cell range of the specified chart series to get each data point's value. ```csharp // Get the first sheet Worksheet sheet = workbook.Worksheets[0]; // Get the chart Chart chart = sheet.Charts[0]; // Get the first series of the chart ChartSerie cs = chart.Series[0]; foreach (CellRange cr in cs.Values) { // Get the data point value string value = cr.Value; } ``` -------------------------------- ### Create and Save Excel File Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/README.md A minimal C# example demonstrating how to create a new Excel workbook, add data to cells, format a header row, create a table, and save the workbook to an .xlsx file. Ensure to dispose of the workbook when done. ```csharp using Spire.Xls; using System.Drawing; // Create workbook Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Add data sheet.Range["A1"].Text = "Name"; sheet.Range["B1"].Text = "Sales"; sheet.Range["A2"].Text = "Product A"; sheet.Range["B2"].Value = 1000; // Format header sheet.Range["A1:B1"].Style.Font.IsBold = true; sheet.Range["A1:B1"].Style.Color = Color.LightGray; // Create table ListObject table = sheet.ListObjects.Create("Data", sheet.Range["A1:B2"]); table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium2; // Save workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Set and Get Cell Number Value Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/03-cellrange-class.md Use the NumberValue property to specifically set or get the cell's value as a double-precision floating-point number. ```csharp cell.NumberValue = 123.45; double amount = cell.NumberValue; ``` -------------------------------- ### Delete Multiple Rows and Columns Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Demonstrates deleting a specified number of rows or columns starting from a given index. Ensure the starting index and count are valid. ```csharp // Delete 4 rows starting from the fifth row (rows 5, 6, 7, and 8) sheet.DeleteRow(5, 4); // Delete 2 columns starting from the second column (columns B and C) sheet.DeleteColumn(2, 2); ``` -------------------------------- ### Complete Workflow for Professional Printing Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/09-pagesetup-class.md An end-to-end example showing how to load an Excel file, configure PageSetup for professional printing (orientation, margins, headers, footers, fitting to one page), and save the result as both an Excel file and a PDF. ```csharp Workbook workbook = new Workbook(); workbook.LoadFromFile("data.xlsx"); Worksheet sheet = workbook.Worksheets[0]; PageSetup ps = sheet.PageSetup; // Configure for professional printing ps.Orientation = PageOrientationType.Portrait; ps.PaperSize = PaperSizeType.PaperLetter; ps.TopMargin = 1; ps.BottomMargin = 1; ps.LeftMargin = 1; ps.RightMargin = 1; ps.LeftHeader = "&\"Calibri\"&12 My Report"; ps.CenterHeader = "&D"; ps.RightHeader = "&P"; ps.LeftFooter = "Confidential"; ps.CenterFooter = "&A"; ps.RightFooter = "&T"; ps.PrintGridlines = false; ps.PrintHeadings = false; ps.CenterHorizontally = true; // Fit to one page ps.FitToPagesWide = 1; ps.FitToPagesTall = 1; // Save for printing workbook.SaveToFile("to_print.xlsx", ExcelVersion.Version2013); workbook.SaveToFile("to_print.pdf", FileFormat.PDF); workbook.Dispose(); ``` -------------------------------- ### Create a Simple Excel File Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/00-api-overview.md Demonstrates how to create a new Excel workbook, add text to a cell, format it, and save the file. Ensure to dispose of the workbook after use. ```csharp using Spire.Xls; Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; sheet.Range["A1"].Text = "Hello World"; sheet.Range["A1"].Style.Font.IsBold = true; sheet.Range["A1"].AutoFitColumns(); workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Set and Get Cell Value (Object) Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/03-cellrange-class.md The Value property allows setting or getting cell content of any object type. Use Value2 to access the raw underlying value. ```csharp cell.Value = 100; cell.Value2 = DateTime.Now; ``` -------------------------------- ### Complete Spire.XLS Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/03-cellrange-class.md Demonstrates a comprehensive workflow including setting cell values, applying styles, merging cells, adding formulas, using rich text, auto-fitting columns, finding and formatting text, and saving the workbook. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; // Access cells and set values CellRange headerCell = sheet.Range["A1"]; headerCell.Text = "Product"; headerCell.Style.Font.IsBold = true; headerCell.Style.Font.Color = Color.White; headerCell.Style.Color = Color.DarkBlue; // Set numeric value CellRange priceCell = sheet.Range["B2"]; priceCell.NumberValue = 99.99; priceCell.Style.NumberFormat = "$#,##0.00"; // Merge cells sheet.Range["A1:D1"].Merge(); sheet.Range["A1:D1"].Text = "Sales Report"; // Add formula sheet.Range["B10"].Formula = "=SUM(B2:B9)"; // Rich text CellRange richCell = sheet.Range["A5"]; RichText rt = richCell.RichText; rt.Text = "Click here"; richCell.Style.Font.Underline = true; // Auto-fit sheet.Range["A1:Z100"].AutoFitColumns(); // Find and format CellRange[] cells = sheet.FindAllString("Total", false, true); foreach (CellRange cell in cells) { cell.Style.Font.IsBold = true; } workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Complete Worksheet Usage Example Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/02-worksheet-class.md Demonstrates a comprehensive workflow including creating a workbook, adding headers and data, finding and styling cells, exporting to a DataTable, and saving the workbook. ```csharp Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; sheet.Name = "Sales"; // Add headers sheet.Range["A1"].Text = "Product"; sheet.Range["B1"].Text = "Units"; sheet.Range["C1"].Text = "Price"; // Add data sheet.Range["A2"].Text = "Widget"; sheet.Range["B2"].Value = 100; sheet.Range["C2"].Value = 25.50; // Find and highlight CellRange[] found = sheet.FindAllString("Widget", false, true); foreach (CellRange range in found) { range.Style.Color = Color.Yellow; } // Export to DataTable DataTable dt = sheet.ExportDataTable(); // Save workbook.SaveToFile("sheet.xlsx", ExcelVersion.Version2013); workbook.Dispose(); ``` -------------------------------- ### Create Doughnut Chart with Percentage Labels Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example demonstrates creating a doughnut chart and enabling percentage labels for each data point, along with setting the legend position. ```csharp // Add a new chart and set its type to Doughnut Chart chart = sheet.Charts.Add(); chart.ChartType = ExcelChartType.Doughnut; // Set the data range for the chart chart.DataRange = sheet.Range["A1:B5"]; chart.SeriesDataFromRange = false; // Set the position of the chart on the worksheet chart.LeftColumn = 4; chart.TopRow = 2; chart.RightColumn = 12; chart.BottomRow = 22; // Set the chart title chart.ChartTitle = "Market share by country"; chart.ChartTitleArea.IsBold = true; chart.ChartTitleArea.Size = 12; // Enable percentage labels for each data point foreach (ChartSerie cs in chart.Series) { cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true; } // Set the legend position to the top chart.Legend.Position = LegendPositionType.Top; ``` -------------------------------- ### Create and Save Excel to Stream in C# Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/README.md Demonstrates how to dynamically create an Excel workbook, add content, and save it to a file stream. Also shows how to load an Excel file from a stream. ```csharp using Spire.Xls; using System.IO; namespace CreateExcelFiles { class Program { static void Main(string[] args) { //A: Dynamically create Excel file and save it to stream Workbook wbToStream = new Workbook(); Worksheet sheet = wbToStream.Worksheets[0]; sheet.Range["C10"].Text = "The sample demonstrates how to save an Excel workbook to stream."; FileStream file_stream = new FileStream("To_stream.xls", FileMode.Create); wbToStream.SaveToStream(file_stream); file_stream.Close(); System.Diagnostics.Process.Start("To_stream.xls"); //B. Load Excel file from stream Workbook wbFromStream = new Workbook(); FileStream fileStream = File.OpenRead("sample.xls"); fileStream.Seek(0, SeekOrigin.Begin); wbFromStream.LoadFromStream(fileStream); wbFromStream.SaveToFile("From_stream.xls", ExcelVersion.Version97to2003); fileStream.Dispose(); System.Diagnostics.Process.Start("From_stream.xls"); } } } ``` -------------------------------- ### Name Property Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/08-table-listobject-class.md Gets or sets the table name, which is used in formulas. ```APIDOC ## Name Property ### Description Gets or sets the table name (used in formulas). ### Method `public string Name { get; set; }` ### Request Example ```csharp ListObject table = sheet.ListObjects[0]; string name = table.Name; // Returns table name table.Name = "NewTableName"; // Change name ``` ``` -------------------------------- ### Create Sunburst Chart Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md This example demonstrates the creation of a sunburst chart, setting its data range, title, and data label formatting. It also includes an option to hide the legend. ```csharp // Add chart Chart officeChart = sheet.Charts.Add(); // Set chart type as Sunburst officeChart.ChartType = ExcelChartType.SunBurst; //Set data range in the worksheet officeChart.DataRange = sheet["A1:D16"]; officeChart.TopRow = 1; officeChart.BottomRow = 17; officeChart.LeftColumn = 6; officeChart.RightColumn = 14; // Set the chart title officeChart.ChartTitle = "Sales by quarter"; // Formatting data labels officeChart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Size = 8; // Hiding the legend officeChart.HasLegend = false; ``` -------------------------------- ### GetCellType Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/02-worksheet-class.md Gets the type of data in a cell, with an option to include relative formulas. ```APIDOC ## GetCellType ### Description Gets the type of data in a cell. ### Method `GetCellType(int row, int column, bool includeRelativeFormula)` ### Parameters #### Path Parameters - **row** (int) - Yes - Row index - **column** (int) - Yes - Column index - **includeRelativeFormula** (bool) - Yes - Include relative formulas ### Returns CellValueType (Text, Number, Blank, Boolean, DateTime, etc.) ``` -------------------------------- ### Create a New Workbook Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/01-workbook-class.md Initializes a new, empty Excel workbook with a single default worksheet. Use this to start a new Excel file from scratch. ```csharp Workbook workbook = new Workbook(); ``` -------------------------------- ### Get Plot Area Settings Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/05-chart-class.md Retrieves the plot area settings for a chart. ```csharp public PlotArea PlotArea { get; } ``` -------------------------------- ### DisplayTotalRow Property Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/08-table-listobject-class.md Gets or sets whether the totals row is displayed in the table. ```APIDOC ## DisplayTotalRow Property ### Description Gets or sets whether to display the totals row. ### Method `public bool DisplayTotalRow { get; set; }` ### Request Example ```csharp table.DisplayTotalRow = true; ``` ``` -------------------------------- ### How to Use This Reference Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/README.md Guidance on navigating and utilizing the Spire.XLS API documentation effectively. ```APIDOC ## How to Use This Reference 1. **For Overview**: Start with [00-api-overview.md](00-api-overview.md) 2. **For Specific Class**: Jump directly to the relevant documentation file 3. **For Code Examples**: Each file includes complete, working examples 4. **For Types**: Refer to [12-types-and-enums.md](12-types-and-enums.md) for enumeration definitions 5. **For Tasks**: Use the "Quick API Map" table above to find relevant methods ``` -------------------------------- ### DataRange Property Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/_autodocs/05-chart-class.md Gets or sets the cell range that provides data for the chart. ```APIDOC ## DataRange Property ### Description Gets or sets the data range for the chart. ### Method ```csharp public CellRange DataRange { get; set; } ``` ### Example ```csharp Chart chart = sheet.Charts.Add(ExcelChartType.Column); chart.DataRange = sheet.Range["A1:C10"]; chart.SeriesDataFromRange = false; ``` ``` -------------------------------- ### Load and Save Kingsoft Spreadsheets Files (.et, .ett) Source: https://github.com/eiceblue/spire.xls-for-.net/blob/master/CS-Examples/xls_cs.md Demonstrates loading and saving files in Kingsoft Spreadsheets formats (.et and .ett) using Spire.XLS. Ensure the data files are in the correct directory. ```csharp using Spire.Xls; //create a workbook Workbook workbook = new Workbook(); //load .et or .ett file workbook.LoadFromFile(@"..\..\..\..\..\Data\Sample-et.et"); //workbook.LoadFromFile(@"..\..\..\..\..\Data\Sample-ett.ett"); //save to .et or .ett file workbook.SaveToFile("result.et", FileFormat.ET); //workbook.SaveToFile("result.ett", FileFormat.ETT); // Dispose of the workbook object to release resources workbook.Dispose(); ```