检查文件中的内容并记录excel

用到的工具包 jxl.jar    

可以在http://download.csdn.net/detail/jtl913/6339245这里下载得到

 

package com.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class CheckPsubBX {
 private   long count=0;
 public int rownum=0; //行数
 public File excel=null;// excel文件
 public WritableSheet theSheet=null;//当前sheet
 private WritableWorkbook wwb=null;//excel
 /**
  *
  * @param args
  */
 
 static String sysName="";
 
 public static void main(String[] args) throws Exception {
  System.out.println("start check......");
  CheckPsubBX cl=new CheckPsubBX();
  cl.CheckPsub("E:\\XXX\\xxx\\xxx", "总文件夹名");
  System.out.println("end check!");
 }
 
 public  void CheckPsub(String sourcePath , String sysname) throws Exception{
  createFile("D:\\psub检查结果.xls");
  createPsubSheet(2);
  
  sysName=sysname;
        String fileName = sourcePath;
        File newFile = new File(fileName);
        File subFiles[]=newFile.listFiles();
        String [] cols={"列标题1","列标题2","列标题3","列标题4"};
        WritableSheet todaySheet = setTitleAndColumn( "",cols );
        FileList(subFiles);
        Label finalresult=new Label(0, rownum+1, "检测出文件中包含XX的记录数为:"+count);
        todaySheet.addCell(finalresult);
       
        saveExcel();
 }
 

 private   void FileList(File file[]){
  File ff=null;
  for(int i=0;i<file.length;i++){
   ff=file[i];
   if(ff.isDirectory()&&ff.listFiles().length>0){
    if(ff.getName().toUpperCase().equals("XXX")){continue; }
    
    File files[]=ff.listFiles();
    FileList(files);
   }else{
     readFileByLines(ff.getPath());
   }
  }
  
 }
 private   void readFileByLines(String fileName){
        File file = new File(fileName);
      
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line=0;
            boolean iszhushi=false;
            while ((tempString = reader.readLine()) != null) {         
             line++;
             if(iszhushi&&!Pattern.compile(".*?(\\*/|-->)"+"[如果是注释块并并且不是注释结尾则过滤掉此行
              continue;
             }
             //如果此行是以/*,<!--开头的则认为是注释开始并且也是已*/,-->结尾的语句则过滤掉此行
             if(Pattern.compile("[
\\t\\n\\x0B\\f\\r]*(/\\*|<!--).*?").matcher(tempString).matches()&& Pattern.compile(".*?(\\*/|-->)"+"[\\t\\n\\x0B\\f\\r]*").matcher(tempString).matches()){
              continue;
             }else if(Pattern.compile("[ 如果此行是以/*,<!--开头的则认为是注释开始语句 注释变量为true
              iszhushi=true;
              continue;
             }
             if(Pattern.compile(".*?(
\\*/|-->)"+"[如果此行是以*/ ,-->结尾的认为是注释结尾
              iszhushi=false;
              continue;
             }
             if(Pattern.compile("[
如果是反斜线或者'开头的就直接过滤
              continue;
             }
             if(tempString.toUpperCase().indexOf("PSUB.SENDSTRINGTOSERVER")>-1){
        String[] cols={sysName,"XXX",fileName,String.valueOf(line)};
        try{
         WriteLine(cols);
                         }catch(Exception e){
                          e.printStackTrace();
                         }
        count++;
             }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
 }
 //=======================创建excel=========================
 /**
  * 删除以前的文件 并赋值excel
  * */
 public void createFile(String fileName){
  excel=new File(fileName);
  if(excel.exists()){
   excel.delete();
  }
 }
 
 public void createPsubSheet(int theSheetNum) throws IOException{
  wwb = Workbook.createWorkbook(excel);
  theSheet=wwb.createSheet(sysName, wwb.getNumberOfSheets());
  theSheet.setColumnView(0,30);
  theSheet.setColumnView(1,10);
  theSheet.setColumnView(2,100);
  theSheet.setColumnView(3,50);
 
 
}

/**
 * 设置sheet标题以及列名
 * @throws WriteException
 * @throws RowsExceededException
 * */
public WritableSheet  setTitleAndColumn( String title,String[] cols ) throws RowsExceededException, WriteException{
 boolean hasTitle=true;
 int colBegin=0;
 WritableCellFormat [] format=formatList();
 //判断是否有titile,没有titile下面的列就可以占据第一行了
 if(title==null||title.equals("")){
  hasTitle=false;
 }else{
   Label titleLab=new Label(0, 0, title);
   titleLab.setCellFormat(format[0]);
   theSheet.addCell(titleLab);
   theSheet.mergeCells(0, 0, cols.length-1, 0);
 }
 if(hasTitle){
  colBegin=1;
  rownum=2;
 }else{
  colBegin=0;
  rownum=1;
 }
 //将列名灌入
 for(int i=0;i<cols.length;i++){
   Label colLab=new Label(i, colBegin, cols[i]);
   colLab.setCellFormat(format[1]);
   theSheet.addCell(colLab);
 }
 
 return theSheet;
}

/**
 * 灌入内容
 * @throws WriteException
 * @throws RowsExceededException
 * */
public WritableSheet WriteLine(String[] cols ) throws RowsExceededException, WriteException{
 WritableCellFormat [] format=formatList();
   for(int i=0;i<cols.length;i++){
     Label colLab=new Label(i, rownum, cols[i]);
     colLab.setCellFormat(format[2]);
     theSheet.addCell(colLab);
   }
   rownum++;
 return theSheet;
}

 

/**单元格格式集合
 * 1.title表名小三粗体
 * 2.columnName列名粗体,底纹灰色
 * 3.autoWrap自动换行的单元格
 * */
private static WritableCellFormat [] formatList(){
  WritableCellFormat [] formats=new WritableCellFormat[3];
  try {
 WritableFont wf1=new WritableFont(WritableFont.ARIAL, 22,WritableFont.BOLD, false);
 WritableCellFormat title=new WritableCellFormat(wf1);
 title.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);  //边框样式
 title.setAlignment(Alignment.CENTRE);
 title.setVerticalAlignment(VerticalAlignment.CENTRE);
 
 WritableFont wf2=new WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD, false);
 WritableCellFormat columnName=new WritableCellFormat(wf2);
 columnName.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);  //边框样式
 columnName.setAlignment(Alignment.CENTRE);
 Colour c=Colour.GRAY_25;
 columnName.setBackground(c);
 columnName.setVerticalAlignment(VerticalAlignment.CENTRE);
 
 WritableFont wf3=new WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD, false);
 WritableCellFormat autoWrap=new WritableCellFormat(wf3);
 autoWrap.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);  //边框样式
 autoWrap.setVerticalAlignment(VerticalAlignment.CENTRE);
 autoWrap.setWrap(true);
 formats[0]=title;
 formats[1]=columnName;
 formats[2]=autoWrap;
 } catch (WriteException e) {
 }
 
 
 return formats;
}

/**
 * 保存excel
 * @throws IOException
 * @throws WriteException
 * */
public void saveExcel() throws IOException, WriteException{
    wwb.write();
  wwb.close();
}

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值