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

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

三,代码

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<list.length;f++){
                     sheet.addCell(new Label(f, i, list[f], headerFormat));
                 }
                 i++;
                 //判断内容是否为空行,如果是,则转行
                 if("\\r".equals(lineTxt)){
                    continue;
                 }
            }
            //写入文件
            workbook.write();
            //关闭文件
            workbook.close();
            read.close();
        }catch (Exception e){
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }

}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值