java poi导出excel模板_POI通过模板导出EXCEL文件的实例
一般的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對(duì)象了,操作熟悉的HSSFWorkbook對(duì)象想必爽歪歪了。這里還有一個(gè)需求,就是需要設(shè)置一些單元格的樣式,這在之前我也沒有接觸到過,記錄下來。
//生成單元格樣式
HSSFCellStyle cellStyle = wb.createCellStyle(); //wb是上一步創(chuàng)建的HSSFWorkbook對(duì)象
//設(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對(duì)象
如此,整個(gè)需求基本完成。對(duì)于整個(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操作:
* 第一步、設(shè)置excel模板路徑(setSrcPath)
* 第二步、設(shè)置要生成excel文件路徑(setDesPath)
* 第三步、設(shè)置模板中哪個(gè)Sheet列(setSheetName)
* 第四步、獲取所讀取excel模板的對(duì)象(getSheet)
* 第五步、設(shè)置數(shù)據(jù)(分為6種類型數(shù)據(jù):setCellStrValue、setCellDateValue、setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)
* 第六步、完成導(dǎo)出 (exportToNewFile)
*
* @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模板的對(duì)象
*/
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è)參考,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的java poi导出excel模板_POI通过模板导出EXCEL文件的实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第九章 DLL文件 windows程序
- 下一篇: AcWing 738. 数组填充