1 读取一个txt文件,这个文件中有18万笔数据;
2 文件中的每一行内容为: 身份证号码|姓名;
比如:
432323197709242312|曹辉
3 要求每200行写入一个excel文件,并且excel有格式要求。
我是采用jxl.jar包来写入excel文件的,因为听说这个包对中文的支持挺好的。
我写的程序效率比较低,但是实现了需求中的功能。
开始测试了好几次都报错,数组越界,结果是由于数据中有的数据有问题,
有的行不是按照规定的格式来显示的,有的行只有一个 ?号,或者该行为乱码,
或者该行没有 | 分隔符。
我写的JAVA程序如下:
package com.xjh.util; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class CreateExcel5 { public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); print(sdf.format(new Date())); for (int k = 1; k < 911; k++) { BufferedReader in = new BufferedReader(new FileReader("c:\\wq_1.txt")); WritableWorkbook book = Workbook.createWorkbook(new File("c:\\test" + k + ".xls")); WritableSheet sheet = book.createSheet("xiejiaohui" + k, 0); insertHead(book, sheet); String s; int i = 0; while ((s = in.readLine()) != null) { // if ( "".equals(s.trim()) || s.length() < 10) { // if (s.length() < 10) { // print(i + " xiejiaohui"); // if (s.length() > 0) { // print(i + " xiejiaohui" + "[" + (200 * (k - 1)) + "]" + " " + "[" + (200 * k) + "] " + s); // } // continue; // } if (!s.contains("|")) { if (s.length() > 0) { print(i + " xiejiaohui" + "[" + (200 * (k - 1)) + "]" + " " + "[" + (200 * k) + "] " + s); } continue; } // if (k > 149) { // print(i + " xiejiaohui" + "[" + (200 * (k - 1)) + "]" + " " + "[" + (200 * k) + "] " + s); // } if (i >= (200 * (k - 1)) && i < (200 * k)) { String[] s2 = s.split("\\|"); sheet.addCell(new jxl.write.Number(0, i + 3 - (200 * (k - 1)), i + 1 - (200 * (k - 1)))); sheet.addCell(new Label(1, i + 3 - (200 * (k - 1)), s2[0])); sheet.addCell(new Label(2, i + 3 - (200 * (k - 1)), s2[1])); // print("i=" + i + " k=" + k + " " + s2[0] + " " + s2[1]); } else { } i++; } book.write(); book.close(); in.close(); } print(sdf.format(new Date())); } // 写表头 public static void insertHead(WritableWorkbook book, WritableSheet sheet) throws Exception { Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String gmxx = "公民信息"; String zbsj = "制表时间 " + sdf.format(now); String xh = "序号"; String zjh = "证件号"; String xm = "姓名"; sheet.addCell(new Label(0, 0, gmxx)); sheet.addCell(new Label(0, 1, zbsj)); sheet.addCell(new Label(0, 2, xh)); sheet.addCell(new Label(1, 2, zjh)); sheet.addCell(new Label(2, 2, xm)); } public static void print(String s) { System.out.println(s); } public static void print(int i) { System.out.println(i); } }