Java读取txt文本内容并写入Excel-优化

参考原文如下:https://blog.csdn.net/hongjingchao/article/details/51734556

官网:http://jexcelapi.sourceforge.net/

java doc: http://jxl.sourceforge.net/javadoc/index.html#

一,需求

客户给了20M以下格式的数据,要我转成excel,这用手,估计得废了,几百万数据,我估计要疯。

a@*@3@*@
b@*@9@*@

二,pom


    net.sourceforge.jexcelapi
    jxl
    2.6.12

三,代码

package com.cn.hw.com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class TT{

    public static void main(String argv[]){
        //读取的txt文件路径
        String txtFilePath = "D://q.txt";
        //生成的excel文件路径
        String excelFilePath = "D:/数据.xls";
        //编码格式
        String encoding = "GBK";
        readAndWrite(txtFilePath,excelFilePath,encoding);
    }

    public static void readAndWrite(String filePath,String excelFilePath,String encoding){
        try{
            File file = new File(filePath);
            File tempFile = new File(excelFilePath);
            //判断文件是否存在
            if (!file.isFile() || !file.exists()){
                System.out.println("找不到指定的文件");
            }
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
            BufferedReader bufferedReader = new BufferedReader(read);
            WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
            WritableSheet sheet = workbook.createSheet("Sheet1", 0);

            //设置字体为宋体,11号
            WritableFont headerFont = new WritableFont(WritableFont.createFont("宋体"), 11,
                    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            WritableCellFormat headerFormat = new WritableCellFormat (headerFont);

            //一些临时变量,用于写到excel中
            String lineTxt = null;
            int i = 0;
            while ((lineTxt = bufferedReader.readLine()) != null){
                 String[] list =  lineTxt.split("@!@");
                 for (int f=0;f

你可能感兴趣的:(java)