直接上码

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里,省去了要花费大量时间的人工输入。

《程序实现》从xml、txt文件里读取数据写入excel表格的更多相关文章

  1. c++学习笔记—c++对txt文件的读取与写入

    一.文件的输入输出 头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据.ofstream向一个给定文件写入数据.fstream读写给定数据.这些类型与cin和cout的 ...

  2. 从txt文件中读取数据放在二维数组中

    1.我D盘中的test.txt文件内的内容是这样的,也是随机产生的二维数组 /test.txt/ 5.440000 3.4500006.610000 6.0400008.900000 3.030000 ...

  3. JAVA读取、写入Excel表格(含03版)

    引言 工作中可能会遇到对Excel读取和写入,如果我们自己手动写的话,会很麻烦,但是Apache中有poi工具类.poi工具类封装好了对于Excel读取和写入,我们需要用的时候,直接调用该方法就好了. ...

  4. c++对txt文件的读取与写入

    转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...

  5. c# txt文件的读取和写入

    我们在工程实践中经常要处理传感器采集的数据,有时候要把这些数据记录下来,有时候也需要把记录下来的数据读取到项目中.接下来我们用C#演示如何对txt文件进行读写操作.我们要用到StreamReader  ...

  6. C# txt文件的读取与写入

    C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...

  7. Python学习笔记_从CSV读取数据写入Excel文件中

    本示例特点: 1.读取CSV,写入Excel 2.读取CSV里具体行.具体列,具体行列的值 一.系统环境 1. OS:Win10 64位英文版 2. Python 3.7 3. 使用第三方库:csv. ...

  8. 玩转excel===Excel处理txt文件中的数据,Excel中的分列处理

    我的txt文件数据是这样的,目标是用第一列的数据生成图表: 现在我需要拿到pss列,用Excel的操作如下,先用Excel打开txt文档 所有数据都在A列,单独拿出来第一列数字.这时候要选择分列: o ...

  9. python读取数据写入excel

    '''写入excel文件''' import xlsxwriter # todo 创建excel文件 xl = xlsxwriter.Workbook(r'D:\testfile\test.xlsx' ...

随机推荐

  1. 给你的SpringBoot做埋点监控--JVM应用度量框架Micrometer

    JVM应用度量框架Micrometer实战 前提 spring-actuator做度量统计收集,使用Prometheus(普罗米修斯)进行数据收集,Grafana(增强ui)进行数据展示,用于监控生成 ...

  2. linux100day(day3)--常用文本处理命令和vim文本编辑器

    今天,来介绍几个常用文本处理命令和vim文本编辑器 day3--常用文本处理命令和vim文本编辑器 col,用于过滤控制字符,-b过滤掉所有控制字符,这个命令并不常用,但可以使用man 命令名| co ...

  3. 从Dictionary源码看哈希表

    一.基本概念 哈希:哈希是一种查找算法,在关键字和元素的存储地址之间建立一个确定的对应关系,每个关键字对应唯一的存储地址,这些存储地址构成了有限.连续的存储地址. 哈希函数:在关键字和元素的存储地址之 ...

  4. .netcore持续集成测试篇之 .net core 2.1项目集成测试

    系列目录 从.net到.net core以后,微软非常努力,以每年一到两个大版本的频率在演进.net core,去年相继发布了.net core 2.1和2.2,其中2.1是长期支持版,不断的快速更新 ...

  5. linux+jenkins+python+svn 自动化测试集成之路

    本文背景: 背景1---个人基础: 本机win7上安装pycharm,使用python搭建API自动化测试框架,本地运行Pass.本机上搭建jenkins,创建测试任务,定时构建Pass. 背景2-- ...

  6. 二.安全NA之ASA基础

    一.ASA常用命令 show run interface #查看接口配置 show ip address #查看IP地址 show conn #查看防火墙状态信息,U代表up:I,代表进流量:O,代表 ...

  7. 关于Python虚拟环境与包管理你应该知道的事

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  8. Day 05--最终修改(一)

    1.今天大家集中交流了自己的的课程设计雏形,我们感到收获颇丰.在感慨别的同学设计的别出心裁的同时,我们也反思了自己的模型.通过与老师的讨论我们决定修改已有的首页格式,以及丰富用户的身份:即小程序不仅面 ...

  9. UVA10831题解

    Gerg's Cake Gerg is having a party, and he has invited his friends. p of them have arrived already, ...

  10. 快应用list组件 scrollTo 方法的调用方式

    例如,滚动到list 的第4个list-item: this.$element('alist').scrollTo({index:3})