CLJ Excel framework in the Java library in the Java library
The CLJ Excel framework is a Java class library for processing Excel data and provides rich data processing technology.It is built based on open source Apache Poi projects, which aims to simplify the complexity of Excel data processing.This article will introduce the data processing technical principles of the CLJ Excel framework in the Java class library, and provide the corresponding Java code example.
1. Reading and writing of Excel data
The CLJ Excel framework implements the reading and writing of the excel file through the API provided by Apache Poi.The following is an example code that reads the content of the Excel file and printed:
import org.apache.poi.ss.usermodel.*;
public class ExcelProcessor {
public static void main(String[] args) {
try {
Workbook workbook = WorkbookFactory.create(new File("path/to/excel/file.xlsx"));
Sheet sheet = workbook.getsheetat (0); // Select the first worksheet
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.println(cell.getStringCellValue());
break;
case NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
// Treatment of other data types ...
}
}
}
workbook.close();
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}
}
}
The above code creates a Workbook object through WorkbookFactory and selects the first worksheet.Then, the nested cycle traverses each cell and processes different processing according to its data type.In this way, we can read and process the data in the Excel file.
For writing Excel data, it is also implemented through the Workbook object.Below is a sample code written to the Excel file:
import org.apache.poi.ss.usermodel.*;
public class ExcelProcessor {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet (); // Create a worksheet
// data input
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, world!");
// Save the excel file
try (FileOutputStream fileOutputStream = new FileOutputStream("path/to/excel/file.xlsx")) {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The above code creates a new Workbook object and worksheet, and a string was written in the first cell.Then, the Workbook object is written into the specified Excel file via FileoutPutStream.
2. Data processing technology
In addition to the basic Excel data reading and writing, the CLJ Excel framework also provides some advanced data processing technologies to more conveniently operate Excel data.
1. Style settings
The CLJ Excel framework can set various styles of cells, such as fonts, colors, borders, background color, etc.The following is an example code that shows how to set the font style of the cell:
import org.apache.poi.ss.usermodel.*;
public class ExcelProcessor {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, world!");
// Set font style
Font font = workbook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 12);
font.setBold(true);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
// Save the excel file ...
}
}
The above code creates a font object and cell style object, and is applied to the cell.By setting the attributes of the font, such as the font name, the size of the font, and the thick body, the font style of the cell can be customized.
2. Formula calculation
The CLJ Excel framework supports calculation of the formulas in Excel.The following is an example code that shows how to calculate the EXCEL table data containing the formula:
import org.apache.poi.ss.usermodel.*;
public class ExcelProcessor {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row1 = sheet.createrow (0); // The first line
Cell cell1 = row1.createCell(0);
cell1.setCellValue(10);
Row row2 = sheet.createrow (1); // The second line
Cell cell2 = row2.createCell(0);
cell2.setCellValue(5);
Row row3 = sheet.createrow (2); // third line
Cell cell3 = row3.createCell(0);
cell3.setcellformula ("Sum (a1: A2)"); // Set formula
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Evaluator.evaluateFormulacell (cell3); // Calculating formula
// Save the excel file ...
}
}
The above code creates an Excel form containing formulas.By setting up the formula of the cell and using FormulaEvaluator to calculate the formula, we can get the correct result.
3. Summary
The CLJ Excel framework is a powerful Java class library for processing Excel data.This article introduces its data processing technical principles in the Java library and provides corresponding Java code examples.By reading and writing, style settings, and formula calculation of Excel files, we can handle Excel data more flexibly.I hope this article can help you better understand the application of the CLJ Excel framework in data processing.