《程序实现》从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' ...
随机推荐
- 在Linux和Windows系统中输出目录结构
前言 一直以来就想在写文章时,能以文本形式(而不是截图)附上项目的目录结构,今天终于知道怎么操作了,在这分享一下. Linux 首先说下Linux上输出目录结构的方法. yum安装tree 需要支持t ...
- 从MYSQL的ibtmp1文件太大说起
1. 啥情况呀 测试环境机器磁盘空间不足的告警打破了下午的沉寂,一群人开始忙活着删数据.但是,不久前刚清理了一波数据,测试环境在没做压测的情况下不至于短短一个月不到就涨了200G数据,于是,我悄悄的 ...
- 服务注册组件——Eureka高可用集群搭建
服务注册组件--Eureka高可用集群搭建 什么是Eureka? 服务注册组件:将微服务注册到Eureka中. 为什么需要服务注册? 微服务开发重点在一个"微"字,大型应用拆分成微 ...
- Hive 系列(四)—— Hive 常用 DDL 操作
一.Database 1.1 查看数据列表 show databases; 1.2 使用数据库 USE database_name; 1.3 新建数据库 语法: CREATE (DATABASE|SC ...
- 高德全链路压测平台TestPG的架构与实践
导读 2018年十一当天,高德DAU突破一个亿,不断增长的日活带来喜悦的同时,也给支撑高德业务的技术人带来了挑战.如何保障系统的稳定性,如何保证系统能持续的为用户提供可靠的服务?是所有高德技术人面临的 ...
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...
- python paramiko外部传参和内部调用命令的方法
学习了很久的python,但在工作中使用的时候,却发现不知道怎么传参进入到python中执行,所以这两天就研究 了python args怎么将外部参数传入到python中执行 1.首先使用python ...
- 谈谈JVM垃圾回收
概述 Java运行时区域中,程序计数器,虚拟机栈,本地方法栈三个区域随着线程的而生,随线程而死,这几个区域的内存分配和回收都具备确定性,不需要过多考虑回收问题.而Java堆和方法区则不一样,一个接口的 ...
- 二阶段js 入门知识点 自我总结复习
二阶段自我总复习 1.javascript基础 : 客户端 安全性 跨平台 脚本语言 三大结构: 顺序 .选择.循环 顺序:运算符和表达式 ...
- input 上传图片
<!--多图上传--><input name="image_mortgage_property[]" type="file" multiple ...