将文本文件写入excel

package com.lgg.textChangeToExcel;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;


import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class Test {
public static void readFileByLines(String fileNmae) throws IOException,
RowsExceededException, WriteException {
// 打开文件
// WritableWorkbook book = Workbook.createWorkbook(new
// File("D://data3.xls"));
WritableWorkbook book = Workbook.createWorkbook(new File(
"F://test//data.xls"));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 读入txt中的内容


HashMap hm = new HashMap();
 
InputStream in = new FileInputStream("F://test//userGroup.txt");// 读取文件的数据。


// 将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
InputStreamReader isr = new InputStreamReader(in,"utf-8");// 读取
BufferedReader reader = null;
try {
reader = new BufferedReader(isr);
String tempString = null;
// 一次读入一行,直到读入文件结束
int i = 0;


while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
String[] str = tempString.split(",");
// Label[] label = null;
for (int j = 0; j < str.length; j++) {
// System.out.println(i+" "+j);
// 在Label对象的构造子中指明单元格位置是第j列第i行(j,i)以及单元格内容为str[j]
Label label = new Label(j, i, str[j].trim());
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
i++;
// 写入数据并关闭文件


// reader.close();


}
book.write();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
book.close();
} catch (WriteException e) {
e.printStackTrace();
}
if (reader != null) {
reader.close();
}
}


}


public static void main(String[] args) throws RowsExceededException,
WriteException {
try {
readFileByLines("F://test//userGroup.txt");
} catch (IOException e) {
e.printStackTrace();
}


}


class Text {
public int id;
public String phoneNum;
private String Coloums;
private String company;


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getPhoneNum() {
return phoneNum;
}


public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}


public String getColoums() {
return Coloums;
}


public void setColoums(String coloums) {
Coloums = coloums;
}


public String getCompany() {
return company;
}


public void setCompany(String company) {
this.company = company;
}


}


}




/*


*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值