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;
}
}
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;
}
}
}
/*
*/