直接上码
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import java.util.regex.*; import javax.swing.JFileChooser; public class main { /** * @param args */ public static void main(String[] args) { int a=0; int b=0; int countTemp=0; int row=1; String temp=null; try { WritableWorkbook wwb = null; //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 wwb = Workbook.createWorkbook(new File("C:/Users/Administrator/Desktop/result.xls")); WritableSheet ws = wwb.createSheet("Sheet 1", 0); String th[] = { "弹幕ID", "用户ID", "弹幕发送时间", "字体颜色", "字号", "弹幕类型", "弹幕字数" }; WritableFont contentFont = new WritableFont(WritableFont.createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD); WritableCellFormat contentFormat = new WritableCellFormat(contentFont); for (int i = 0; i < 7; i++) { ws.addCell(new Label(i, 0, th[i], contentFormat)); } JFileChooser jfc1=new JFileChooser("d:/"); jfc1.showOpenDialog(null); File sf1=jfc1.getSelectedFile(); String readFile=sf1.getAbsolutePath(); //String readFile="C:/Users/Administrator/Desktop/first.xml"; BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(readFile))); while((temp=br.readLine())!=null){ a=0; b=0; countTemp=0; String temp1=null; String[] th1=new String[6]; //System.out.println(temp); String regEx = "\<d.*\<\/d\>"; Pattern pat = Pattern.compile(regEx); Matcher mat = pat.matcher(temp); if(mat.find()){ temp1=mat.group(); //System.out.println(temp1); }else{ continue; } //计算用户ID for(int i=0;i<temp1.length();i++){ if(temp1.charAt(i)==','){ countTemp++; } if(countTemp==6 && a==0){ a=i; } if(countTemp==7){ b=i; break; } } //System.out.println(a); //System.out.println(b); th1[0]=temp1.substring(a+1, b); //计算弹幕发送时间 for(int i=0;i<temp1.length();i++){ if(temp1.charAt(i)=='"'){ a=i; } if(temp1.charAt(i)==','){ b=i; break; } } th1[1]=temp1.substring(a+1, b); //计算字体颜色 a=0; b=0; countTemp=0; for(int i=0;i<temp1.length();i++){ if(temp1.charAt(i)==','){ countTemp++; } if(countTemp==3 && a==0){ a=i; } if(countTemp==4){ b=i; break; } } th1[2]=temp1.substring(a+1, b); //计算字号 a=0; b=0; countTemp=0; for(int i=0;i<temp1.length();i++){ if(temp1.charAt(i)==','){ countTemp++; } if(countTemp==2 && a==0){ a=i; } if(countTemp==3){ b=i; break; } } if(Integer.parseInt(temp1.substring(a+1, b))==12){ th1[3]="非常小"; }else if(Integer.parseInt(temp1.substring(a+1, b))==16){ th1[3]="特小"; }else if(Integer.parseInt(temp1.substring(a+1, b))==18){ th1[3]="小"; }else if(Integer.parseInt(temp1.substring(a+1, b))==25){ th1[3]="中"; }else if(Integer.parseInt(temp1.substring(a+1, b))==36){ th1[3]="大"; }else if(Integer.parseInt(temp1.substring(a+1, b))==45){ th1[3]="很大"; }else if(Integer.parseInt(temp1.substring(a+1, b))==64){ th1[3]="特别大"; } //计算弹幕类型 a=0; b=0; countTemp=0; for(int i=0;i<temp1.length();i++){ if(temp1.charAt(i)==','){ countTemp++; } if(countTemp==1 && a==0){ a=i; } if(countTemp==2){ b=i; break; } } if(Integer.parseInt(temp1.substring(a+1, b))==1||Integer.parseInt(temp1.substring(a+1, b))==2||Integer.parseInt(temp1.substring(a+1, b))==3){ th1[4]="滚动弹幕"; }else if(Integer.parseInt(temp1.substring(a+1, b))==4){ th1[4]="底端弹幕"; }else if(Integer.parseInt(temp1.substring(a+1, b))==5){ th1[4]="顶端弹幕"; }else if(Integer.parseInt(temp1.substring(a+1, b))==6){ th1[4]="逆向弹幕"; }else if(Integer.parseInt(temp1.substring(a+1, b))==7){ th1[4]="精准定位"; }else if(Integer.parseInt(temp1.substring(a+1, b))==8){ th1[4]="高级弹幕"; } //计算弹幕字数 for(int i=1;i<temp1.length();i++){ if(temp1.charAt(i)=='>'){ a=i; } if(temp1.charAt(i)=='<'){ b=i; break; } } //System.out.println(a); //System.out.println(b); th1[5]=String.valueOf((b-a)/2+1);//字数存在问题 ws.addCell(new Label(0, row, new Integer(row).toString(), contentFormat)); for (int i = 0; i < 6; i++) { ws.addCell(new Label(i+1, row, th1[i], contentFormat)); } row++; } wwb.write(); wwb.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
使用javax.swing.JFileChooser出现访问限制错误,
Access restriction: The type JFileChooser is not accessible due to restriction on required library
解决办法:Project->Properties->Java Compiler->Errors/Warnings->Deprecated and restricted API->Forbidden reference改为warning,这样便可以用了。
程序的功能就是选择文件进行读取数据,将有效数据写入excel里,省去了要花费大量时间的人工输入。