直接上码

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. Docker进阶-容器监控cAdvisor+InfluxDB+Granfana

    概述 前面文章介绍使用docker compose组合应用并利用scale快速对容器进行扩容. 由于docker compose启动的服务都在同一台宿主机上,对于一个宿主机上运行多个容器应用时,容器的 ...

  2. csdn论坛页抓取

    抓取csdn论坛 实现功能 获取论坛分类所有链接,并拼接成推荐精华页的完成的链接 获取推荐精华页的帖子状态,赏分,帖子标题,作者,发布时间,回复量,查看量,最后发表时间 置顶内容不爬取,只打印置顶内容 ...

  3. React Native 生命周期及相关方法小技巧使用

    ES6 生命周期图解 很多文章里的图解,第一步是 getDefaultProps , 第二步是 getinitialstate ,这是 ES5 的写法; 实际上ES6 中 getinitialstat ...

  4. Keras实例教程(4)之迁移学习VGG

    https://blog.csdn.net/baimafujinji/article/details/80743814

  5. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  6. Nginx入门(二):镜像和容器

    0.docker常用命令 #镜像名 版本标签 镜像id 创建时间 镜像大小 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289 ...

  7. HTML5学习手册

    目录 HTML 简介 HTML 简介 什么是 HTML? HTML 标签 HTML 文档 = 网页 基本的 HTML 标签 - 四个实例 HTML 标题 HTML 段落 HTML 链接 HTML 图像 ...

  8. effective java 3th item2:考虑 builder 模式,当构造器参数过多的时候

    yiaz 读书笔记,翻译于 effective java 3th 英文版,可能有些地方有错误.欢迎指正. 静态工厂方法和构造器都有一个限制:当有许多参数的时候,它们不能很好的扩展. 比如试想下如下场景 ...

  9. 2019 Multi-University Training Contest 1

    2019 Multi-University Training Contest 1 A. Blank upsolved by F0_0H 题意 给序列染色,使得 \([l_i,r_i]\) 区间内恰出现 ...

  10. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...