package com.zyq.read;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class CreateExcelForDD {
public static void main(String[] args) {
/*
* 1.打开文件
* 2.生成名为“第一页”的工作表,参数0表示这是第一页
* 3.读取"客户资料.txt"文件内容
* 4.读取的每行字符串
* 5.根据~分割字符串
* 6.将分割后的字符子串放到aList中
* 7.将aList放到大的arrayList中
* 8.遍历arrayList,将其中的数据写入到xls表中
* 9.将数据写到j列i行
*/
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File("F:\\客户资料.xls"));// 1
WritableSheet sheet = book.createSheet("第一页", 0); // 2
ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
try {
String eccoding = "GBK";
File file = new File("F:\\客户资料.txt");// 3
if (file.isFile() && file.exists()) {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), eccoding);
BufferedReader bufferedReader = new BufferedReader(reader);
String lineTxt = null;// 4
while ((lineTxt = bufferedReader.readLine()) != null) {
ArrayList<String> aList = new ArrayList<>();
String[] strings = lineTxt.split("~");// 5
for (int i = 0; i < strings.length; i++) {
aList.add(strings[i]);// 6
}
arrayList.add(aList);// 7
}
} else
System.out.println("file is not exist or file is not a file");
} catch (Exception e) {
System.out.println(e);
}
for (int i = 0; i < arrayList.size(); i++) {// 8
ArrayList<String> aList2 = arrayList.get(i);
for (int j = 0; j < aList2.size(); j++) {
Label label = new Label(j, i, aList2.get(j));// 9
sheet.addCell(label);
}
}
book.write();
} catch (Exception e) {
System.out.println(e);
} finally {
try {
book.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class CreateExcelForDD {
public static void main(String[] args) {
/*
* 1.打开文件
* 2.生成名为“第一页”的工作表,参数0表示这是第一页
* 3.读取"客户资料.txt"文件内容
* 4.读取的每行字符串
* 5.根据~分割字符串
* 6.将分割后的字符子串放到aList中
* 7.将aList放到大的arrayList中
* 8.遍历arrayList,将其中的数据写入到xls表中
* 9.将数据写到j列i行
*/
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File("F:\\客户资料.xls"));// 1
WritableSheet sheet = book.createSheet("第一页", 0); // 2
ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
try {
String eccoding = "GBK";
File file = new File("F:\\客户资料.txt");// 3
if (file.isFile() && file.exists()) {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), eccoding);
BufferedReader bufferedReader = new BufferedReader(reader);
String lineTxt = null;// 4
while ((lineTxt = bufferedReader.readLine()) != null) {
ArrayList<String> aList = new ArrayList<>();
String[] strings = lineTxt.split("~");// 5
for (int i = 0; i < strings.length; i++) {
aList.add(strings[i]);// 6
}
arrayList.add(aList);// 7
}
} else
System.out.println("file is not exist or file is not a file");
} catch (Exception e) {
System.out.println(e);
}
for (int i = 0; i < arrayList.size(); i++) {// 8
ArrayList<String> aList2 = arrayList.get(i);
for (int j = 0; j < aList2.size(); j++) {
Label label = new Label(j, i, aList2.get(j));// 9
sheet.addCell(label);
}
}
book.write();
} catch (Exception e) {
System.out.println(e);
} finally {
try {
book.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}