Java读取指定文件下的文件名到Excel中

来源:互联网 发布:山东数据恢复 编辑:程序博客网 时间:2024/05/18 00:37
package Test_Maya_Excel;


import java.io.File;
/**
 * 扫描指定路径下面文件名字,并保存到指定的Excel中。
 * 有待改进(GUI)
 * @author HL
 *
 */


public class Test_maya {


public static void main(String[] args) throws Exception {   
        // This is the path where the file's name you want to take.   
        String path = "E:\\Read";   
        getFile(path);   
    }   
       
    @SuppressWarnings("null")
private static void getFile(String path) throws Exception{   
        // get file list where the path has   
        File file = new File(path);   
        // get the folder list   
        File[] array = file.listFiles();   
         
       
        
 
        
        
        String[] str = new String [120];
     
        
        for(int i=0;i<array.length;i++){   
            if(array[i].isFile()){   //如果是文件,遍历
                // only take file name   
            System.out.println(array[i].getName());
               str[i]=array[i].getName();
          
               
            }else if(array[i].isDirectory()){   //如果是文件夹,遍历文件夹
                getFile(array[i].getPath());   
            }   
        } 
        
        Maya_Excel.file_to_Excel(str,array.length);
        
    }
    
    



}






package Test_Maya_Excel;


import java.io.File;
import java.io.IOException;


import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


class Maya_Excel{




public static  void file_to_Excel(String[] str,int n) throws Exception 
{
 


File file = new File("E:\\GetInfo\\现有场景汇总.xls");
   //打开文件 
   WritableWorkbook book= Workbook.createWorkbook(file); 
   //生成名为“第一页”的工作表,参数0表示这是第一页 
   WritableSheet sheet=book.createSheet("第一页",0); 
   //在Label对象的构造子中指名单元格位置是第一列第一行(0,0) 
   
   
   
   for(int i=0;i<n;i++){
   
    //添加str1
   
   Label label=new Label(0,i,str[i]); 
   //将定义好的单元格添加到工作表中 
   sheet.addCell(label); 
   
   
   }
   
   
   
 
   //写入数据并关闭文件 
   book.write(); 
   book.close(); //最好在finally中关闭,此处仅作为示例不太规范
   }
  


}



0 0
原创粉丝点击