
Program Guide (80)
Children categories
Java remove the formulas but keep the values on Excel worksheet
Written by support iceblueThis article will demonstrate how to use Spire.XLS for Java to remove the formulas but keep the values on the Excel worksheet.
Firstly, view the original Excel:
import com.spire.xls.*; public class Test { public static void main(String[] args) throws Exception { String inputFile = "Sample.xlsx"; String outputFile="output/removeFormulasButKeepValues_result.xlsx"; //Create a workbook. Workbook workbook = new Workbook(); //Load the file from disk. workbook.loadFromFile(inputFile); //Loop through worksheets. for (Worksheet sheet : (Iterable) workbook.getWorksheets()) { //Loop through cells. for (CellRange cell : (Iterable) sheet.getRange()) { //If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell. if (cell.hasFormula()) { Object value = cell.getFormulaValue(); cell.clear(ExcelClearOptions.ClearContent); cell.setValue(value.toString()); } } } //Save to file workbook.saveToFile(outputFile, ExcelVersion.Version2013); } }
Output:
Split a Workbook into Separate Excel Files in Java
Written by support iceblueThis article demonstrates how to split a workbook into multiple Excel files (each containing one worksheet) by using Spire.XLS for Java.
import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class SplitWorkbook { public static void main(String[] args) { //Create a Workbook object Workbook wb = new Workbook(); //Load an Excel document wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx"); //Declare a Workbook variable Workbook newWb; //Declare a String variable String sheetName; //Specify the folder path, which is used to store the generated Excel files String folderPath = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Loop through the worksheets in the source file for (int i = 0; i < wb.getWorksheets().getCount(); i++) { //Initialize the Workbook object newWb = new Workbook(); //Remove the default sheets newWb.getWorksheets().clear(); //Add the the specific worksheet of the source document to the new workbook newWb.getWorksheets().addCopy(wb.getWorksheets().get(i)); //Get the worksheet name sheetName = wb.getWorksheets().get(i).getName(); //Save the new workbook to the specified folder newWb.saveToFile(folderPath + sheetName + ".xlsx", FileFormat.Version2013); } } }
Split a Worksheet into Several Excel Files in Java
Written by support iceblueThis article demonstrates how to split a worksheet into several Excel documents by using Spire.XLS for Java.
import com.spire.xls.CellRange; import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class SplitWorksheet { public static void main(String[] args) { //Create a Workbook object to load the original Excel document Workbook bookOriginal = new Workbook(); bookOriginal.loadFromFile("C:\\Users\\Administrator\\Desktop\\Emplyees.xlsx"); //Get the first worksheet Worksheet sheet = bookOriginal.getWorksheets().get(0); //Get the header row CellRange headerRow = sheet.getCellRange(1, 1, 1, 5); //Get two cell ranges CellRange range1 = sheet.getCellRange(2, 1, 6, 5); CellRange range2 = sheet.getCellRange(7, 1, 11, 5); //Create a new workbook Workbook newBook1 = new Workbook(); //Copy the header row and range 1 to the new workbook sheet.copy(headerRow, newBook1.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range1, newBook1.getWorksheets().get(0), 2, 1, true, false); //Copy the column width from the original workbook to the new workbook for (int i = 0; i < sheet.getLastColumn(); i++) { newBook1.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } //Save the new workbook to an Excel file newBook1.saveToFile("Sales.xlsx", ExcelVersion.Version2016); //Copy the header row and range 2 to another workbook, and save it to another Excel file Workbook newBook2 = new Workbook(); sheet.copy(headerRow, newBook2.getWorksheets().get(0), 1, 1, true, false); sheet.copy(range2, newBook2.getWorksheets().get(0), 2, 1, true, false); for (int i = 0; i < sheet.getLastColumn(); i++) { newBook2.getWorksheets().get(0).setColumnWidth(i + 1, sheet.getColumnWidth(i + 1)); } newBook2.saveToFile("Technicians.xlsx", ExcelVersion.Version2016); } }
Add Trendline to Chart and Read Trendline Equation in Excel in Java
Written by support iceblueThis article demonstrates how to add Trendline to an Excel chart and read the equation of the Trendline using Spire.XLS for Java.
Add Trendline
import com.spire.xls.*; import com.spire.xls.core.IChartTrendLine; import java.awt.*; public class AddTrendline { public static void main(String[] args){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load the Excel file workbook.loadFromFile("test.xlsx"); //Get the first chart in the first worksheet Chart chart = workbook.getWorksheets().get(0).getCharts().get(0); //Add a Trendline to the first series of the chart IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Linear); //Set Trendline name trendLine.setName("Linear(Series1)"); //Set line type and color trendLine.getBorder().setPattern(ChartLinePatternType.DashDot); trendLine.getBorder().setColor(Color.blue); //Set forward and backward value trendLine.setForward(0.5); trendLine.setBackward(0.5); //Set intercept value trendLine.setIntercept(5); //Display equation on chart trendLine.setDisplayEquation(true); //Display R-Squared value on chart trendLine.setDisplayRSquared(true); //Save the result file workbook.saveToFile("AddTrendline.xlsx", ExcelVersion.Version2013); } }
Read Trendline equation
import com.spire.xls.Chart; import com.spire.xls.Workbook; import com.spire.xls.core.IChartTrendLine; public class ReadEquationOfTrendline { public static void main(String[] args){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load the Excel file workbook.loadFromFile("AddTrendline.xlsx"); //Get the first chart in the first worksheet Chart chart = workbook.getWorksheets().get(0).getCharts().get(0); //Read the equation of the first series of the chart IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().get(0); String equation = trendLine.getFormula(); System.out.println("The equation is: " + equation); } }
Accept/reject the tracked changes on Excel workbook in C#/VB.NET
Written by support iceblueThis article will show you how to accept/reject the tracked changes on Excel workbook in C#/VB.NET with the help of Spire.XLS.
using Spire.Xls; namespace TrackChanges_XLS { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); if (workbook.HasTrackedChanges) { workbook.AcceptAllTrackedChanges(); //workbook.RejectAllTrackedChanges(); } workbook.SaveToFile("Result.xlsx", FileFormat.Version2013); } } }
Imports Spire.Xls Namespace TrackChanges_XLS Class Program Private Shared Sub Main(ByVal args() As String) Dim workbook As Workbook = New Workbook workbook.LoadFromFile("Sample.xlsx") If workbook.HasTrackedChanges Then workbook.AcceptAllTrackedChanges 'workbook.RejectAllTrackedChanges(); End If workbook.SaveToFile("Result.xlsx", FileFormat.Version2013) End Sub End Class End Namespace
Set Font and Background Color for Excel Textbox in Java
Written by support iceblueThis tutorial will demonstrate how to set font and background color for Excel textbox using Spire.XLS for Java.
The following is a screenshot of a sample document:
Using the code
import java.awt.*; import com.spire.xls.*; import com.spire.xls.core.spreadsheet.shapes.*; public class SetFontAndBackground { public static void main(String[] args) { //Load an Excel sample Workbook workbook = new Workbook(); workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Get the textbox which will be edited XlsTextBoxShape shape = (XlsTextBoxShape) sheet.getTextBoxes().get(0); //Set the font and background color for the textbox //Set font ExcelFont font = workbook.createFont(); font.setFontName("Calibri"); font.setSize(14); font.isBold(false); font.setColor(Color.MAGENTA); (new RichText(shape.getRichText())).setFont(0, shape.getText().length() - 1, font); //Set background color shape.getFill().setFillType(ShapeFillType.SolidColor); shape.getFill().setForeKnownColor(ExcelColors.Yellow); //Save to file workbook.saveToFile("output/setFontAndBackgroundForTextBox.xlsx", ExcelVersion.Version2013); } }
Output
This article demonstrates how to set Excel page margins before printing the Excel worksheets in Java applications. By using Spire.XLS for Java, we could set top margin, bottom margin, left margin, right margin, header margin, and footer margin. Please note that the unit for margin is inch on Spire.XLS for Java while On Microsoft Excel, it is cm (1 inch=2.54 cm).
import com.spire.xls.*; public class setMargins { public static void main(String[] args) { String outputFile="output/setMarginsOfExcel.xlsx"; //Load the sample document from file Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx";); //Get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //Get the PageSetup object of the first worksheet. PageSetup pageSetup = sheet.getPageSetup(); //Set the page margins of bottom, left, right and top. pageSetup.setBottomMargin(2); pageSetup.setLeftMargin(1); pageSetup.setRightMargin(1); pageSetup.setTopMargin(3); //Set the margins of header and footer. pageSetup.setHeaderMarginInch(2); pageSetup.setFooterMarginInch(2); //Save to file. workbook.saveToFile(outputFile, ExcelVersion.Version2013); } }
Output:
This article demonstrates how to create a scatter chart and add a trendline to it in an Excel document by using Spire.XLS for Java.
import com.spire.xls.*; import com.spire.xls.core.IChartTrendLine; import java.awt.*; public class ScatterChart { public static void main(String[] args) { //Create a a Workbook object and get the first worksheet Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); //Rename the first worksheet and set the column width sheet.getCellRange("A1:B1").setColumnWidth(22f);; sheet.setName("Scatter Chart"); //Insert data sheet.getCellRange("A1").setValue("Advertising Expenditure"); sheet.getCellRange("A2").setValue("10429"); sheet.getCellRange("A3").setValue("95365"); sheet.getCellRange("A4").setValue("24085"); sheet.getCellRange("A5").setValue("109154"); sheet.getCellRange("A6").setValue("34006"); sheet.getCellRange("A7").setValue("84687"); sheet.getCellRange("A8").setValue("17560"); sheet.getCellRange("A9").setValue ("61408"); sheet.getCellRange("A10").setValue ("29402"); sheet.getCellRange("B1").setValue("Sales Revenue"); sheet.getCellRange("B2").setValue ("42519"); sheet.getCellRange("B3").setValue("184357"); sheet.getCellRange("B4").setValue ("38491"); sheet.getCellRange("B5").setValue ("214956"); sheet.getCellRange("B6").setValue ("75469"); sheet.getCellRange("B7").setValue ("134735"); sheet.getCellRange("B8").setValue("47935"); sheet.getCellRange("B9").setValue ("151832"); sheet.getCellRange("B10").setValue ("65424"); //Set cell style sheet.getCellRange("A1:B1").getStyle().getFont().isBold(true); sheet.getCellRange("A1:B1").getStyle().setColor(Color.darkGray); sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white); sheet.getCellRange("A1:B10").getStyle().setHorizontalAlignment(HorizontalAlignType.Center); sheet.getCellRange("A2:B10").getCellStyle().setNumberFormat("\"$\"#,##0") ; //Create a scatter chart and set its data range Chart chart = sheet.getCharts().add(ExcelChartType.ScatterMarkers); chart.setDataRange(sheet.getCellRange("B2:B10")); chart.setSeriesDataFromRange(false); //Set position of the chart. chart.setLeftColumn(4); chart.setTopRow(1); chart.setRightColumn(13); chart.setBottomRow(22); //Set chart title and series data label chart.setChartTitle("Advertising & Sales Relationship"); chart.getChartTitleArea().isBold(true); chart.getChartTitleArea().setSize(12); chart.getSeries().get(0).setCategoryLabels(sheet.getCellRange("B2:B10")); chart.getSeries().get(0).setValues(sheet.getCellRange("A2:A10")); //Add a trendline IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Exponential); trendLine.setName("Trendline"); //Set title of the x and y axis chart.getPrimaryValueAxis().setTitle("Advertising Expenditure ($)"); chart.getPrimaryCategoryAxis().setTitle("Sales Revenue ($)"); //Save the document workbook.saveToFile("ScatterChart.xlsx",ExcelVersion.Version2010); workbook.dispose(); } }
Java split one cell contents into multiple columns in Excel
Written by support iceblueThis article demonstrates how to use Spire.XLS for Java to split Excel text or numbers in one cell into multiple columns by delimiters. The delimiter characters could be Space ( ), Comma (,) Semicolon(;) etc.
import com.spire.xls.*; public class splitDataIntoMultipleColumns { public static void main(String[] args) { //Load the sample document from file Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //Split data into separate columns by the delimiter characters of space. String[] splitText = null; String text = null; for (int i = 1; i < sheet.getLastRow(); i++) { text = sheet.getRange().get(i + 1, 1).getText(); splitText = text.split(" "); for (int j = 0; j < splitText.length; j++) { sheet.getRange().get(i + 1, 1 + j + 1).setText(splitText[j]); } } //Save to file workbook.saveToFile("Result.xlsx", ExcelVersion.Version2013); } }
Output:
This article demonstrates how to create pivot chart in an Excel file in Java using Spire.XLS for Java.
The input Excel file:
import com.spire.xls.*; import com.spire.xls.core.IPivotTable; public class CreatePivotChart { public static void main(String[] args) { //Load the Excel file Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //get the first pivot table in the worksheet IPivotTable pivotTable = sheet.getPivotTables().get(0); //Add a clustered column chart based on the pivot table data to the second worksheet Chart chart = workbook.getWorksheets().get(1).getCharts().add(ExcelChartType.ColumnClustered, pivotTable); //Set chart position chart.setTopRow(2); chart.setBottomRow(15); //Set chart title chart.setChartTitle("Total"); //Save the result file workbook.saveToFile("CreatPivotChart.xlsx", ExcelVersion.Version2013); } }
Output: