《程序实现》从xml、txt文件里读取数据写入excel表格

时间:2022-01-04 15:03:39

直接上码

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=;
int b=;
int countTemp=;
int row=;
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", );
String th[] = { "弹幕ID", "用户ID", "弹幕发送时间", "字体颜色", "字号", "弹幕类型", "弹幕字数" };
WritableFont contentFont = new WritableFont(WritableFont.createFont("楷体 _GB2312"), , WritableFont.NO_BOLD);
WritableCellFormat contentFormat = new WritableCellFormat(contentFont);
for (int i = ; i < ; i++) {
ws.addCell(new Label(i, , 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=;
b=;
countTemp=;
String temp1=null;
String[] th1=new String[];
//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=;i<temp1.length();i++){
if(temp1.charAt(i)==','){
countTemp++;
}
if(countTemp== && a==){
a=i;
}
if(countTemp==){
b=i;
break;
}
}
//System.out.println(a);
//System.out.println(b);
th1[]=temp1.substring(a+, b);
//计算弹幕发送时间
for(int i=;i<temp1.length();i++){
if(temp1.charAt(i)=='"'){
a=i;
}
if(temp1.charAt(i)==','){
b=i;
break;
}
}
th1[]=temp1.substring(a+, b);
//计算字体颜色
a=;
b=;
countTemp=;
for(int i=;i<temp1.length();i++){
if(temp1.charAt(i)==','){
countTemp++;
}
if(countTemp== && a==){
a=i;
}
if(countTemp==){
b=i;
break;
}
}
th1[]=temp1.substring(a+, b);
//计算字号
a=;
b=;
countTemp=;
for(int i=;i<temp1.length();i++){
if(temp1.charAt(i)==','){
countTemp++;
}
if(countTemp== && a==){
a=i;
}
if(countTemp==){
b=i;
break;
}
}
if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="非常小";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="特小";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="小";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="中";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="大";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="很大";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="特别大";
}
//计算弹幕类型
a=;
b=;
countTemp=;
for(int i=;i<temp1.length();i++){
if(temp1.charAt(i)==','){
countTemp++;
}
if(countTemp== && a==){
a=i;
}
if(countTemp==){
b=i;
break;
}
}
if(Integer.parseInt(temp1.substring(a+, b))==||Integer.parseInt(temp1.substring(a+, b))==||Integer.parseInt(temp1.substring(a+, b))==){
th1[]="滚动弹幕";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="底端弹幕";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="顶端弹幕";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="逆向弹幕";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="精准定位";
}else if(Integer.parseInt(temp1.substring(a+, b))==){
th1[]="高级弹幕";
}
//计算弹幕字数
for(int i=;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[]=String.valueOf((b-a)/+);//字数存在问题 ws.addCell(new Label(, row, new Integer(row).toString(), contentFormat));
for (int i = ; i < ; i++) {
ws.addCell(new Label(i+, 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里,省去了要花费大量时间的人工输入。