## 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);
	        }
	    }
	}

你可能感兴趣的:(java编程)