JAVA?POI設(shè)置EXCEL單元格格式用法舉例
前言
本文將介紹POI Excel for Java的格式設(shè)置基本用法,包括:?jiǎn)卧駱邮皆O(shè)置、值設(shè)置(文本、小數(shù)、百分比、貨幣、日期、科學(xué)計(jì)數(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.單元格樣式設(shè)置
使用Aspose Excel for Java可以方便地設(shè)置Excel文件中的樣式。下面是一個(gè)簡(jiǎn)單的設(shè)置單元格樣式的示例代碼:
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式
cellStyle.setAlignment(HorizontalAlignment.LEFT); ?// 設(shè)置單元格水平方向?qū)ζ浞绞?
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 設(shè)置單元格垂直方向?qū)ζ浞绞?
cellStyle.setFillForegroundColor(IndexedColors.BROWN.getIndex());//設(shè)置背景顏色cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // 設(shè)置前景顏色
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()); // 上邊邊框顏色
//設(shè)置字體
Font font = wb.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設(shè)置字體大小
Font font2 = wb.createFont();
font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示 font2.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);//選擇需要用到的字體格式
cell.setCellStyle(cellStyle); // 設(shè)置單元格樣式 ?3.單元格值設(shè)置
3.1.設(shè)置單元格為文本格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("@"));//文本格式
cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());3.2.設(shè)置單元格為日期格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("yyyy-MM-dd"));//日期格式
cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());3.3.設(shè)置單元格數(shù)值格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("0"));//數(shù)據(jù)格式只顯示整數(shù)"_ "
//cellStyle.setDataFormat(df.getFormat("0.00"));//保留兩位小數(shù)點(diǎn)
cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());3.4.設(shè)置單元格為貨幣格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("¥#,##0"));//設(shè)置貨幣格式
cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());3.5.設(shè)置單元格為百分比格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("0.00%"));//%保留兩位小數(shù)點(diǎn)
cell.setCellStyle(cellStyle);
// 設(shè)置單元格內(nèi)容為double類型,數(shù)值需要進(jìn)行轉(zhuǎn)換計(jì)算
?cell.setCellValue(Double.parseDouble(data.toString())/100d);3.6.設(shè)置單元格為中文大寫格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));//設(shè)置中文大寫 cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());3.7.設(shè)置單元格為科學(xué)計(jì)數(shù)法格式
CellStyle cellStyle=wb.createCellStyle(); // 創(chuàng)建單元格樣式 ? ?
// 此處設(shè)置數(shù)據(jù)格式
DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("0.00E+00"));//設(shè)置科學(xué)計(jì)數(shù)法 cell.setCellStyle(cellStyle);
cell.setCellValue(data.toString());總結(jié)
到此這篇關(guān)于JAVA POI設(shè)置EXCEL單元格格式的文章就介紹到這了,更多相關(guān)POI設(shè)置EXCEL單元格格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot 使用 Curator 操作 ZooKeeper
本文主要介紹使用 Curator 訪問 ZooKeeper 的一些基本方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
Java實(shí)現(xiàn)的對(duì)稱加密算法AES定義與用法詳解
這篇文章主要介紹了Java實(shí)現(xiàn)的對(duì)稱加密算法AES,結(jié)合實(shí)例形式分析了對(duì)稱加密算法AES的定義、特點(diǎn)、用法及使用場(chǎng)景,需要的朋友可以參考下2018-04-04
基于springboot微信公眾號(hào)開發(fā)(微信自動(dòng)回復(fù))
這篇文章主要介紹了基于springboot微信公眾號(hào)開發(fā)(微信自動(dòng)回復(fù)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
SpringMVC數(shù)據(jù)頁(yè)響應(yīng)ModelAndView實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
本文主要介紹了SpringMVC數(shù)據(jù)頁(yè)響應(yīng)ModelAndView實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring WebFlux實(shí)現(xiàn)參數(shù)校驗(yàn)的示例代碼
請(qǐng)求參數(shù)校驗(yàn),在實(shí)際的應(yīng)用中很常見,網(wǎng)上的文章大部分提供的使用注解的方式做參數(shù)校驗(yàn)。本文主要介紹 Spring Webflux Function Endpoint 使用 Spring Validation 來(lái)校驗(yàn)請(qǐng)求的參數(shù)。感興趣的可以了解一下2021-08-08
Java使用itext5實(shí)現(xiàn)PDF表格文檔導(dǎo)出
這篇文章主要介紹了Java使用itext5實(shí)現(xiàn)PDF表格文檔導(dǎo)出,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
這篇文章主要介紹了java并發(fā)之ArrayBlockingQueue詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05
Mybatis一對(duì)多查詢的兩種姿勢(shì)(值得收藏)
這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多查詢的兩種姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

