Java reads Excel files and copies (copy) files to the specified directory sample _java

Source: Internet
Author: User

Copy Code code as follows:

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;

/**
* Read the Excel file and put the list of files in
* @param sheetnumber
* @param dir Excel file directory
* @return
* @throws FileNotFoundException
* @throws IOException
*/
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));

Get the specified table
Hssfsheet sheet = workbook.getsheetat (sheetnumber);
Get total rows of data
int rowcount = Sheet.getlastrownum ();
Logger.info ("Found Excel rows count:" + rowcount);
if (RowCount < 1) {
return result;
}
Read data line by row
for (int rowIndex = 4; rowIndex <= rowcount; rowindex++) {
Get Row objects
Hssfrow row = Sheet.getrow (RowIndex);
if (row!= null) {
list<object> rowdata = new arraylist<object> ();
Get the number of cells in the bank
int columnCount = Row.getlastcellnum ();
Get data from each cell in the bank
Hssfcell cell = Row.getcell (1);
Get 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 + +;
}
}
}
}
}
/**
* Copy str to DestPath
*
* @param str
* @param sourcepath
* @param destpath
* @return Boolean isfile return true;else return false;
* @throws IOException
*/
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;

}
/**
* Get the contents of the cell
* @param cell
* @return
*/
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;
}

/**
*
* @param directory of args args[0]:excel files; args[1]: Source directory (compiled file directory); args[2]: Publish Directory
* @throws Exception
*/
public static void Main (string[] args) throws Exception {

if (args = null | | args.length <3)
{
Logger.info ("file isn't find;");

Logger.fatal ("Java Cn.id5.deploy.DeployByExcel $ $ $ \n$0:excel file directory; $: source directory (compiled file directory); $: Publish directory; $: JSP directory (default is WebContent, nullable) \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/

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.