JAVA: POI sets EXCEL cell format

Table of contents

1. Maven introduction

2. Cell style setting

 3. Cell value setting

3.1. Set the cell to text format

3.2. Set the cell to date format

3.3. Set the cell value format

3.4. Format cells as currency

3.5. Set the cell to percentage format

3.6. Set the cell to Chinese uppercase format

3.7. Set the cell format as scientific notation


This article will introduce the basic format setting usage of POI Excel for Java, including: cell style setting, value setting (text, decimal, percentage, currency, date, scientific notation and Chinese capitalization, etc.).

1. Maven introduction

<poi.version>3.14</poi.version>
 
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>

2. Cell style setting

Use Aspose Excel for Java to easily set the style in the Excel file. Here is a simple sample code for setting cell styles:

CellStyle cellStyle=wb.createCellStyle(); // Create a cell style
cellStyle.setAlignment(HorizontalAlignment.LEFT); // Set the horizontal alignment of the cell
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // Set the vertical alignment of the cell its way

cellStyle.setFillForegroundColor(IndexedColors.BROWN.getIndex());//Set the background color cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); //Set the foreground color

cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // bottom border cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // bottom border color

cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // left border

cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // left border color

cellStyle.setBorderRight(CellStyle.BORDER_THIN); // right border

cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // right border color

cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // top border cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // top border color

//Set font
Font font = wb.createFont();

font.setFontName("Black body");

font.setFontHeightInPoints((short) 16);//Set font size

Font font2 = wb.createFont();

font2.setFontName("Imitation Song_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//Bold display font2.setFontHeightInPoints((short) 12);

cellStyle.setFont(font);//Select the font format to be used

cell.setCellStyle(cellStyle); // set cell style  

 3. Cell value setting

3.1. Set the cell to text format

 CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("@"));//text format

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.2. Set the cell to date format

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("yyyy-MM-dd"));/ /date format

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.3. Set the cell value format

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("0"));//data format only Display integer "_ "
//cellStyle.setDataFormat(df.getFormat("0.00"));//Retain two decimal points

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.4. Format cells as currency

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here
DataFormat df = workbook.createDataFormat();

cellStyle.setDataFormat(df.getFormat("¥#,##0"));//Set currency format

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.5. Set the cell to percentage format

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here
DataFormat df = workbook.createDataFormat();

cellStyle.setDataFormat(df.getFormat("0.00%"));//% keep two decimal points

cell.setCellStyle(cellStyle);

// Set the cell content to double type, the value needs to be converted and calculated

 cell.setCellValue(Double.parseDouble(data.toString())/100d);

3.6. Set the cell to Chinese uppercase format

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here

DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));//Set Chinese uppercase cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.7. Set the cell format as scientific notation

CellStyle cellStyle=wb.createCellStyle(); // create cell style    
// set data format here

DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("0.00E+00"));//Set scientific notation cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

Guess you like

Origin blog.csdn.net/u012998680/article/details/131480487