`
jiangduxi
  • 浏览: 443827 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java实现读取文件下所有的excel文件

 
阅读更多

   最近处理一个压缩包,里面有几百上千个excel文件。本来可以通过直接将所有excel合并至一个大excel文件,然后再导入至数据库。
   但是这样太麻烦了,于是写了两个类来处理一个类处理excel文件,一个类读取文件夹下的文件。
下面给出代码
解析excel类(包括读写更新操作)
public class ExcelFile
{
	
	/**
	 * read
	 * @param file
	 */
	public static void readExl(String file){
		try
		{
			Workbook book = Workbook.getWorkbook(new File(file));
			
			Sheet[] sheets = book.getSheets();
		
			for(int i=0; i < sheets.length;i++){
				 Sheet sheet = sheets[i];
				 System.out.println("Sheet"+i);
				 int col = sheet.getColumns();
				 int row = sheet.getRows();
				 
				 for( int r=0 ; r < row;r++){
						StringBuffer sb = new StringBuffer();
					  for (int c = 0; c < col; c++)
					{
						  Cell cell = sheet.getCell(c,r);
						  
						  String result = cell.getContents();
						  
						 sb.append(result).append("   ");
					}
					  
					 System.out.println(sb.toString());
				 }
			}
			book.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		
	}
	
	
	/**
	 * Update
	 * @param file
	 */
	public static void updateExl(String file){
		try
		{
			Workbook wb = Workbook.getWorkbook(new File(file));
			WritableWorkbook book = Workbook.createWorkbook(new File(file),wb);
			
			//WritableSheet sheet = book.createSheet("second", 1);
			WritableSheet sheet = book.getSheet(0);
			Label label = new Label(0,0,"test data 2");
			sheet.addCell(label);
			book.write();
			book.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		
	}
	
	public static void readExl(String file, int i){
		try
		{
			Workbook book = Workbook.getWorkbook(new File(file));
			Sheet sheet = book.getSheet(i);
			Cell cell = sheet.getCell(0,0);
			String result = cell.getContents();
			System.out.println(result);
			book.close();
			
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		
	}
	
	
	/**
	 * Create
	 * @param file
	 */
	public void createExl(String file){
	  try
	{
		WritableWorkbook book = Workbook.createWorkbook(new File(file));
		WritableSheet sheet = book.createSheet("first", 0);
		
		
		Label lable = new Label(0,0,"test");
		sheet.addCell(lable);
		
		Number number = new Number(1,0,123);
		sheet.addCell(number);
		book.write();
		book.close();
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
		
	}

}

还有个类就是读取文件夹下的文件包括(读取和删除)
public class ReadFile
{
	/**
	 * Delete
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static boolean deleteFile(String path) throws FileNotFoundException,IOException{
		try
		{
			File file = new File(path);
			if (!file.isDirectory())
			{
				file.delete();
			}else{
				 String filelist[] = file.list();
				 for(int i=0; i< filelist.length;i++){
					  File delfile = new File(path +"\\"+filelist[i]);
					  if(delfile.isDirectory()){
						  System.out.println("FileName:"+delfile.getName());
						  System.out.println("AbsolutePath:"+delfile.getAbsolutePath());
						  delfile.delete();
					  }else{
						  deleteFile(path+"\\"+filelist[i]);
					  }
				 }
				 file.delete();
			}
		}
		catch (Exception e)
		{
			System.out.println("deletFile() Exception:"+e.getMessage());
		}
		return true;
	}
	
	
	
	/**
	 * Read
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static boolean readFile(String path) throws FileNotFoundException,IOException{
		try
		{
			File file = new File(path);
			if (!file.isDirectory())
			{
			   
			}else{
				System.out.println("文件夹");
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++)
				{
					File readFile = new File(path+"\\"+filelist[i]);
					if(!readFile.isDirectory()){
						System.out.println("read file Name:" + readFile.getName());
						ExcelFile.readExl(readFile.getPath());
						System.out.println("------------------------");
						//System.out.println("path=" + readFile.getPath());
			            //System.out.println("absolutepath=" + readFile.getAbsolutePath());
			            //System.out.println("name=" + readFile.getName());
					}else{
						readFile(path+"\\"+filelist[i]);
					}
				}
				
			}
		}
		catch (Exception e)
		{
			System.out.println("readFile() Exception:"+e.getMessage());
		}
		return true;
	}
	
	public static void main(String[] args)
	{
	
			try
			{
				readFile("C:/Documents and Settings/Developer/Desktop/All-01012012-05312012");
			}
			catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//deleteFile("C:/Documents and Settings/Developer/Desktop/All-01012012-05312012/All-01012012-05312012/ok");
		
	}
}

0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics