Java reads the excel file and copies the of copy file to the specified directory sample

  • 2020-04-01 02:57:01
  • OfStack


mport java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class DeployByExcel {
 private static Logger logger= Logger.getLogger(DeployByExcel.class);

 static final int BUFFER = 8192;  

 //Excel
 private HSSFWorkbook workbook ;
 
 public List<String> getDatasInSheet(int sheetNumber,File dir) throws FileNotFoundException, IOException{

  File[] files = dir.listFiles();  
  List<String> result = new ArrayList<String>(); 
        for(File f : files)
        {
         if(!f.getName().toLowerCase().endsWith(".xls"))
         {
          continue;
         }
         workbook = new HSSFWorkbook(new FileInputStream(f));

   //Gets the specified table
   HSSFSheet sheet = workbook.getSheetAt(sheetNumber); 
   //Get the total number of rows of data
   int rowCount = sheet.getLastRowNum();
   logger.info("found excel rows count: " + rowCount);
   if (rowCount < 1) {
    return result;
   } 
   //Read the data line by line
   for (int rowIndex = 4; rowIndex <= rowCount; rowIndex++) {  
    //Get the row object
    HSSFRow row = sheet.getRow(rowIndex); 
    if (row != null) {
     List<Object> rowData = new ArrayList<Object>();
     //Gets the number of cells in the row
     int columnCount = row.getLastCellNum();
     //Gets the data in each cell on the row
     HSSFCell cell = row.getCell(1);
     //Gets the data in the specified cell
     String str = (String)this.getCellString(cell);
     if (str!=null && str.length()>1)
      result.add(str);
    }
   }
        }
  return result;
 }

 private void copy(String sourcePath,String destPath,List<String> fileList,String webContent) throws IOException{
  int num =1 ;
  for (String str : fileList){
   str = str.replace(".java", ".class");
   if (str.indexOf("/")!=-1){

    if (str.indexOf("src")==0){
     str = str.replace("src", "WEB-INF/classes");
    }else if (str.toUpperCase().indexOf(webContent.toUpperCase())==0){
     str = str.replace(webContent+"/", "");
    }

    
    boolean f = copyFile(str,sourcePath,destPath);
    if(f)
    {
     logger.info("The file is:" + num);
     num ++;

     String fileName1 = str;
     int n = 1;
     while(fileName1.endsWith(".class"))
     {
      str = fileName1.replace(".class", "$" + n +".class");
      if(!copyFile(str,sourcePath,destPath))
      {
       break;
      }
      n ++;
     }
    }
   }
  }
 }
 
 private boolean copyFile(String str,String sourcePath,String destPath) throws IOException
 {
  boolean f = false;
  String destFilePath = destPath+str;
  String sourceFilePath = sourcePath+str;
  File newDir = new File(destFilePath.substring(0,destFilePath.lastIndexOf('/')));
  File sourceFile = new File(sourceFilePath.trim());
  if(!sourceFile.exists())
  {
   return f;
  }
  logger.info("dest:"+destFilePath+"     "+"source:"+sourceFilePath);
  File destFile = new File(destFilePath.trim());
  if (!newDir.exists()){
   newDir.mkdirs();
  }
  if(!sourceFile.isDirectory())
  {
   InputStream in=new FileInputStream(sourceFile);
   FileOutputStream out=new FileOutputStream(destFile);
   byte[] buffer=new byte[1024];
   int ins;
   while((ins=in.read(buffer))!=-1){
    out.write(buffer,0,ins);
   }
   in.close();
   out.flush();
   out.close();
   f = true;
  }
  return f;

 }
 
 protected Object getCellString(HSSFCell cell){
  Object result = null;
     if (cell != null) {
      int cellType = cell.getCellType();
      switch(cellType){
       case HSSFCell.CELL_TYPE_STRING :
        result = cell.getRichStringCellValue().getString();
        break;
       case HSSFCell.CELL_TYPE_NUMERIC:
        result=cell.getNumericCellValue();
        break;
       case HSSFCell.CELL_TYPE_FORMULA:
        result = cell.getNumericCellValue();
        break;
       case HSSFCell.CELL_TYPE_ERROR:
        result=null;
        break;
       case HSSFCell.CELL_TYPE_BOOLEAN:
        result=cell.getBooleanCellValue();
        break;
       case HSSFCell.CELL_TYPE_BLANK:
        result=null;
        break;
      }
     }
     return result;
 }
   
 
 public static void main(String[] args) throws Exception {

  if(args == null || args.length <3 )
  {
   logger.info("file is not find;");

   logger.fatal("java cn.id5.deploy.DeployByExcel $0 $1 $2 $3 n$0:Excel File directory; $1: The source directory ( Compiled file directory ) ; $2: Publishing catalogue; $3:jsp Directory (default is webContent Can be empty) nexiting.");
   System.exit(0);
  }

    File file = new File(args[0]);
    DeployByExcel deploy = new DeployByExcel();
    List<String> fileList = deploy.getDatasInSheet(0,file);
    String classPath = args[1];
    String destPath = args[2]; 
    String webContent = (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/

 }
}


Related articles: