直接上碼
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里,省去了要花費大量時間的人工輸入。