300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java poi 模板填数据库 java使用POI读取excel模版并向固定表格里填写数据详解

java poi 模板填数据库 java使用POI读取excel模版并向固定表格里填写数据详解

时间:2022-03-10 07:27:14

相关推荐

java poi 模板填数据库 java使用POI读取excel模版并向固定表格里填写数据详解

java使用POI读取excel模版并向固定表格里填写数据详解:publicclassExportExcelDemo{

privateHSSFWorkbookworkbook=null;

/**

*显示的导出表的标题

*/

privateStringtitle;

/**

*导出表的列名

*/

privateString[]rowName;

privateListdataList=newArrayList<>();

/**

*构造方法,传入要导出的数据

*

*@paramtitle

*@paramrowName

*@paramdataList

*/

publicExportExcelDemo(Stringtitle,String[]rowName,ListdataList){

this.dataList=dataList;

this.rowName=rowName;

this.title=title;

}

/**

*判断文件的sheet是否存在

*@paramfilePath文件路径

*@paramsheetName表格索引名

*@return

*/

publicbooleansheetExist(StringfilePath,StringsheetName){

booleanflag=false;

Filefile=newFile(filePath);

if(file.exists()){//文件存在

//创建workbook

try{

workbook=newHSSFWorkbook(newFileInputStream(file));

//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)

HSSFSheetsheet=workbook.getSheet(sheetName);

if(sheet!=null)

flag=true;

}catch(Exceptione){

e.printStackTrace();

}

}else{//文件不存在

flag=false;

}

returnflag;

}

/**

*

*(xls后缀导出)

*@paramTODO

*@returnvoid返回类型

*@authorxsw

*@-12-7上午10:44:00

*/

publicstaticvoidcreateXLS(StringimportFilePath,StringexportFilePath)throwsIOException{

try{

//excel模板路径

Filefi=newFile(importFilePath);

POIFSFileSystemfs=newPOIFSFileSystem(newFileInputStream(fi));

//读取excel模板

HSSFWorkbookwb=newHSSFWorkbook(fs);

//读取了模板内所有sheet内容

HSSFSheetsheet=wb.getSheetAt(0);

//如果这行没有了,整个公式都不会有自动计算的效果的

sheet.setForceFormulaRecalculation(true);

//在相应的单元格进行赋值

HSSFCellcell=sheet.getRow(11).getCell(6);//第11行第6列

cell.setCellValue(1);

HSSFCellcell2=sheet.getRow(11).getCell(7);

cell2.setCellValue(2);

sheet.getRow(12).getCell(6).setCellValue(12);

sheet.getRow(12).getCell(7).setCellValue(12);

//修改模板内容导出新模板

FileOutputStreamout=newFileOutputStream(exportFilePath);

wb.write(out);

out.close();

}catch(Exceptione){

System.out.println("文件读取错误!");

}

}

/**

*

*(xlsx后缀导出)

*@param

*@returnvoid返回类型

*@authorxsw

*@-12-7上午10:44:30

*/

publicstaticvoidcreateXLSX(StringimportFilePath,StringexportFilePath)throwsIOException{

//excel模板路径

Filefi=newFile(importFilePath);

InputStreamin=newFileInputStream(fi);

//读取excel模板

XSSFWorkbookwb=newXSSFWorkbook(in);

//读取了模板内所有sheet内容

XSSFSheetsheet=wb.getSheetAt(0);

//如果这行没有了,整个公式都不会有自动计算的效果的

sheet.setForceFormulaRecalculation(true);

//在相应的单元格进行赋值

XSSFCellcell=sheet.getRow(11).getCell(6);//第12行第7列

cell.setCellValue(1);

XSSFCellcell2=sheet.getRow(11).getCell(7);

cell2.setCellValue(2);

sheet.getRow(12).getCell(6).setCellValue(3);

sheet.getRow(12).getCell(7).setCellValue(4);

//修改模板内容导出新模板

FileOutputStreamout=newFileOutputStream(exportFilePath);

wb.write(out);

out.close();

}

/**

*@param@paramfile

*@param@return

*@param@throwsIOException

*@returnList(excel每行拼接成List中的String)

*@throws

*@Title:readExcel

*@Description:TODO(对外提供读取excel的方法)

*/

publicstaticsynchronizedvoidreadExcel(StringimportFilePath,StringexportFilePath)throwsIOException{

Filefile=newFile(importFilePath);

StringfileName=file.getName();

//Listlist=newArrayList();

//根据其名称获取后缀

Stringextension=fileName.lastIndexOf(".")==-1?"":fileName

.substring(fileName.lastIndexOf(".")+1);

if("xls".equals(extension)){

readExcel(newFileInputStream(file),exportFilePath);

}elseif("xlsx".equals(extension)||"xlsm".equals(extension)){

readExcel(newFileInputStream(file),exportFilePath);

}elseif("tmp".equals(extension)){

readExcel(newFileInputStream(file),exportFilePath);

}else{

thrownewIOException("不支持的文件类型");

}

}

publicstaticvoidmain(String[]args)throwsIOException{

//excle

StringimportFilePath="/Users/dataDemo.xlsx";

StringexportFilePath="/Users/test2.xlsx";

//createXLSX(importFilePath,exportFilePath);

readExcel(importFilePath,exportFilePath);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。