首发地址:http://blog.csdn.net/u014737138/article/details/38120403

不多说了 直接看代码:

下面的FileFind类首先是找到文件夹下面所有的txt文件,并且获取他们的绝对路径或者相对路径存放在数组中

public class FileFind {
@SuppressWarnings("rawtypes") /**
* 利用字符串的.endsWith()来判断后缀名
* 利用文件类的.listFiles()来获取一个文件夹下所有文件
*
* @param path
* @param data
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static List getData(String path, List data) {
try{
File f = new File(path);
if (f.isDirectory()) {
File[] fs = f.listFiles();
for (int i = ; i < fs.length; i++) {
// 如果该文件夹下面还有文件夹,那么继续往下面去找,递归
data = getData(fs[i].getPath(), data);
}
} else if (f.getName().endsWith(".txt")) {
// 匹配文本文件,*.txt
data.add(f.getName());// 得到相对路径
} }catch(Exception e){
e.printStackTrace();
}
return data;
} /**
*
* @param path
* @param data
* @return data 返回文件的绝对路径
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getFileAbsolutePath(String path, List data) {
try{
File f = new File(path);
if (f.isDirectory()) {
File[] fs = f.listFiles();
for (int i = ; i < fs.length; i++) {
// 如果该文件夹下面还有文件夹,那么继续往下面去找,递归
data = getFileAbsolutePath(fs[i].getPath(), data);
}
} else if (f.getName().endsWith(".txt")) {
// 匹配文本文件,*.txt
data.add(f.getAbsolutePath().toString());// 得到相对路径
// System.out.println(f.getAbsolutePath());// 得到绝对路径
} }catch(Exception e){
e.printStackTrace();
}
return data;
}
}

类GetStringByLocation获取文本文件内容,这个案例中只用了,readTEXT函数,按照\t读取字段内容

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class GetStringByLocation { /**
* 按照行读取文本文件的数据,每一行存入到list一维数组中
*
* @param list 用来存储读取的数据
* @param destFile 读取的文件路径
* @throws Exception 读取文件出错 抛出异常
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void readTxt(List list, String destFile) throws Exception {
try{
BufferedReader reader = new BufferedReader(new FileReader(destFile));
String line = reader.readLine(); while (line != null) {
list.add(line);
line = reader.readLine();
} reader.close();
}catch(Exception e){
e.printStackTrace();
}
} /**
*
* @param list 存放读取的结果,按照每行的数据格式 \t读取每一个字段
* @param pathName 读取的文本文件路径
* @throws Exception 抛出异常
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void readTEXT(List list,String pathName) throws Exception{
String text = null;
try
{
InputStreamReader read1 = new InputStreamReader(new FileInputStream(pathName));
BufferedReader br1 = new BufferedReader(read1);
while((text = br1.readLine())!=null)
{
/***相应操作***/
System.out.println(text);
String[] ss =text.split("\t"); for(int j=;j<ss.length;j++){
System.out.println(ss[j]);
list.add(ss[j]);
} text = br1.readLine();
}
br1.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
} /**
* 从文件读取数据
* @param path 文件路径
* @return 文件数据
*/
public static List<String> getFileData(String path)
{
List<String> result = new ArrayList<String>();
FileReader fr = null;
BufferedReader br = null; try
{
fr = new FileReader(path);
br = new BufferedReader(fr);
String str; while((str = br.readLine()) != null)
{
result.add(str);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(fr != null)
{
fr.close();
} if(br != null)
{
br.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
} return result;
}
}

最后就是写入到Excel文件中去的主函数:

public class ExcelForAlibaba {
@SuppressWarnings("rawtypes")
public static void main(String[] args)
{ String targetfile = "E:/out.xls";// 输出的excel文件名
String worksheet = "InfoList";// 输出的excel文件工作表名
String[] title = { "A", "A", "A" };// excel工作表的标题 WritableWorkbook workbook;
try {
// 创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下
// workbook = Workbook.createWorkbook(new File("output.xls")); OutputStream os = new FileOutputStream(targetfile);
workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(worksheet, ); // 添加第一个工作表 jxl.write.Label label;
System.out.println("title:"+title.length);
for (int i = ; i < title.length; i++) {
// Label(列号,行号 ,内容 )
label = new jxl.write.Label(i, , title[i]);
sheet.addCell(label);
} //添加内容:
String[] row = { "E", "E", "E" };// excel工作表的的行数据
jxl.write.Label label1;
System.out.println("row:"+row.length);
for(int i =;i<row.length;i++){
label1 = new jxl.write.Label(i, , row[i]); sheet.addCell(label1);
} try{
GetStringByLocation test = new GetStringByLocation();
FileFind filefind = new FileFind(); List absdata = new ArrayList();//存放文本文件的绝对路径 String path = "E:\\DD\DD\\test";//文件夹路径 absdata = filefind.getFileAbsolutePath(path, absdata);//获取文本文件的路径集,存放在数组中
int j=;//从第三行开始,写到Excel文件中 for (int m = ; m < absdata.size(); m++) {
//每一个文件进行操作,每一个文件里面的内容都放在list中,
List list = new ArrayList();//存放每一个文件的内容
test.readTEXT(list, absdata.get(m).toString());
for (int i = ; i < ; i++) {
//i表示第一列,第二列。。。。。。
jxl.write.Label labeltemp;
// System.out.println(list.get(i).toString());
labeltemp = new jxl.write.Label(i, j, list.get(i).toString());
sheet.addCell(labeltemp);
} j++;//从下一行开始写起
} }catch(Exception e){
e.printStackTrace();
} workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end"); } }

以上代码经过测试完全没有问题。需要导入的包自己弄,需要自己写三个包

package org.txtOperate; GetStringByLocation
package org.file.operate; FileFind
package org.excelTest;man()

需要引用的各种第三方包:在main(0)中

import jxl.*;
import jxl.write.*;
import java.io.*;
import java.io.File.*;
import java.util.*; import org.file.operate.FileFind;
import org.txtOperate.GetStringByLocation;

转载请注明,交流请联系nlp30508@qq.com

批量处理txt文本文件到Excel文件中去----java的更多相关文章

  1. Java读取、写入、处理Excel文件中的数据(转载)

    原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...

  2. [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. Python:将爬取的网页数据写入Excel文件中

    Python:将爬取的网页数据写入Excel文件中 通过网络爬虫爬取信息后,我们一般是将内容存入txt文件或者数据库中,也可以写入Excel文件中,这里介绍关于使用Excel文件保存爬取到的网页数据的 ...

  4. 条形码的应用三-----------从Excel文件中读取条形码

    条形码的应用三------从Excel文件中读取条形码 介绍 上一篇文章,我向大家展示了生成多个条形码并存储到Excel文件中的一个方法.后来我又有了个想法:既然条码插入到excel中了,我可不可以从 ...

  5. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  6. C#可以获取Excel文件中Sheet的名字

    C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619)  14:13: ...

  7. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

    在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...

  8. 读取Excel文件中的单元格的内容和颜色

    怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...

  9. 从Excel文件中读取内容

    从Excel文件中读取内容 global::System.Web.HttpPostedFileBase file = Request.Files["txtFile"]; strin ...

随机推荐

  1. python基础语法20 面向对象5 exec内置函数的补充,元类,属性查找顺序

    exec内置函数的补充 exec: 是一个python内置函数,可以将字符串的代码添加到名称空间中; - 全局名称空间 - 局部名称空间 exec(字符串形式的代码, 全局名称空间, 局部名称空间) ...

  2. 怎么删掉xampp文件夹

    删掉xampp文件夹时,提示:操作无法完成,因为其中的文件夹或文件已在另一程序中打开 具体的解决方法: 菜单栏输入:服务 找到apachezt和mysqlzt,并禁用    -- 因为之前打开Zent ...

  3. Vue.js如何获得兄弟元素,子元素,父元素(DOM操作)

    我不是代码的生产者,我只是知识的搬运工. 戳这

  4. Computer Network Chapter4 solution

    1.以太网使用曼彻斯特编码,效率50% 2.侦听信道时间:来回延时时间(10usec):发送数据(25.6usec): 3.单向时延t=S(距离)/V(电缆传输速率):最小帧长=2*t*C(数据传输速 ...

  5. linux在目录下查字符串, 查文件数目

    在目录下所有文件中查找某个字符串(递归查) grep -rnl '字符串' 目录名 统计一个文件夹下目录数或文件数, 如下分步讲解: 1. 显示目录下内容: ls -l                ...

  6. Centos7下搭建NFS服务器与连接详解

    一,环境介绍    本实验使用了两台centos7虚拟机,其中         服务器:192.168.1.188    客户端:192.168.1.189 二,实验步骤    192.168.1.1 ...

  7. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  8. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  9. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  10. C++ 10进制, 16进制, ASCII码, 单字节与多字节的相互转换

    这些简单的转换是用的比较频繁的, 因此将这些功能全部封装在一个类中 头文件 #pragma once #include <stdlib.h> #include <string> ...