Data formatting and style management in Adams Excel: outside of customized Excel
Data formatting and style management in Adams Excel: outside of customized Excel
Introduction:
Microsoft Excel is a very powerful and flexible tool in data processing and report generation. Excel has rich functions and powerful data processing capabilities, which can meet various data analysis and reporting needs. However, Excel's default styles and formats may not meet users' personalized needs. In Adams Excel, we provide a powerful set of tools and functionalities that enable customization of data formatting and style management, enabling users to create unique and attractive Excel spreadsheets according to their needs.
1、 Formatting data
1. Number formatting
In Excel, data formatting is crucial for data visualization and understanding. Through Adams Excel, users can flexibly format the data in the table, making it more intuitive and readable. Here is an example of how to format numbers in Excel using Java code:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DataFormattingExample {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("DataFormattingExample");
CellStyle numericStyle = workbook.createCellStyle();
DataFormat dataFormat = workbook.createDataFormat();
numericStyle.setDataFormat(dataFormat.getFormat("#,##0.00"));
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(12345.6789);
cell.setCellStyle(numericStyle);
try (FileOutputStream fileOutputStream = new FileOutputStream("DataFormattingExample.xlsx")) {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The above code creates a new workbook, creates a worksheet called 'DataFormattingExample', and sets the number format of the cells to '#, # # 0.00'. Then, a numerical value of 12345.6789 is filled in the cells and the style is applied to the cells. Finally, write the workbook to a file named 'DataFormattingExample. xlsx' through 'FileOutputStream'.
2. Date formatting
In addition to number formatting, date formatting is also one of the frequently used functions in Excel. Here is an example of how to format dates in Excel using Java code:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormattingExample {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("DateFormattingExample");
CellStyle dateStyle = workbook.createCellStyle();
CreationHelper creationHelper = workbook.getCreationHelper();
dateStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd"));
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
LocalDate currentDate = LocalDate.now();
cell.setCellValue(currentDate);
cell.setCellStyle(dateStyle);
try (FileOutputStream fileOutputStream = new FileOutputStream("DateFormattingExample.xlsx")) {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The above code creates a new workbook, creates a worksheet called 'DateFormattingExample', and sets the date format of the cells to 'yyyy MM dd'. Then fill in the current date into the cell and apply the style to the cell. Finally, write the workbook to a file named "DateFormattingExample. xlsx" using 'FileOutputStream'.
2、 Style Management
In addition to data formatting, style management in Excel is also very important. Styles can include fonts, background colors, borders, etc., which can help users better visualize table data. Through Adams Excel, users can easily manage styles through Java code. Here is an example of how to use Java code to manage styles in Excel:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class StyleManagementExample {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("StyleManagementExample");
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 12);
headerStyle.setFont(headerFont);
Row headerRow = sheet.createRow(0);
Cell headerCell = headerRow.createCell(0);
headerCell.setCellValue("Header");
headerCell.setCellStyle(headerStyle);
try (FileOutputStream fileOutputStream = new FileOutputStream("StyleManagementExample.xlsx")) {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The above code creates a new workbook, creates a worksheet called 'StyleManagementExample', and sets the style of the header cells. Styles include bold font and 12 point font size. Then set the header text into the cell and apply the style to the cell. Finally, use 'FileOutputStream' to write the workbook to a file named 'StyleManagementExample. xlsx'.
Conclusion:
In Adams Excel, we provide support for data formatting and style management, allowing users to easily customize the appearance and style of Excel tables. Whether by formatting numbers, dates, or managing styles such as fonts and background colors, users can create unique and attractive Excel spreadsheets according to their own needs. Through the above Java code example, you can better understand the functions provided by Adams Excel and how to use them. I hope this article is helpful to you!