《程序实现》从xml、txt文件里读取数据写入excel表格
直接上码
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表格的更多相关文章
- c++学习笔记—c++对txt文件的读取与写入
一.文件的输入输出 头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据.ofstream向一个给定文件写入数据.fstream读写给定数据.这些类型与cin和cout的 ...
- 从txt文件中读取数据放在二维数组中
1.我D盘中的test.txt文件内的内容是这样的,也是随机产生的二维数组 /test.txt/ 5.440000 3.4500006.610000 6.0400008.900000 3.030000 ...
- JAVA读取、写入Excel表格(含03版)
引言 工作中可能会遇到对Excel读取和写入,如果我们自己手动写的话,会很麻烦,但是Apache中有poi工具类.poi工具类封装好了对于Excel读取和写入,我们需要用的时候,直接调用该方法就好了. ...
- c++对txt文件的读取与写入
转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...
- c# txt文件的读取和写入
我们在工程实践中经常要处理传感器采集的数据,有时候要把这些数据记录下来,有时候也需要把记录下来的数据读取到项目中.接下来我们用C#演示如何对txt文件进行读写操作.我们要用到StreamReader ...
- C# txt文件的读取与写入
C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...
- Python学习笔记_从CSV读取数据写入Excel文件中
本示例特点: 1.读取CSV,写入Excel 2.读取CSV里具体行.具体列,具体行列的值 一.系统环境 1. OS:Win10 64位英文版 2. Python 3.7 3. 使用第三方库:csv. ...
- 玩转excel===Excel处理txt文件中的数据,Excel中的分列处理
我的txt文件数据是这样的,目标是用第一列的数据生成图表: 现在我需要拿到pss列,用Excel的操作如下,先用Excel打开txt文档 所有数据都在A列,单独拿出来第一列数字.这时候要选择分列: o ...
- python读取数据写入excel
'''写入excel文件''' import xlsxwriter # todo 创建excel文件 xl = xlsxwriter.Workbook(r'D:\testfile\test.xlsx' ...
随机推荐
- 【0728 | 预习】第三篇 Python基础
第三篇 Python基础预习 Part 1 变量 一.什么是变量? 二.为什么要有变量? 三.定义变量 四.变量的组成 五.变量名的命名规范 六.变量名的两种风格 Part 2 常量 Part 3 P ...
- p2p 打洞专场(转)
就像1000个人眼中有1000个哈姆雷特一样,每个人眼中的区块链也是不一样的!作为技术人员眼中的区块链就是将各种技术的融合,包括密码学,p2p网络,分布式共识机制以及博弈论等.我们今天就来讨论一下区块 ...
- Linux(CentOS7)下RabbitMQ下载安装教程
原文链接:http://www.studyshare.cn/software/details/1172/0 一.下载安装步骤 下载erlang 1.wget 下载地址 2.rpm -Uvh erlan ...
- Spring基础笔记
Spring带给了我们什么便利? 注解版本的IOC如何玩? 组件注册 组件注册的过程中有哪些过滤规则? 如何控制组件的作用域(单例多例)? 六种注册组件的方式? 生命周期 什么是bean的生命周期 在 ...
- 四、Ansible的Galaxy包管理器
一.什么是Ansible Galaxy? Ansible Galaxy是Ansible的第三方插件管理和安装工具,其实就是包管理软件.作用类似于Ubuntu的apt,Centos的yum,Python ...
- .NET使用Bogus生成大量随机数据
.NET如何生成大量随机数据 在演示Demo.数据库脱敏.性能测试中,有时需要生成大量随机数据.Bogus就是.NET中优秀的高性能.合理.支持多语言的随机数据生成库. Bogus的Github链接: ...
- .NET Core C#中级篇2-5 常见实用类
.NETCore CSharp 中级篇2-5 本节内容为常见实用类和方法的使用 String.Format string.format方法是一个字符串格式化类,它里面的一些写法是对字符串进行指定格式的 ...
- 2019牛客暑期多校训练营(第十场)J - Wood Processing (斜率优化DP)
>传送门< 题意 $n$个宽度为$w_{i}$,高为$h_{i}$ 的 木块,要求分成$k$组,对于每组内的所有木块,高度都变为组内最低木块的高度,宽度保持不变,求变化的最小面积. 分析 ...
- 图表控件业界革命 -Arction新产品LightningChart JS 上市
芬兰高科技企业Arction Ltd 在今年8月份推出了用于网页的数据可视化控件新解决方案—— LightningChart JS. 初次的基准测试表明,该产品为网页应用程序的数据可视化刷新了新的纪录 ...
- python+jinja2实现接口数据批量生成工具
在做接口测试的时候,我们经常会遇到一种情况就是要对接口的参数进行各种可能的校验,手动修改很麻烦,尤其是那些接口参数有几十个甚至更多的,有没有一种方法可以批量的对指定参数做生成处理呢. 答案是肯定的! ...