java读取excel文件并复制(copy)文件到指定目录示例
mportjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.log4j.Logger;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
publicclassDeployByExcel{
privatestaticLoggerlogger=Logger.getLogger(DeployByExcel.class);
staticfinalintBUFFER=8192;
//Excel
privateHSSFWorkbookworkbook;
/**
*读取Excel文件并将文件列表放到list中
*@paramsheetNumber
*@paramdirexcel文件所在目录
*@return
*@throwsFileNotFoundException
*@throwsIOException
*/
publicList<String>getDatasInSheet(intsheetNumber,Filedir)throwsFileNotFoundException,IOException{
File[]files=dir.listFiles();
List<String>result=newArrayList<String>();
for(Filef:files)
{
if(!f.getName().toLowerCase().endsWith(".xls"))
{
continue;
}
workbook=newHSSFWorkbook(newFileInputStream(f));
//获得指定的表
HSSFSheetsheet=workbook.getSheetAt(sheetNumber);
//获得数据总行数
introwCount=sheet.getLastRowNum();
logger.info("foundexcelrowscount:"+rowCount);
if(rowCount<1){
returnresult;
}
//逐行读取数据
for(introwIndex=4;rowIndex<=rowCount;rowIndex++){
//获得行对象
HSSFRowrow=sheet.getRow(rowIndex);
if(row!=null){
List<Object>rowData=newArrayList<Object>();
//获得本行中单元格的个数
intcolumnCount=row.getLastCellNum();
//获得本行中各单元格中的数据
HSSFCellcell=row.getCell(1);
//获得指定单元格中数据
Stringstr=(String)this.getCellString(cell);
if(str!=null&&str.length()>1)
result.add(str);
}
}
}
returnresult;
}
privatevoidcopy(StringsourcePath,StringdestPath,List<String>fileList,StringwebContent)throwsIOException{
intnum=1;
for(Stringstr:fileList){
str=str.replace(".java",".class");
if(str.indexOf("/")!=-1){
if(str.indexOf("src")==0){
str=str.replace("src","WEB-INF/classes");
}elseif(str.toUpperCase().indexOf(webContent.toUpperCase())==0){
str=str.replace(webContent+"/","");
}
booleanf=copyFile(str,sourcePath,destPath);
if(f)
{
logger.info("Thefileis:"+num);
num++;
StringfileName1=str;
intn=1;
while(fileName1.endsWith(".class"))
{
str=fileName1.replace(".class","$"+n+".class");
if(!copyFile(str,sourcePath,destPath))
{
break;
}
n++;
}
}
}
}
}
/**
*copystrtodestPath
*
*@paramstr
*@paramsourcePath
*@paramdestPath
*@returnbooleanisFilereturntrue;elsereturnfalse;
*@throwsIOException
*/
privatebooleancopyFile(Stringstr,StringsourcePath,StringdestPath)throwsIOException
{
booleanf=false;
StringdestFilePath=destPath+str;
StringsourceFilePath=sourcePath+str;
FilenewDir=newFile(destFilePath.substring(0,destFilePath.lastIndexOf("/")));
FilesourceFile=newFile(sourceFilePath.trim());
if(!sourceFile.exists())
{
returnf;
}
logger.info("dest:"+destFilePath+" "+"source:"+sourceFilePath);
FiledestFile=newFile(destFilePath.trim());
if(!newDir.exists()){
newDir.mkdirs();
}
if(!sourceFile.isDirectory())
{
InputStreamin=newFileInputStream(sourceFile);
FileOutputStreamout=newFileOutputStream(destFile);
byte[]buffer=newbyte[1024];
intins;
while((ins=in.read(buffer))!=-1){
out.write(buffer,0,ins);
}
in.close();
out.flush();
out.close();
f=true;
}
returnf;
}
/**
*获得单元格中的内容
*@paramcell
*@return
*/
protectedObjectgetCellString(HSSFCellcell){
Objectresult=null;
if(cell!=null){
intcellType=cell.getCellType();
switch(cellType){
caseHSSFCell.CELL_TYPE_STRING:
result=cell.getRichStringCellValue().getString();
break;
caseHSSFCell.CELL_TYPE_NUMERIC:
result=cell.getNumericCellValue();
break;
caseHSSFCell.CELL_TYPE_FORMULA:
result=cell.getNumericCellValue();
break;
caseHSSFCell.CELL_TYPE_ERROR:
result=null;
break;
caseHSSFCell.CELL_TYPE_BOOLEAN:
result=cell.getBooleanCellValue();
break;
caseHSSFCell.CELL_TYPE_BLANK:
result=null;
break;
}
}
returnresult;
}
/**
*
*@paramargsargs[0]:Excel文件所在目录;args[1]:源目录(编译后的文件目录);args[2]:发布目录
*@throwsException
*/
publicstaticvoidmain(String[]args)throwsException{
if(args==null||args.length<3)
{
logger.info("fileisnotfind;");
logger.fatal("javacn.id5.deploy.DeployByExcel$0$1$2$3\n$0:Excel文件所在目录;$1:源目录(编译后的文件目录);$2:发布目录;$3:jsp所在目录(默认为webContent,可空)\nexiting.");
System.exit(0);
}
Filefile=newFile(args[0]);
DeployByExceldeploy=newDeployByExcel();
List<String>fileList=deploy.getDatasInSheet(0,file);
StringclassPath=args[1];
StringdestPath=args[2];
StringwebContent=(args.length>3&&args[3]!=null&&args[3].length()>1)?args[3]:"WebContent";
deploy.copy(classPath,destPath,fileList,webContent);
///tmp/gboss/media/terry/doc/Project_ID5/gboss/WebContent/
}
}
相关文章
- 导出的Excel名字乱码_恢复的excel文件乱码
- excel中html批量转化为pdf文件,如何将大量的Excel转换成PDF?
- java删除文件目录及文件_Java删除文件,目录
- java源程序文件的扩展名_使用Java语言编写的源程序保存时的文件扩展名是什么…
- java后端开发需要什么_从事Java后端开发,要学习哪些知识和技能?[通俗易懂]
- java常量有哪些_Java中的常量有哪些?
- java怎么用_如何使用Java编写程序
- Java后台接收参数出现java.lang.Integer cannot be cast to java.lang.Double错误(已解决)[通俗易懂]
- xml格式化 java_Java XML格式化程序
- Java XLSTransformer生成excel文件
- java 文件上传到服务器_Java上传文件到服务器端的方法「建议收藏」
- java list 转json 字符串_Java之JSON字符串与List集合之间相互转换
- Java生成随机数组_java生成唯一数字
- 图片链接如何在excel里转成图片_mdf文件怎么转成Excel
- Python xlwt数据保存到 Excel中以及xlrd读取excel文件画图
- Java读取Excel文件详解编程语言
- java把Excel文件数据导入数据库详解编程语言
- java POI读取Excel文件详解编程语言
- [问题解决]大数据量上载excel文件数据到SAP系统[ALSM_EXCEL_TO_INTERNAL_TABLE]详解编程语言
- 快速安装Excel:Linux用户指南(linux安装excel)
- Java连接SQL Server:实现数据库完美对接(java链接sqlserver)
- MySQL与Java的数据交互之旅(mysql对应java)
- Java快速加载Oracle数据库(java加载oracle)