## Java简单工具---读取TXT文件根据需要筛选排序生成excel文件

Java简单工具—读取TXT文件根据需要筛选排序生成excel文件

详细代码:

	package web;
	
	import java.io.BufferedReader;
	import java.io.File;
	import java.io.FileInputStream;
	import java.io.FileNotFoundException;
	import java.io.IOException;
	import java.io.InputStreamReader;
	import java.io.UnsupportedEncodingException;
	
	import jxl.Workbook;
	import jxl.WorkbookSettings;
	import jxl.write.Label;
	import jxl.write.WritableSheet;
	import jxl.write.WritableWorkbook;
	import jxl.write.WriteException;
	import jxl.write.biff.RowsExceededException;
	
	public class txt2excel {
	    public static void main(String[] args) {
	
	        File file = new File("C:\\Users\\Administrator\\Desktop\\1.txt");// 将读取的txt文件
	        File file2 = new File("C:\\Users\\Administrator\\Desktop\\work.xls");// 将生成的excel表格
	
	        if (file.exists() && file.isFile()) {
	
	            InputStreamReader read = null;
	            String line = "";
	            BufferedReader input = null;
	            WritableWorkbook wbook = null;
	            WritableSheet sheet;
	
	            try {
	                
	                read = new InputStreamReader(new FileInputStream(file), "GBK");
	                input = new BufferedReader(read);
	
	                wbook = Workbook.createWorkbook(file2);// 根据路径生成excel文件
	                sheet = wbook.createSheet("first", 0);// 新标签页
	
	/*                try {
	                    Label company = new Label(0, 0, "语言");// 如下皆为列名
	                    sheet.addCell(company);
	                    Label position = new Label(1, 0, "数量");
	                    sheet.addCell(position);
	                    Label salary = new Label(2, 0, "备注");
	                    sheet.addCell(salary);
	            //        Label status = new Label(3, 0, "Bonded No.");
	             //       sheet.addCell(status);
	                } catch (RowsExceededException e) {
	                    e.printStackTrace();
	                } catch (WriteException e) {
	                    e.printStackTrace();
	                }*/
	
	                int m = 0;// excel行数
	                int n = 0;// excel列数
	                Label t;
	                while ((line = input.readLine()) != null) {
	                    String[] words = line.split("[ ,\"\t:]");// 把读出来的这行根据空格或tab分割开
	                   // System.out.println(words.toString());
	
	                    for (int i = 0; i < words.length; i++) {
	                        if (!words[i].matches("\\s*")) { // 当不是空行时
	                        	System.out.println(words[i].trim());
	                        		t = new Label(n, m, words[i].trim());
	                                sheet.addCell(t);
	                   //             n++;
	                                m++;
	                        }
	                    }
	                 //   n = 0;// 回到列头部
	                   // m++;// 向下移动一行
	                }
	            } catch (UnsupportedEncodingException e) {
	                e.printStackTrace();
	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	            } catch (IOException e) {
	                e.printStackTrace();
	            } catch (RowsExceededException e) {
	                e.printStackTrace();
	            } catch (WriteException e) {
	                e.printStackTrace();
	            } finally {
	                try {
	                    wbook.write();
	                    wbook.close();
	                    input.close();
	                    read.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                } catch (WriteException e) {
	                    e.printStackTrace();
	                }
	            }
	            System.out.println("over!");
	            try {
					Runtime.getRuntime().exec("cmd /c start C:\\Users\\Administrator\\Desktop\\work.xls");
				} catch (IOException e) {
					e.printStackTrace();
				}
	            System.exit(0);
	        } else {
	            System.out.println("file is not exists or not a file");
	            System.exit(0);
	        }
	    }
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值