POI操作excel示例工具类.docx
- 文档编号:23951649
- 上传时间:2023-05-22
- 格式:DOCX
- 页数:34
- 大小:22.59KB
POI操作excel示例工具类.docx
《POI操作excel示例工具类.docx》由会员分享,可在线阅读,更多相关《POI操作excel示例工具类.docx(34页珍藏版)》请在冰豆网上搜索。
POI操作excel示例工具类
由于近期项目要用到excel来转存页面中的表单数据,对poi操作excel进行了一番了解,写了以下,但总觉的不是很好
特此:
发布在此供大家评论,广义集思,还望多多指教
1.workBook处理类
Java代码
/**
* Excel WorkBook工具类
* @author dsy
* @version 1.0
*/
public class ExcelWorkBook {
public HSSFWorkbook workbook = null;
public static HSSFWorkbook workbookTemp = null;
//设置当前workbookName
private String workbookName = null;
private HSSFSheet sheet = null;
private FileOutputStream fileOut;
public ExcelWorkBook() {
if(workbook !
= null) {
workbook = null;
}
workbook = workbookTemp;
}
public ExcelWorkBook(String workbookName) {
workbook = workbookTemp;
setWorkbookName(workbookName);
}
public String getWorkbookName() {
return workbookName;
}
public void setWorkbookName(String workbookName) {
workbookName = workbookName;
}
public HSSFSheet getSheet() {
sheet = workbook.createSheet(getWorkbookName());
return sheet;
}
/**
* 用于stylUtils的所需要的workbook必须项所做的处理
* @return
*/
public static HSSFWorkbook getWorkbook() {
return workbookTemp;
}
public static void setWorkbook(HSSFWorkbook workbook) {
workbookTemp = workbook;
}
/**
* 输入当前WorkBook为下载临时文件记录
* @param excelName
*/
public void writerFileStream(String excelName) {
try {
fileOut = new FileOutputStream(excelName);
workbook.write(fileOut);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOut.flush();
fileOut.close();
if(workbook !
= null) {
workbook = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
*ExcelWorkBook工具类
*@authordsy
*@version1.0
*/
publicclassExcelWorkBook{
publicHSSFWorkbookworkbook=null;
publicstaticHSSFWorkbookworkbookTemp=null;
//设置当前workbookName
privateStringworkbookName=null;
privateHSSFSheetsheet=null;
privateFileOutputStreamfileOut;
publicExcelWorkBook(){
if(workbook!
=null){
workbook=null;
}
workbook=workbookTemp;
}
publicExcelWorkBook(StringworkbookName){
workbook=workbookTemp;
setWorkbookName(workbookName);
}
publicStringgetWorkbookName(){
returnworkbookName;
}
publicvoidsetWorkbookName(StringworkbookName){
workbookName=workbookName;
}
publicHSSFSheetgetSheet(){
sheet=workbook.createSheet(getWorkbookName());
returnsheet;
}
/**
*用于stylUtils的所需要的workbook必须项所做的处理
*@return
*/
publicstaticHSSFWorkbookgetWorkbook(){
returnworkbookTemp;
}
publicstaticvoidsetWorkbook(HSSFWorkbookworkbook){
workbookTemp=workbook;
}
/**
*输入当前WorkBook为下载临时文件记录
*@paramexcelName
*/
publicvoidwriterFileStream(StringexcelName){
try{
fileOut=newFileOutputStream(excelName);
workbook.write(fileOut);
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
fileOut.flush();
fileOut.close();
if(workbook!
=null){
workbook=null;
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
2.ExcelRow工具类
Java代码
/**
* Excel Row工具类
* @author dsy
* @version 1.0
*/
public class ExcelSheetRow {
public ExcelSheetRow() {
// TODO Auto-generated constructor stub
}
public static HSSFSheet sheet = null;
/**
* 设置当前Sheet名字
*/
private static String sheetName = null;
private static HSSFRow row = null;
/**
* 创建当前标题行
* @param sheet
* @return
*/
public static HSSFRow createCurrSheetTitle(ExcelWorkBook work) {
HSSFSheet sheet = work.getSheet();
row = sheet.createRow(0);
return row;
}
/**
* 创建当前excel记录内容
* @param sheet
* @param i
* @return
*/
public static HSSFRow createCurrSheetRecord(ExcelWorkBook work,int i) {
HSSFSheet sheet = work.getSheet();
row = sheet.createRow(i+1);
return row;
}
public static String getSheetName() {
return sheetName;
}
public static void setSheetName(String sheetName) {
ExcelSheetRow.sheetName = sheetName;
}
}
/**
*ExcelRow工具类
*@authordsy
*@version1.0
*/
publicclassExcelSheetRow{
publicExcelSheetRow(){
//TODOAuto-generatedconstructorstub
}
publicstaticHSSFSheetsheet=null;
/**
*设置当前Sheet名字
*/
privatestaticStringsheetName=null;
privatestaticHSSFRowrow=null;
/**
*创建当前标题行
*@paramsheet
*@return
*/
publicstaticHSSFRowcreateCurrSheetTitle(ExcelWorkBookwork){
HSSFSheetsheet=work.getSheet();
row=sheet.createRow(0);
returnrow;
}
/**
*创建当前excel记录内容
*@paramsheet
*@parami
*@return
*/
publicstaticHSSFRowcreateCurrSheetRecord(ExcelWorkBookwork,inti){
HSSFSheetsheet=work.getSheet();
row=sheet.createRow(i+1);
returnrow;
}
publicstaticStringgetSheetName(){
returnsheetName;
}
publicstaticvoidsetSheetName(StringsheetName){
ExcelSheetRow.sheetName=sheetName;
}
}
3. ExcelCell工具类
Java代码
/**
* Excel Cell工具类
* @author dsy
* @version 1.0
*/
public class ExcelSheetCell {
private static HSSFRow row = null;
private static HSSFCell cell = null;
/**
* 用于产生当前excel标题
* @param sheet [当前工作表单]
* @param firstRowValue [标题数组]
* @param style [当前单元格风格]
*/
public static void createCurrRowTitle(ExcelSheetRow sheetRow,ExcelWorkBook work ,String[] firstRowValue,HSSFCellStyle style) {
row = sheetRow.createCurrSheetTitle(work);
for (int i = 0; i < firstRowValue.length; i++) {
cell = row.createCell((short) i);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(firstRowValue[i]);
}
}
/**
* 用于生成excel当前记录内容,标题除外
* @param sheet [当前工作表单]
* @param beanList [当前数据列表,i=Object[]]
* @param style [当前单元格风格]
*/
public static void createCurrRowRecord(ExcelSheetRow sheetRow,ExcelWorkBook work,List beanList,HSSFCellStyle style) {
Object[] obj = null;
for (int i = 0; i < beanList.size(); i++) {
row = sheetRow.createCurrSheetRecord(work,i);
obj = (Object[]) beanList.get(i);
if (obj !
= null) {
createExcelCell(row, obj,style);
}
}
}
/**
* 需要以数组的方式提供当前每条记录
* 通过数组自动判断有多少列,生成当前行
*/
private static void createExcelCell(HSSFRow row, Object[] obj,HSSFCellStyle style) {
try {
for (int i = 0; i < obj.length; i++) {
try {
if (obj[i].toString() !
= null) {
cell = row.createCell((short) i);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[i].toString());
}
} catch (NullPointerException e) {
continue;
}
}
} catch (Exception ex) {
System.out.print(ex);
}
}
}
/**
*ExcelCell工具类
*@authordsy
*@version1.0
*/
publicclassExcelSheetCell{
privatestaticHSSFRowrow=null;
privatestaticHSSFCellcell=null;
/**
*用于产生当前excel标题
*@paramsheet[当前工作表单]
*@paramfirstRowValue[标题数组]
*@paramstyle[当前单元格风格]
*/
publicstaticvoidcreateCurrRowTitle(ExcelSheetRowsheetRow,ExcelWorkBookwork,String[]firstRowValue,HSSFCellStylestyle){
row=sheetRow.createCurrSheetTitle(work);
for(inti=0;i cell=row.createCell((short)i); cell.setCellStyle(style); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(firstRowValue[i]); } } /** *用于生成excel当前记录内容,标题除外 *@paramsheet[当前工作表单] *@parambeanList[当前数据列表,i=Object[]] *@paramstyle[当前单元格风格] */ publicstaticvoidcreateCurrRowRecord(ExcelSheetRowsheetRow,ExcelWorkBookwork,ListbeanList,HSSFCellStylestyle){ Object[]obj=null; for(inti=0;i row=sheetRow.createCurrSheetRecord(work,i); obj=(Object[])beanList.get(i); if(obj! =null){ createExcelCell(row,obj,style); } } } /** *需要以数组的方式提供当前每条记录 *通过数组自动判断有多少列,生成当前行 */ privatestaticvoidcreateExcelCell(HSSFRowrow,Object[]obj,HSSFCellStylestyle){ try{ for(inti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- POI 操作 excel 示例 工具
![提示](https://static.bdocx.com/images/bang_tan.gif)