JAVA?POI設置EXCEL單元格格式用法舉例
更新時間:2023年08月24日 08:54:31 作者:風蕭蕭1999
這篇文章主要給大家介紹了關于JAVA?POI設置EXCEL單元格格式用法的相關資料,POI中可能會用到一些需要設置EXCEL單元格格式的操作,需要的朋友可以參考下
前言
本文將介紹POI Excel for Java的格式設置基本用法,包括:單元格樣式設置、值設置(文本、小數(shù)、百分比、貨幣、日期、科學計數(shù)法和中文大寫等)。
1.Maven引入
<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.單元格樣式設置
使用Aspose Excel for Java可以方便地設置Excel文件中的樣式。下面是一個簡單的設置單元格樣式的示例代碼:
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 cellStyle.setAlignment(HorizontalAlignment.LEFT); ?// 設置單元格水平方向對其方式 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 設置單元格垂直方向對其方式 cellStyle.setFillForegroundColor(IndexedColors.BROWN.getIndex());//設置背景顏色cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // 設置前景顏色 cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部邊框? cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部邊框顏色 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左邊邊框 cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左邊邊框顏色 cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右邊邊框 cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右邊邊框顏色 cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上邊邊框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上邊邊框顏色 //設置字體 Font font = wb.createFont(); font.setFontName("黑體"); font.setFontHeightInPoints((short) 16);//設置字體大小 Font font2 = wb.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示 font2.setFontHeightInPoints((short) 12); cellStyle.setFont(font);//選擇需要用到的字體格式 cell.setCellStyle(cellStyle); // 設置單元格樣式 ?
3.單元格值設置
3.1.設置單元格為文本格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat df = workbook.createDataFormat(); cellStyle.setDataFormat(df.getFormat("@"));//文本格式 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
3.2.設置單元格為日期格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat df = workbook.createDataFormat(); cellStyle.setDataFormat(df.getFormat("yyyy-MM-dd"));//日期格式 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
3.3.設置單元格數(shù)值格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat df = workbook.createDataFormat(); cellStyle.setDataFormat(df.getFormat("0"));//數(shù)據(jù)格式只顯示整數(shù)"_ " //cellStyle.setDataFormat(df.getFormat("0.00"));//保留兩位小數(shù)點 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
3.4.設置單元格為貨幣格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat df = workbook.createDataFormat(); cellStyle.setDataFormat(df.getFormat("¥#,##0"));//設置貨幣格式 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
3.5.設置單元格為百分比格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat df = workbook.createDataFormat(); cellStyle.setDataFormat(df.getFormat("0.00%"));//%保留兩位小數(shù)點 cell.setCellStyle(cellStyle); // 設置單元格內容為double類型,數(shù)值需要進行轉換計算 ?cell.setCellValue(Double.parseDouble(data.toString())/100d);
3.6.設置單元格為中文大寫格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));//設置中文大寫 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
3.7.設置單元格為科學計數(shù)法格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ? // 此處設置數(shù)據(jù)格式 DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("0.00E+00"));//設置科學計數(shù)法 cell.setCellStyle(cellStyle); cell.setCellValue(data.toString());
總結
到此這篇關于JAVA POI設置EXCEL單元格格式的文章就介紹到這了,更多相關POI設置EXCEL單元格格式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot feign動態(tài)設置數(shù)據(jù)源(https請求)
這篇文章主要介紹了SpringBoot如何在運行時feign動態(tài)添加數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-08-08springboot oauth2實現(xiàn)單點登錄實例
我們見過的很多網(wǎng)站,容許使用第三方賬號登錄,oauth2是用來做三方登錄的,本文就詳細的介紹springboot oauth2實現(xiàn)單點登錄實例,具有一定的參考價值,感興趣的可以了解一下2022-01-01