### Complete Workbook Configuration Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/configuration.md A comprehensive example demonstrating how to configure a workbook, including page setup, document properties, cell styles, data entry, column widths, and saving the file. ```java public class ConfigureWorkbook { public static void main(String[] args) { Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); sheet.setName("Sales Report"); // Configure page setup PageSetup setup = sheet.getPageSetup(); setup.setOrientation(PageOrientation.Landscape); setup.setPaperSize(PaperSize.A4); setup.setTopMargin(0.75); setup.setBottomMargin(0.75); // Configure document properties workbook.getDocumentProperties().setTitle("Q1 2024 Report"); workbook.getDocumentProperties().setAuthor("Finance Team"); // Create header style CellStyle headerStyle = workbook.getStyles().addStyle("header"); ExcelFont font = headerStyle.getFont(); font.setFontName("Calibri"); font.setSize(12); font.isBold(true); headerStyle.getInterior().setColor(Color.darkBlue); // Add data with style sheet.get("A1").setText("Month"); sheet.get("A1").setCellStyleName("header"); sheet.get("B1").setText("Sales"); sheet.get("B1").setCellStyleName("header"); sheet.get("A2").setText("January"); sheet.get("B2").setNumberValue(50000); // Set column widths sheet.getColumn(0).setWidth(15); sheet.getColumn(1).setWidth(15); // Save workbook.saveToFile("output/configured.xlsx", ExcelVersion.Version2013); } } ``` -------------------------------- ### Complete Workbook Formatting Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-additional.md Demonstrates creating a workbook with styled headers, currency formatting, borders, and setting column widths. This example covers comprehensive cell and sheet formatting. ```java // Create styled, formatted, and sorted data Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Create header style CellStyle headerStyle = workbook.getStyles().addStyle("header"); ExcelFont headerFont = headerStyle.getFont(); headerFont.setFontName("Calibri"); headerFont.setSize(12); headerFont.isBold(true); headerFont.setColor(Color.white); Interior headerFill = headerStyle.getInterior(); headerFill.setColor(Color.darkBlue); headerStyle.setVerticalAlignment(VerticalAlignmentType.Center); // Create currency style CellStyle currencyStyle = workbook.getStyles().addStyle("currency"); currencyStyle.setNumberFormat("$#,##0.00"); // Add and format headers sheet.get("A1").setText("Product"); sheet.get("A1").setCellStyleName(headerStyle.getName()); sheet.get("B1").setText("Quantity"); sheet.get("B1").setCellStyleName(headerStyle.getName()); sheet.get("C1").setText("Price"); sheet.get("C1").setCellStyleName(headerStyle.getName()); // Add borders to header sheet.get("A1:C1").borderAround(LineStyleType.Thin); sheet.get("A1:C1").borderInside(LineStyleType.Thin); // Add data sheet.get("A2").setText("Widget"); sheet.get("B2").setInt(100); sheet.get("C2").setNumberValue(29.99); sheet.get("C2").setCellStyleName(currencyStyle.getName()); // Set column widths sheet.getColumn(0).setWidth(15); sheet.getColumn(1).setWidth(12); sheet.getColumn(2).setWidth(12); // Save workbook.saveToFile("output/styled.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### Get Page Setup and Configure Orientation/Margins Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Retrieves the PageSetup object to configure page layout options such as orientation and margins. This is useful for controlling print output. ```java PageSetup setup = sheet.getPageSetup(); setup.setOrientation(PageOrientation.Landscape); setup.setTopMargin(0.5); ``` -------------------------------- ### getPageSetup Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Retrieves the page setup options for the worksheet, including margins and orientation. ```APIDOC ## getPageSetup() ### Description Retrieves page setup options (margins, orientation, etc.). ### Method ```java PageSetup getPageSetup() ``` ### Parameters #### Path Parameters - (none) ### Response #### Success Response (200) - **PageSetup** (PageSetup) - Page setup object ### Request Example ```java PageSetup setup = sheet.getPageSetup(); setup.setOrientation(PageOrientation.Landscape); setup.setTopMargin(0.5); ``` ``` -------------------------------- ### PageSetup Class Methods Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/MANIFEST.md Methods for configuring page setup options like orientation, paper size, and zoom. ```APIDOC ## PageSetup Class ### Description Manages page formatting settings, including orientation, paper size, margins, and zoom level. ### Methods - `setOrientation(PageOrientation orientation)`: Sets the page orientation (e.g., Portrait, Landscape). - `setPaperSize(PaperSizeType paperSize)`: Sets the paper size for printing. - `setZoom(int zoom)`: Sets the zoom level for the page. - Methods for margins are also available. ``` -------------------------------- ### Retrieve and Get Textbox by Name Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Adds a new textbox, sets its name and text, then retrieves it by name to get its text content. ```java ITextBoxShape textBox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65); textBox.setName("FirstTextBox"); extBox.setText("Spire.XLS for Java is a professional Java Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets without using Microsoft Office or Microsoft Excel."); ITextBoxShape FindTextBox = sheet.getTextBoxes().get("FirstTextBox"); String text = FindTextBox.getText(); ``` -------------------------------- ### TextBox.getFormat Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-shapes.md Gets the format object for styling the text box. ```APIDOC ## TextBox.getFormat() ### Description Gets the format object for styling the text box. ### Method ```java Format getFormat() ``` ### Response #### Success Response - **Format** - The format object for the text box ### Request Example ```java Format format = box.getFormat(); format.setFontName("Arial"); format.setFontSize(11); ``` ``` -------------------------------- ### Create a New Workbook Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-workbook.md Instantiate a new Workbook object to start working with an Excel file. This is the initial step for creating or manipulating spreadsheets. ```java Workbook workbook = new Workbook(); ``` -------------------------------- ### Complete Table Creation and Configuration Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-listobject.md Demonstrates the creation, population, styling, and configuration of a table, including setting up the total row with labels and calculations. Saves the result to an Excel file. ```java // Create and populate table data Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Create sample data sheet.get("A1").setText("Product"); sheet.get("B1").setText("Q1 Sales"); sheet.get("C1").setText("Q2 Sales"); sheet.get("A2").setText("Product A"); sheet.get("B2").setNumberValue(10000); sheet.get("C2").setNumberValue(12000); sheet.get("A3").setText("Product B"); sheet.get("B3").setNumberValue(15000); sheet.get("C3").setNumberValue(14000); sheet.get("A4").setText("Product C"); sheet.get("B4").setNumberValue(8000); sheet.get("C4").setNumberValue(9500); // Create table IListObject table = sheet.getListObjects().create( "SalesTable", sheet.get(1, 1, 4, 3) // A1:C4 ); // Apply style table.setBuiltInTableStyle(TableBuiltInStyles.TableStyleLight9); // Configure display table.setShowHeader(true); table.setDisplayTotalRow(true); // Set up total row table.getColumns().get(0).setTotalsRowLabel("Total"); table.getColumns().get(1).setTotalsCalculation(ExcelTotalsCalculation.Sum); table.getColumns().get(2).setTotalsCalculation(ExcelTotalsCalculation.Sum); // Save workbook.saveToFile("output/table_example.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### getFormula() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellrange.md Gets the formula content of a cell as a string. This includes the formula itself, starting with an equals sign. ```APIDOC ## getFormula() ### Description Gets the formula content of a cell. ### Method String getFormula() ### Returns `String` - The cell's formula as a string ### Request Example ```java String formula = sheet.get("A3").getFormula(); // e.g., "=A1+A2" System.out.println("Formula: " + formula); ``` ``` -------------------------------- ### get(int row, int column, int lastRow, int lastColumn) Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Retrieves a range of cells defined by starting and ending row and column coordinates. ```APIDOC ## get(int row, int column, int lastRow, int lastColumn) ### Description Retrieves a range of cells from top-left to bottom-right coordinates. ### Method (Implicitly invoked via Worksheet object) ### Parameters #### Path Parameters - **row** (int) - Required - Starting row number (1-based) - **column** (int) - Required - Starting column number (1-based) - **lastRow** (int) - Required - Ending row number (1-based, inclusive) - **lastColumn** (int) - Required - Ending column number (1-based, inclusive) ### Response #### Success Response - **CellRange** - The requested range ### Request Example ```java CellRange range = sheet.get(1, 1, 10, 5); // Range A1:E10 for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; j++) { range.get(i, j).setText("Cell " + i + "," + j); } } ``` ``` -------------------------------- ### Create Simple Excel File with Hello World Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Creates a basic Excel file and writes 'Hello World' to cell A1, then auto-fits the column width. ```java //Create a workbook Workbook workbook = new Workbook(); //Get the first sheet Worksheet sheet = workbook.getWorksheets().get(0); sheet.get("A1").setText("Hello World"); sheet.get("A1").autoFitColumns(); //Save to file workbook.saveToFile("output/helloWorld.xlsx", ExcelVersion.Version2010); ``` -------------------------------- ### Get Column by Index and Set Total Calculation Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-listobject.md Retrieves a specific table column using its 0-based index and configures its total row calculation, for example, to sum values. ```java ListColumn col = table.getColumns().get(0); col.setTotalsCalculation(ExcelTotalsCalculation.Sum); ``` -------------------------------- ### Complete Chart Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-chart.md Demonstrates the creation of a complete Excel chart with data, series, labels, titles, axes, and legend, then saves it to a file. ```java // Create a workbook and add data Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Add sample data sheet.get("A1").setText("Month"); sheet.get("B1").setText("Sales"); sheet.get("C1").setText("Costs"); sheet.get("A2").setText("January"); sheet.get("B2").setNumberValue(10000); sheet.get("C2").setNumberValue(6000); sheet.get("A3").setText("February"); sheet.get("B3").setNumberValue(12000); sheet.get("C3").setNumberValue(7000); sheet.get("A4").setText("March"); sheet.get("B4").setNumberValue(15000); sheet.get("C4").setNumberValue(8000); // Create chart Chart chart = sheet.getCharts().add(); chart.setChartType(ExcelChartType.Column); chart.setLeft(300); chart.setTop(50); chart.setWidth(500); chart.setHeight(400); // Set data chart.setSeries(sheet.getRange().get("B2:C4")); chart.setCategoryLabels(sheet.getRange().get("A2:A4").getAddress()); // Configure title and axes ChartTitle title = chart.getTitle(); title.setText("Sales vs Costs"); Axis xAxis = chart.getAxisX(); xAxis.setTitle("Month"); Axis yAxis = chart.getAxisY(); yAxis.setTitle("Amount ($)"); // Configure legend Legend legend = chart.getLegend(); legend.setPosition(LegendPosition.Right); // Configure data labels DataLabels labels = chart.getDataLabels(); labels.setShowValue(true); // Save workbook.saveToFile("output/chart_sample.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### Get Cell Displayed Text Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Retrieves the formatted text displayed in an Excel cell after applying number formatting. This is useful for getting the user-visible representation of a cell's value. ```java //Create a workbook Workbook workbook = new Workbook(); //Get first worksheet of the workbook Worksheet sheet = workbook.getWorksheets().get(0); //Set value for B8 CellRange cell = sheet.getRange().get("B8"); cell.setNumberValue(0.012345); //Set the cell style CellStyle style = cell.getCellStyle(); style.setNumberFormat("0.00"); //Get the cell value String cellValue = cell.getValue(); //Get the displayed text of the cell String displayedText = cell.getDisplayedText(); ``` -------------------------------- ### Create a New Workbook and Add Data Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/README.md Demonstrates how to create a new Excel workbook, add worksheets, populate cells with text and numbers, and save the workbook to a file. Ensure to dispose of the workbook when done. ```java import com.spire.xls.*; // Create a new workbook Workbook workbook = new Workbook(); workbook.createEmptySheets(1); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); sheet.setName("Sales Data"); // Add data to cells sheet.get("A1").setText("Product"); sheet.get("B1").setText("Q1 Sales"); sheet.get("A2").setText("Widget A"); sheet.get("B2").setNumberValue(50000); // Save to file workbook.saveToFile("output.xlsx", ExcelVersion.Version2013); workbook.dispose(); ``` -------------------------------- ### Complete Row and Column Manipulation Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-row-column.md Demonstrates comprehensive row and column operations, including adding data, configuring headers, setting widths, hiding rows/columns, and applying formatting. ```java // Create workbook and sheet Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Add sample data sheet.get("A1").setText("Product"); sheet.get("B1").setText("Q1 Sales"); sheet.get("C1").setText("Q2 Sales"); for (int i = 2; i <= 10; i++) { sheet.get(i, 1).setText("Product " + (i - 1)); sheet.get(i, 2).setNumberValue(Math.random() * 10000); sheet.get(i, 3).setNumberValue(Math.random() * 10000); } // Configure header row Row headerRow = sheet.getRow(0); headerRow.setHeight(25); CellStyle headerStyle = workbook.getStyles().addStyle("header"); headerStyle.getFont().isBold(true); headerStyle.getFont().setSize(12); headerStyle.getInterior().setColor(Color.darkBlue); for (int col = 0; col < 3; col++) { sheet.get(1, col + 1).setCellStyleName(headerStyle.getName()); } // Set column widths sheet.getColumn(0).setWidth(20); // Column A sheet.getColumn(1).setWidth(15); // Column B sheet.getColumn(2).setWidth(15); // Column C // Hide a row sheet.getRow(5).setHidden(true); // Hide row 6 // Hide a column sheet.getColumn(2).setHidden(false); // Show column C // Configure data rows for (int i = 1; i < 9; i++) { Row row = sheet.getRow(i); row.setHeight(20); } // Apply number format to sales columns CellStyle currencyStyle = workbook.getStyles().addStyle("currency"); currencyStyle.setNumberFormat("$#,##0.00"); for (int i = 2; i <= 10; i++) { sheet.get(i, 2).setCellStyleName(currencyStyle.getName()); sheet.get(i, 3).setCellStyleName(currencyStyle.getName()); } // Save workbook.saveToFile("output/rows_columns.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### Get Last Column with Data Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Gets the 1-based index of the last column containing data in the worksheet. Returns 0 if the sheet is empty. Useful for determining the data range. ```java int lastCol = sheet.getLastColumn(); System.out.println("Last column with data: " + lastCol); ``` -------------------------------- ### Create and Save a New Workbook Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/INDEX.md Demonstrates how to create a new Excel workbook, add a sheet, set cell content, and save it to a file. ```java Workbook workbook = new Workbook(); workbook.createEmptySheets(1); Worksheet sheet = workbook.getWorksheets().get(0); sheet.get("A1").setText("Hello"); workbook.saveToFile("output.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### Get Last Row with Data Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Gets the 1-based index of the last row containing data in the worksheet. Returns 0 if the sheet is empty. Useful for iterating through rows with content. ```java int lastRow = sheet.getLastRow(); System.out.println("Last row with data: " + lastRow); ``` -------------------------------- ### Set Page Margins Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-additional.md Sets the top, bottom, left, and right margins for the page setup. Margins are specified in inches. ```java setup.setTopMargin(0.75); setup.setBottomMargin(0.75); setup.setLeftMargin(0.5); setup.setRightMargin(0.5); ``` -------------------------------- ### Delete Multiple Rows and Columns from Excel Worksheet Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Deletes a specified number of rows or columns starting from a given index. Ensure the starting index and count are valid to prevent errors. ```java //Delete 4 rows from the fifth row. worksheet.deleteRow(5, 4); //Delete 2 columns from the second column. worksheet.deleteColumn(2, 2); ``` -------------------------------- ### PageSetup Class Configuration Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-additional.md Methods for configuring page setup options, including orientation, paper size, margins, zoom, and printing options. ```APIDOC ## setOrientation(PageOrientation orientation) ### Description Sets page orientation. ### Method void ### Parameters #### Path Parameters - **orientation** (PageOrientation) - Required - The page orientation (e.g., Landscape, Portrait). ### Response #### Success Response (200) - void ## setPaperSize(PaperSize size) ### Description Sets the paper size for printing. ### Method void ### Parameters #### Path Parameters - **size** (PaperSize) - Required - The paper size (e.g., Letter, A4). ### Response #### Success Response (200) - void ## setTopMargin(double margin) ### Description Sets top margin in inches. ### Method void ### Parameters #### Path Parameters - **margin** (double) - Required - The top margin in inches. ### Response #### Success Response (200) - void ## setZoom(int percentage) ### Description Sets the zoom/scale percentage. ### Method void ### Parameters #### Path Parameters - **percentage** (int) - Required - The zoom percentage (e.g., 100 for normal). ### Response #### Success Response (200) - void ## isCenterHorizontally(boolean center) ### Description Sets whether sheet is centered horizontally on page. ### Method void ### Parameters #### Path Parameters - **center** (boolean) - Required - True to center horizontally, false otherwise. ### Response #### Success Response (200) - void ## isPrintGridlines(boolean print) ### Description Sets whether gridlines print. ### Method void ### Parameters #### Path Parameters - **print** (boolean) - Required - True to print gridlines, false otherwise. ### Response #### Success Response (200) - void ``` -------------------------------- ### Load and Save Excel File with Macros Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md This example shows how to load an Excel file containing macros, modify a cell, and then save it back in the Excel 97-2003 format. Ensure the macro sample file exists at the specified path. ```java Workbook workbook = new Workbook(); workbook.loadFromFile("data/macroSample.xls"); Worksheet sheet = workbook.getWorksheets().get(0); sheet.getRange().get("A5").setText("This is a simple test!"); String output = "output/loadAndSaveFileWithMacro_result.xls"; workbook.saveToFile(output, ExcelVersion.Version97to2003); workbook.dispose(); ``` -------------------------------- ### Get Cell by Row and Column Indices (getCellRange) Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-worksheet.md Retrieves a single cell using 1-based row and column indices. This method is an alternative to `get(int, int)` for accessing individual cells. ```java CellRange cell = sheet.getCellRange(2, 3); // Cell C2 cell.setValue(42); ``` -------------------------------- ### Create and Configure Tables Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/README.md Explains how to create a ListObject (table) in a worksheet, assign a name, define the data range, apply a built-in style, and configure total row calculations. Ensure data is prepared before table creation. ```java import com.spire.xls.*; // Prepare data sheet.get("A1").setText("Product"); sheet.get("B1").setText("Quantity"); sheet.get("C1").setText("Price"); sheet.get("A2").setText("Widget"); sheet.get("B2").setNumberValue(100); sheet.get("C2").setNumberValue(29.99); // Create table IListObject table = sheet.getListObjects().create( "SalesTable", sheet.get(1, 1, 2, 3) ); // Configure table table.setBuiltInTableStyle(TableBuiltInStyles.TableStyleLight9); table.setDisplayTotalRow(true); table.getColumns().get(1).setTotalsCalculation(ExcelTotalsCalculation.Sum); ``` -------------------------------- ### getText Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-additional.md Gets the text content of a RichText object. ```APIDOC ## getText() ### Description Gets the text content. ### Method String ### Endpoint N/A (Method of RichText object) ``` -------------------------------- ### Create and Initialize Workbook with Sheets Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-workbook.md After creating a new workbook, you can initialize it with a specified number of empty sheets and access them. ```java Workbook workbook = new Workbook(); workbook.createEmptySheets(1); Worksheet sheet = workbook.getWorksheets().get(0); ``` -------------------------------- ### TextBox.getText Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-shapes.md Gets the text content of the text box. ```APIDOC ## TextBox.getText() ### Description Gets the text content of the text box. ### Method ```java String getText() ``` ### Response #### Success Response - **String** - The text content of the text box ### Request Example ```java String content = box.getText(); ``` ``` -------------------------------- ### Workbook() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-workbook.md Creates a new, empty workbook with default settings. This is the primary constructor for initializing a Workbook object. ```APIDOC ## Workbook() ### Description Creates a new, empty workbook with default settings. ### Constructor Workbook() ### Parameters (none) ### Returns A new `Workbook` instance. ### Example ```java Workbook workbook = new Workbook(); workbook.createEmptySheets(1); Worksheet sheet = workbook.getWorksheets().get(0); ``` ``` -------------------------------- ### Get Table Name Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-listobject.md Retrieves the name of the table. ```java String tableName = table.getName(); System.out.println("Table: " + tableName); ``` -------------------------------- ### Various Excel Workbook Opening Methods in Java Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md This example demonstrates multiple ways to open Excel files, including loading from a file path, an input stream, a specific Excel version (97-2003), an XML file, and a CSV file with custom parameters. Ensure the file paths and parameters are correctly set. ```java Workbook workbook = new Workbook(); workbook.loadFromFile(filePath); workbook.dispose(); InputStream stream = new FileInputStream(filePath); Workbook workbookFromStream = new Workbook(); workbookFromStream.loadFromStream(stream); stream.close(); workbookFromStream.dispose(); Workbook workbook97 = new Workbook(); workbook97.loadFromFile(filePath, ExcelVersion.Version97to2003); workbook97.dispose(); Workbook workbookXML = new Workbook(); workbookXML.loadFromXml(xmlFilePath); workbookXML.dispose(); Workbook workbookCSV = new Workbook(); workbookCSV.loadFromFile(csvFilePath, delimiter, startRow, startColumn); workbookCSV.dispose(); ``` -------------------------------- ### getRangeAddress() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellrange.md Gets the address of the cell range in a specific notation. ```APIDOC ## getRangeAddress() ### Description Gets the address in a specific notation. ### Method ```java String getRangeAddress() ``` ### Returns `String` - Range address ### Example ```java String addr = cell.getRangeAddress(); ``` ``` -------------------------------- ### getAddress() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellrange.md Gets the address of the cell range in standard notation. ```APIDOC ## getAddress() ### Description Gets the address of the cell range. ### Method ```java String getAddress() ``` ### Returns `String` - Cell address (e.g., "A1:D10") ### Example ```java String addr = sheet.get("A1:D10").getAddress(); // Returns "A1:D10" ``` ``` -------------------------------- ### Initialize Workbook and Add Conditional Formatting Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Sets up a new workbook, creates a worksheet, and calls a method to add various conditional formatting rules. ```Java public class variousConditionalFormatting { public static void main(String[] args) { // Create a new Workbook object Workbook workbook = new Workbook(); // Create an empty sheet in the workbook workbook.createEmptySheets(1); // Get the first worksheet from the workbook Worksheet sheet = workbook.getWorksheets().get(0); // Add conditional formatting to the new sheet AddConditionalFormattingForNewSheet(sheet); } private static void AddConditionalFormattingForNewSheet(Worksheet sheet) { AddDefaultIconSet(sheet); AddIconSet2(sheet); AddIconSet3(sheet); AddDefaultColorScale(sheet); Add3ColorScale(sheet); Add2ColorScale(sheet); AddEboveEverage(sheet); AddTop10_1(sheet); AddDataBar1(sheet); AddContainsText(sheet); AddTimePeriod_1(sheet); } // This method implements the TimePeriod conditional formatting type with Today attribute. private static void AddTimePeriod_1(Worksheet sheet) { XlsConditionalFormats conds = sheet.getConditionalFormats().add(); conds.addRange(sheet.getCellRange("I1:K2")); sheet.getCellRange("I1:K2").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("I1:K2").getStyle().setColor(Color.gray); IConditionalFormat cf = conds.addTimePeriodCondition(TimePeriodType.Today); cf.setFillPattern(ExcelPatternType.Solid); cf.setBackColor(Color.pink); } // This method implements the ContainsText conditional formatting type. private static void AddContainsText(Worksheet sheet) { XlsConditionalFormats conds = sheet.getConditionalFormats().add(); conds.addRange(sheet.getCellRange("E5:G6")); sheet.getCellRange("E5:G6").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("E5:G6").getStyle().setColor(Color.blue); IConditionalFormat cf = conds.addContainsTextCondition("abc"); cf.setFillPattern(ExcelPatternType.Solid); cf.setBackColor(Color.yellow); } // This method implements the DataBars conditional formatting type. private static void AddDataBar1(Worksheet sheet) { XlsConditionalFormats xcfs = sheet.getConditionalFormats().add(); xcfs.addRange(sheet.getCellRange("E1:G2")); sheet.getCellRange("E1:G2").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("E1:G2").getStyle().setColor(Color.green); IConditionalFormat cf = xcfs.addCondition(); cf.setFormatType(ConditionalFormatType.DataBar); cf.getDataBar().setBarColor(Color.blue); cf.getDataBar().getMinPoint().setType(ConditionValueType.Percent); cf.getDataBar().setShowValue(true); } // This method implements a Top10 conditional formatting type. private static void AddTop10_1(Worksheet sheet) { XlsConditionalFormats conds = sheet.getConditionalFormats().add(); conds.addRange(sheet.getCellRange("A17:C20")); sheet.getCellRange("A17:C20").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("A17:C20").getStyle().setColor(Color.gray); IConditionalFormat cf = conds.addTopBottomCondition(TopBottomType.Top, 10); cf.setFillPattern(ExcelPatternType.Solid); cf.setBackColor(Color.yellow); } // This method implements the AboveAverage conditional formatting type. private static void AddEboveEverage(Worksheet sheet) { XlsConditionalFormats conds = sheet.getConditionalFormats().add(); conds.addRange(sheet.getCellRange("A11:C12")); sheet.getCellRange("A11:C12").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("A11:C12").getStyle().setColor(Color.red); IConditionalFormat cf = conds.addAverageCondition(AverageType.Above); cf.setFillPattern(ExcelPatternType.Solid); cf.setBackColor(Color.pink); } // This method implements the ColorScale conditional formatting type with some color scale attributes. private static void Add2ColorScale(Worksheet sheet) { XlsConditionalFormats xcfs = sheet.getConditionalFormats().add(); xcfs.addRange(sheet.getCellRange("A9:C10")); sheet.getCellRange("A9:C10").getStyle().setFillPattern(ExcelPatternType.Solid); sheet.getCellRange("A9:C10").getStyle().setColor(Color.white); IConditionalFormat cf = xcfs.addCondition(); cf.setFormatType(ConditionalFormatType.ColorScale); cf.getColorScale().setMinColor(Color.yellow); cf.getColorScale().setMaxColor(Color.blue); } // This method implements the ColorScale conditional formatting type with some color scale attributes. ``` -------------------------------- ### getIndentLevel Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellstyle.md Gets the current indentation level of the cell style. ```APIDOC ## getIndentLevel() ### Description Gets the indentation level. ### Method int getIndentLevel() ### Parameters #### Path Parameters - (none) ### Response #### Success Response (200) - **return value** (int) - The indentation level ### Request Example ```java int indent = style.getIndentLevel(); ``` ``` -------------------------------- ### Merge Cells and Apply Formatting Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/README.md Demonstrates merging cells, setting text, applying alignment, and adding borders to a cell range. Also shows how to enable text wrapping for a cell. ```java // Merge cells sheet.merge("A1:D1"); sheet.get("A1").setText("Merged Cell"); // Set cell alignment sheet.get("A1").setHorizontalAlignment(HorizontalAlignmentType.Center); sheet.get("A1").setVerticalAlignment(VerticalAlignmentType.Center); // Add borders sheet.get("A1:D5").borderAround(LineStyleType.Medium); sheet.get("A1:D5").borderInside(LineStyleType.Thin); // Text wrapping sheet.get("A2").setWrapText(true); sheet.get("A2").setText("This is a long text that will wrap to multiple lines in the cell"); ``` -------------------------------- ### Interior Class - getColor Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-additional.md Gets the current fill color of the cells. ```APIDOC ## getColor() ### Description Gets the current fill color. ### Method Color ### Returns Color - The fill color ``` -------------------------------- ### Complete Font Styling Example Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-excelfont.md This snippet shows how to create and apply various font styles like header fonts, emphasis fonts, subscript, and superscript text within cells. It covers setting font name, size, color, style (bold, italic), underline type, and applying rich text formatting. ```java // Create workbook and sheet Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Example 1: Header font ExcelFont headerFont = workbook.createFont(); headerFont.setFontName("Arial"); headerFont.setSize(14); headerFont.isBold(true); headerFont.setColor(Color.white); CellStyle headerStyle = workbook.getStyles().addStyle("header"); headerStyle.setFont(headerFont); headerStyle.getInterior().setColor(Color.darkBlue); sheet.get("A1").setText("Product"); sheet.get("A1").setCellStyleName(headerStyle.getName()); // Example 2: Emphasis font ExcelFont emphasisFont = workbook.createFont(); emphasisFont.setFontName("Calibri"); emphasisFont.setSize(11); emphasisFont.isItalic(true); emphasisFont.setUnderline(UnderlineType.Single); emphasisFont.setColor(Color.red); sheet.get("A2").setText("Important Note"); sheet.get("A2").getCellStyle().setFont(emphasisFont); // Example 3: Subscript in cell text RichText rt = sheet.get("A3").getRichText(); rt.setText("H2O"); ExcelFont subscriptFont = workbook.createFont(); subscriptFont.isSubscript(true); subscriptFont.setSize(8); rt.setFont(2, 3, subscriptFont); // Apply to "2" // Example 4: Superscript in cell text RichText rt2 = sheet.get("A4").getRichText(); rt2.setText("E=mc²"); ExcelFont superscriptFont = workbook.createFont(); superscriptFont.isSuperscript(true); superscriptFont.setSize(8); rt2.setFont(4, 5, superscriptFont); // Apply to "²" workbook.saveToFile("output/font_example.xlsx", ExcelVersion.Version2013); ``` -------------------------------- ### Configure Page Setup for a Worksheet Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/configuration.md Set up page layout options including paper size, orientation, margins, scaling, and print settings like centering and gridlines. ```java PageSetup pageSetup = sheet.getPageSetup(); // Paper size and orientation pageSetup.setPaperSize(PaperSize.A4); pageSetup.setOrientation(PageOrientation.Landscape); // Margins (in inches) pageSetup.setTopMargin(0.5); pageSetup.setBottomMargin(0.5); pageSetup.setLeftMargin(0.75); pageSetup.setRightMargin(0.75); pageSetup.setHeaderMargin(0.3); pageSetup.setFooterMargin(0.3); // Scaling pageSetup.setZoom(100); // 100% scale pageSetup.setZoom(150); // 150% scale // Print settings pageSetup.isCenterHorizontally(true); pageSetup.isCenterVertically(true); pageSetup.isPrintGridlines(true); pageSetup.setPrintQuality(PrintQuality.High); ``` -------------------------------- ### getRotation Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellstyle.md Gets the current text rotation angle of the cell style. ```APIDOC ## getRotation() ### Description Gets the text rotation angle. ### Method int getRotation() ### Parameters #### Path Parameters - (none) ### Response #### Success Response (200) - **return value** (int) - Rotation angle in degrees ### Request Example ```java int rotation = style.getRotation(); ``` ``` -------------------------------- ### Get Style Name Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellstyle.md Retrieves the name assigned to a cell style. ```java String getName() ``` ```java String styleName = style.getName(); System.out.println("Style: " + styleName); ``` -------------------------------- ### Create and Style Slicers from Pivot Table Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Demonstrates creating multiple slicers from a PivotTable, applying different styles, and configuring their cache settings. Includes setting pivot table fields and data aggregation. ```Java PivotTablesCollection pivotTables = worksheet.getPivotTables(); //Add a PivotTable to the worksheet CellRange dataRange = worksheet.getCellRange("A1:C9"); PivotCache cache = wb.getPivotCaches().add(dataRange); //Cell to put the pivot table PivotTable pt = worksheet.getPivotTables().add("TestPivotTable", worksheet.getCellRange("A12"), cache); //Drag the fields to the row area. PivotField pf = (PivotField)pt.getPivotFields().get("fruit"); pf.setAxis(AxisTypes.Row); PivotField pf2 = (PivotField)pt.getPivotFields().get("year"); pf2.setAxis(AxisTypes.Column); //Drag the field to the data area. pt.getDataFields().add(pt.getPivotFields().get("amount"), "SUM of Count", SubtotalTypes.Sum); //Set PivotTable style pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleMedium10); pt.calculateData(); //Get slicer collection XlsSlicerCollection slicers = worksheet.getSlicers(); int index = slicers.add(pt, "E12", 0); XlsSlicer xlsSlicer = slicers.get(index); xlsSlicer.setName("xlsSlicer"); xlsSlicer.setWidth(100); xlsSlicer.setHeight(120); xlsSlicer.setStyleType(SlicerStyleType.SlicerStyleLight2); xlsSlicer.isPositionLocked(true); //Get SlicerCache object of current slicer XlsSlicerCache slicerCache = xlsSlicer.getSlicerCache(); slicerCache.setCrossFilterType(SlicerCacheCrossFilterType.ShowItemsWithNoData); //Style setting XlsSlicerCacheItemCollection slicerCacheItems = xlsSlicer.getSlicerCache().getSlicerCacheItems(); XlsSlicerCacheItem xlsSlicerCacheItem = slicerCacheItems.get(0); xlsSlicerCacheItem.isSelected(false); XlsSlicerCollection slicers_2 = worksheet.getSlicers(); IPivotField r1 = pt.getPivotFields().get("year"); int index_2 = slicers_2.add(pt, "I12", r1); XlsSlicer xlsSlicer_2 = slicers.get(index_2); xlsSlicer_2.setRowHeight(40); xlsSlicer_2.setStyleType(SlicerStyleType.SlicerStyleLight3); xlsSlicer_2.isPositionLocked(false); //Get SlicerCache object of current slicer XlsSlicerCache slicerCache_2 = xlsSlicer_2.getSlicerCache(); slicerCache_2.setCrossFilterType(SlicerCacheCrossFilterType.ShowItemsWithDataAtTop); //Style setting XlsSlicerCacheItemCollection slicerCacheItems_2 = xlsSlicer_2.getSlicerCache().getSlicerCacheItems(); XlsSlicerCacheItem xlsSlicerCacheItem_2 = slicerCacheItems_2.get(1); xlsSlicerCacheItem_2.isSelected(false); pt.calculateData(); ``` -------------------------------- ### ColumnsCollection.getCount() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-row-column.md Gets the total number of columns that contain data in the worksheet. ```APIDOC ## getCount() ### Description Gets the number of columns with data. ### Method ```java int getCount() ``` ### Parameters No parameters ### Returns `int` - Count of columns ### Example ```java int colCount = sheet.getColumns().getCount(); System.out.println("Total columns: " + colCount); ``` ``` -------------------------------- ### Format Cells and Apply Styles Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/README.md Illustrates creating and applying named styles to cells, including custom fonts, background colors, and number formatting for currency. Styles can be reused across cells. ```java import com.spire.xls.*; import java.awt.Color; Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); // Create a named style CellStyle headerStyle = workbook.getStyles().addStyle("header"); ExcelFont font = headerStyle.getFont(); font.setFontName("Arial"); font.setSize(12); font.isBold(true); font.setColor(Color.white); headerStyle.getInterior().setColor(Color.darkBlue); headerStyle.setVerticalAlignment(VerticalAlignmentType.Center); // Apply style to cells sheet.get("A1").setText("Header"); sheet.get("A1").setCellStyleName(headerStyle.getName()); // Number formatting CellStyle currencyStyle = workbook.getStyles().addStyle("currency"); currencyStyle.setNumberFormat("$#,##0.00"); sheet.get("B1").setNumberValue(1234.56); sheet.get("B1").setCellStyleName(currencyStyle.getName()); ``` -------------------------------- ### Apply Number Formatting to Excel Cells Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Demonstrates how to set various number formats, including currency, percentages, and scientific notation, for cells in an Excel worksheet. Also shows how to apply bold formatting and auto-fit column widths. ```java // Set text and formatting for cell B10 sheet.getCellRange("B10").setText("NUMBER FORMATTING"); sheet.getCellRange("B10").getCellStyle().getExcelFont().isBold(true); // Set text, value, and number format for cell B13 sheet.getCellRange("B13").setText("0"); sheet.getCellRange("C13").setNumberValue(1234.5678); sheet.getCellRange("C13").setNumberFormat("0"); // Set text, value, and number format for cell B14 sheet.getCellRange("B14").setText("0.00"); sheet.getCellRange("C14").setNumberValue(1234.5678); sheet.getCellRange("C14").setNumberFormat("0.00"); // Set text, value, and number format for cell B15 sheet.getCellRange("B15").setText("#,##0.00"); sheet.getCellRange("C15").setNumberValue(1234.5678); sheet.getCellRange("C15").setNumberFormat("#,##0.00"); // Set text, value, and number format for cell B16 sheet.getCellRange("B16").setText("$"#,##0.00"); sheet.getCellRange("C16").setNumberValue(1234.5678); sheet.getCellRange("C16").setNumberFormat("$"#,##0.00"); // Set text, value, and number format for cell B17 sheet.getCellRange("B17").setText("0;[Red]-0"); sheet.getCellRange("C17").setNumberValue(-1234.5678); sheet.getCellRange("C17").setNumberFormat("0;[Red]-0"); // Set text, value, and number format for cell B18 sheet.getCellRange("B18").setText("0.00;[Red]-0.00"); sheet.getCellRange("C18").setNumberValue(-1234.5678); sheet.getCellRange("C18").setNumberFormat("0.00;[Red]-0.00"); // Set text, value, and number format for cell B19 sheet.getCellRange("B19").setText("#,##0;[Red]-#,##0"); sheet.getCellRange("C19").setNumberValue(-1234.5678); sheet.getCellRange("C19").setNumberFormat("#,##0;[Red]-#,##0"); // Set text, value, and number format for cell B20 sheet.getCellRange("B20").setText("#,##0.00;[Red]-#,##0.000"); sheet.getCellRange("C20").setNumberValue(-1234.5678); sheet.getCellRange("C20").setNumberFormat("#,##0.00;[Red]-#,##0.00"); // Set text, value, and number format for cell B21 sheet.getCellRange("B21").setText("0.00E+00"); sheet.getCellRange("C21").setNumberValue(1234.5678); sheet.getCellRange("C21").setNumberFormat("0.00E+00"); // Set text, value, and number format for cell B22 sheet.getCellRange("B22").setText("0.00%"); sheet.getCellRange("C22").setNumberValue(1234.5678); sheet.getCellRange("C22").setNumberFormat("0.00%"); // Apply known color Gray25Percent to cells B13:B22 sheet.getCellRange("B13:B22").getCellStyle().setKnownColor(ExcelColors.Gray25Percent); // Auto-fit the width of columns 2 and 3 sheet.autoFitColumn(2); sheet.autoFitColumn(3); ``` -------------------------------- ### getCount() - Rows Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-row-column.md Gets the number of rows that contain data in the worksheet. ```APIDOC ## getCount() ### Description Gets the number of rows with data. ### Method ```java int getCount() ``` ### Parameters No parameters ### Returns `int` - Count of rows ### Example ```java int rowCount = sheet.getRows().getCount(); System.out.println("Total rows: " + rowCount); ``` ``` -------------------------------- ### Implement Various Excel Formulas Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/xls_java.md Provides examples of implementing a wide range of Excel formulas, including mathematical, bitwise, date, and URL encoding functions, and then calculates all formulas in the workbook. ```Java Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); sheet.getColumns()[0].setNumberFormat("@"); sheet.getCellRange("B1").setFormula("=CEILING.MATH(-2.78, 5, -1)"); sheet.getCellRange("B2").setFormula("=BITOR(23,10)"); sheet.getCellRange("B3").setFormula("=BITAND(23,10)"); sheet.getCellRange("B4").setFormula("=BITLSHIFT(23,2)"); sheet.getCellRange("B5").setFormula("=BITRSHIFT(23,2)"); sheet.getCellRange("B6").setFormula("=FLOOR.MATH(12.758, 2, -1)"); sheet.getCellRange("B7").setFormula("=ISOWEEKNUM(DATE(2012, 1, 1))"); sheet.getCellRange("B8").setFormula("=CEILING.PRECISE(-4.6, 3)"); sheet.getCellRange("B9").setFormula("=ENCODEURL(\"https://www.e-iceblue.com\")"); workbook.calculateAllValue(); sheet.getAllocatedRange().autoFitColumns(); ``` -------------------------------- ### getFormulaValue() Source: https://github.com/eiceblue/spire.xls-for-java/blob/main/_autodocs/api-reference-cellrange.md Gets the calculated value of a formula. This returns the result after the formula has been evaluated. ```APIDOC ## getFormulaValue() ### Description Gets the calculated value of a formula. ### Method String getFormulaValue() ### Returns `String` - The calculated formula result ### Request Example ```java String result = sheet.get("A3").getFormulaValue(); ``` ```