Java-读取txt生成excel

本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中

在敲本段代码的时候,也学习了一些其它知识点,如下:

1、byte[] b_charset= String.getBytes("charset");就是返回字符串在给定的charset编码格式下的表现,保存在byte[]数组里面。charset可以是utf-8,gbk,iso8859-1等

2、String s_charset = new String(b_charset,"charset");使用指定编码格式把byte[]解析成String格式。charset可以是utf-8,gbk,iso8859-1等

3、System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序

第1第2条学习来源:String的getBytes()方法

第3条学习来源:System.exit(0)和System.exit(1)区别

读取txt生成excel学习来源:Java生成和操作Excel文件

完整代码如下:

package test;

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.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\Desktop\bbb.txt");// 将读取的txt文件
        File file2 = new File("C:\Users\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, "状态");
                    sheet.addCell(status);
                } catch (RowsExceededException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }

                int m = 1;// excel行数
                int n = 0;// excel列数
                Label t;
                while ((line = input.readLine()) != null) {

                    String[] words = line.split("[ 	]");// 把读出来的这行根据空格或tab分割开

                    for (int i = 0; i < words.length; i++) {
                        if (!words[i].matches("\s*")) { // 当不是空行时
                            t = new Label(n, m, words[i].trim());
                            sheet.addCell(t);
                            n++;
                        }
                    }
                    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!");
            System.exit(0);
        } else {
            System.out.println("file is not exists or not a file");
            System.exit(0);
        }
    }
}
原文地址:https://www.cnblogs.com/kumu/p/6665272.html