Java 从txt读取到Excel

 

需要导入jxl包,下载路径:

  1 package test;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileNotFoundException;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.io.UnsupportedEncodingException;
 10 
 11 import jxl.Workbook;
 12 import jxl.write.Label;
 13 import jxl.write.WritableSheet;
 14 import jxl.write.WritableWorkbook;
 15 import jxl.write.WriteException;
 16 import jxl.write.biff.RowsExceededException;
 17 
 18 /**
 19  *
 20  */
 21 public class txt2excel {
 22     /**
 23      *
 24      * @param args
 25      */
 26     public static void main(String[] args) {
 27 
 28         File file = new File("C:\\Users\\12771\\Desktop\\bbb.txt");// 将读取的txt文件
 29         File file2 = new File("C:\\Users\\12771\\Desktop\\work.xls");// 将生成的excel表格
 30 
 31         if (file.exists() && file.isFile()) {
 32 
 33             InputStreamReader read = null;
 34             String line = "";
 35             BufferedReader input = null;
 36             WritableWorkbook wbook = null;
 37             WritableSheet sheet;
 38 
 39             try {
 40                 read = new InputStreamReader(new FileInputStream(file), "UTF-8");
 41                 input = new BufferedReader(read);
 42 
 43                 wbook = Workbook.createWorkbook(file2);// 根据路径生成excel文件
 44                 sheet = wbook.createSheet("first", 0);// 新标签页
 45 
 46                 try {
 47                     Label sName = new Label(0, 0, "推文标题");// 如下皆为列名
 48                     sheet.addCell(sName);
 49                     Label company = new Label(0, 0, "送达人数");// 如下皆为列名
 50                     sheet.addCell(company);
 51                     Label position = new Label(1, 0, "时间");
 52                     sheet.addCell(position);
 53                     Label salary = new Label(2, 0, "阅读人数");
 54                     sheet.addCell(salary);
 55                     Label status = new Label(3, 0, "分享人数");
 56                     sheet.addCell(status);
 57                 } catch (RowsExceededException e) {
 58                     e.printStackTrace();
 59                 } catch (WriteException e) {
 60                     e.printStackTrace();
 61                 }
 62                 /**
 63                  *
 64                  */
 65                 int m = 1;// excel行数
 66                 int n = 0;// excel列数
 67                 int count = 0;
 68                 Label t;
 69                 while ((line = input.readLine()) != null) {
 70                     /**
 71                     String[] words = line.split("[ \t]");// 把读出来的这行根据空格或tab分割开
 72                     for (int i = 0; i < words.length; i++) {
 73                         if (!words[i].matches("\\s*")) { // 当不是空行时
 74                             t = new Label(n, m, words[i].trim());
 75                             sheet.addCell(t);
 76                             n++;
 77                         }
 78                     }
 79                     **/
 80 
 81                     String word = line;
 82                     t = new Label(n, m, word.trim());
 83                     sheet.addCell(t);
 84                     n++;
 85                     count++;
 86                     if(count == 5) {
 87                         n = 0;// 回到列头部
 88                         m++;// 向下移动一行
 89                         count = 0;
 90                     }
 91 
 92                 }
 93             } catch (UnsupportedEncodingException e) {
 94                 e.printStackTrace();
 95             } catch (FileNotFoundException e) {
 96                 e.printStackTrace();
 97             } catch (IOException e) {
 98                 e.printStackTrace();
 99             } catch (RowsExceededException e) {
100                 e.printStackTrace();
101             } catch (WriteException e) {
102                 e.printStackTrace();
103             } finally {
104                 try {
105                     wbook.write();
106                     wbook.close();
107                     input.close();
108                     read.close();
109                 } catch (IOException e) {
110                     e.printStackTrace();
111                 } catch (WriteException e) {
112                     e.printStackTrace();
113                 }
114             }
115             System.out.println("over!");
116             System.exit(0);
117         } else {
118             System.out.println("file is not exists or not a file");
119             System.exit(0);
120         }
121     }
122 }

 

posted @ 2018-05-10 09:58  听说这是最长的名字了  阅读(396)  评论(0编辑  收藏  举报