POI通過模板導(dǎo)出EXCEL文件的實(shí)例
一般的EXCEL導(dǎo)出使用POI先創(chuàng)建一個(gè)HSSFWorkbook,然后通過不斷創(chuàng)建HSSFRow,HSSFCell后設(shè)置單元格內(nèi)容便可以完成導(dǎo)出。
這次在項(xiàng)目中需要用到模板,導(dǎo)出的內(nèi)容包括(1.模板中的內(nèi)容、樣式。2.自己需要新增的內(nèi)容、樣式。),還需要設(shè)置單元格的樣式,在網(wǎng)上搜了一些blog,完成后記錄一下。
分析這次需求,最關(guān)鍵的就是如何獲取到填充了模板的新HSSFWorkbook,如果獲取到它,我們可以熟練的往里面添加內(nèi)容。
File fi = new File("F:/usr/user.xls");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
HSSFWorkbook wb = new HSSFWorkbook(fs);
這樣便可以獲取到我們熟悉的HSSFWorkbook對象了,操作熟悉的HSSFWorkbook對象想必爽歪歪了。這里還有一個(gè)需求,就是需要設(shè)置一些單元格的樣式,這在之前我也沒有接觸到過,記錄下來。
//生成單元格樣式 HSSFCellStyle cellStyle = wb.createCellStyle(); //wb是上一步創(chuàng)建的HSSFWorkbook對象 //設(shè)置背景顏色 cellStyle.setFillForegroundColor(HSSFColor.RED.index); //solid 填充 foreground 前景色 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
這樣便創(chuàng)建完成了一個(gè)單元格的樣式,接下來便是在特定的單元格添加樣式。
//獲取特定的單元格 HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum); //設(shè)置樣式 cell.setCellStyle(cellStyle); //cellStyle是上一步創(chuàng)建的HSSFCellStyle對象
如此,整個(gè)需求基本完成。對于整個(gè)過程中需要用到的其他方法,這里寫了一個(gè)封裝類。
** 有些方法可能只適用此項(xiàng)目,使用時(shí)需要修改。
package com.pole.educate.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.RichTextString;
/**
* 共分為六部完成根據(jù)模板導(dǎo)出excel操作:<br/>
* 第一步、設(shè)置excel模板路徑(setSrcPath)<br/>
* 第二步、設(shè)置要生成excel文件路徑(setDesPath)<br/>
* 第三步、設(shè)置模板中哪個(gè)Sheet列(setSheetName)<br/>
* 第四步、獲取所讀取excel模板的對象(getSheet)<br/>
* 第五步、設(shè)置數(shù)據(jù)(分為6種類型數(shù)據(jù):setCellStrValue、setCellDateValue、setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)<br/>
* 第六步、完成導(dǎo)出 (exportToNewFile)<br/>
*
* @author Administrator
*
*/
public class ExcelWriter {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
HSSFSheet sheet = null;
HSSFCellStyle cellStyle = null;
private String srcXlsPath = "";// excel模板路徑
private String desXlsPath = ""; // 生成路徑
private String sheetName = "";
/**
* 第一步、設(shè)置excel模板路徑
* @param srcXlsPaths
*/
public void setSrcPath(String srcXlsPaths) {
this.srcXlsPath = srcXlsPaths;
}
/**
* 第二步、設(shè)置要生成excel文件路徑
* @param desXlsPaths
* @throws FileNotFoundException
*/
public void setDesPath(String desXlsPaths) throws FileNotFoundException {
this.desXlsPath = desXlsPaths;
}
/**
* 第三步、設(shè)置模板中哪個(gè)Sheet列
* @param sheetName
*/
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
/**
* 第四步、獲取所讀取excel模板的對象
*/
public void getSheet() {
try {
File fi = new File(srcXlsPath);
if(!fi.exists()){
//System.out.println("模板文件:"+srcXlsPath+"不存在!");
return;
}
fs = new POIFSFileSystem(new FileInputStream(fi));
wb = new HSSFWorkbook(fs);
sheet = wb.getSheet(sheetName);
//生成單元格樣式
cellStyle = wb.createCellStyle();
//設(shè)置背景顏色
cellStyle.setFillForegroundColor(HSSFColor.RED.index);
//solid 填充 foreground 前景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
*/
public HSSFRow createRow(int rowIndex) {
HSSFRow row = sheet.createRow(rowIndex);
return row;
}
/**
*
*/
public void createCell(HSSFRow row,int colIndex) {
row.createCell(colIndex);
}
/**
* 第五步、設(shè)置單元格的樣式
* @param rowIndex 行值
* @param cellnum 列值
*/
public void setCellStyle(int rowIndex, int cellnum) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellStyle(cellStyle);
}
/**
* 第五步、設(shè)置字符串類型的數(shù)據(jù)
* @param rowIndex 行值
* @param cellnum 列值
* @param value 字符串類型的數(shù)據(jù)
*/
public void setCellStrValue(int rowIndex, int cellnum, String value) {
if(value != null) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
}
/**
* 第五步、設(shè)置日期/時(shí)間類型的數(shù)據(jù)
* @param rowIndex 行值
* @param cellnum 列值
* @param value 日期/時(shí)間類型的數(shù)據(jù)
*/
public void setCellDateValue(int rowIndex, int cellnum, Date value) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
/**
* 第五步、設(shè)置浮點(diǎn)類型的數(shù)據(jù)
* @param rowIndex 行值
* @param cellnum 列值
* @param value 浮點(diǎn)類型的數(shù)據(jù)
*/
public void setCellDoubleValue(int rowIndex, int cellnum, double value) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
/**
* 第五步、設(shè)置Bool類型的數(shù)據(jù)
* @param rowIndex 行值
* @param cellnum 列值
* @param value Bool類型的數(shù)據(jù)
*/
public void setCellBoolValue(int rowIndex, int cellnum, boolean value) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
/**
* 第五步、設(shè)置日歷類型的數(shù)據(jù)
* @param rowIndex 行值
* @param cellnum 列值
* @param value 日歷類型的數(shù)據(jù)
*/
public void setCellCalendarValue(int rowIndex, int cellnum, Calendar value) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
/**
* 第五步、設(shè)置富文本字符串類型的數(shù)據(jù)。可以為同一個(gè)單元格內(nèi)的字符串的不同部分設(shè)置不同的字體、顏色、下劃線
* @param rowIndex 行值
* @param cellnum 列值
* @param value 富文本字符串類型的數(shù)據(jù)
*/
public void setCellRichTextStrValue(int rowIndex, int cellnum,
RichTextString value) {
HSSFCell cell = sheet.getRow(rowIndex).getCell(cellnum);
cell.setCellValue(value);
}
/**
* 第六步、完成導(dǎo)出
*/
public void exportToNewFile() {
FileOutputStream out;
try {
out = new FileOutputStream(desXlsPath);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上這篇POI通過模板導(dǎo)出EXCEL文件的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法
- Springboot使用POI實(shí)現(xiàn)導(dǎo)出Excel文件示例
- 詳解poi+springmvc+springjdbc導(dǎo)入導(dǎo)出excel實(shí)例
- jsp利用POI生成Excel并在頁面中導(dǎo)出的示例
- Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格示例代碼
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- java使用poi導(dǎo)出Excel的方法
- POI導(dǎo)出Excel報(bào)錯(cuò)No such file or directory的解決方法
- Java poi導(dǎo)出Excel下載到客戶端
- POI通用導(dǎo)出Excel(.xls,.xlsx)的方法
相關(guān)文章
Mybatis如何自動(dòng)生成數(shù)據(jù)庫表的實(shí)體類
這篇文章主要介紹了Mybatis自動(dòng)生成數(shù)據(jù)庫表的實(shí)體類的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
eclipse連接數(shù)據(jù)庫并實(shí)現(xiàn)用戶注冊登錄功能
這篇文章主要介紹了eclipse連接數(shù)據(jù)庫并實(shí)現(xiàn)用戶注冊登錄功能的相關(guān)資料,需要的朋友可以參考下2021-01-01
java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
這篇文章主要介紹了java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
數(shù)組和二維數(shù)組感覺用王者榮耀的裝備欄來舉例解釋,應(yīng)該更易懂一些。從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級,中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03最新評論

