从txt读取信息然后保存到excel中示例

一、导包

实现这个功能需要poi的包需要将jar包导入


<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17-beta1</version>
</dependency>

二、代码示例:



/**
 * 读取txt将数据保存到excel
 * @param args
 */
public static void main(String[] args) {
    String resultStr =null;
    Map<String, Object> result = null;
    // 读取数据
    try {
       
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("d:/000/result11.txt")),
                "UTF-8"));
        String lineTxt = null;

        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("du_1");
        HSSFRow firstrow = null;
        HSSFCell[] firstcell = null;
        HSSFCell[] c2 = null;
        HSSFCell[] c3 = null;
        HSSFCell[] c4 = null;
        HSSFCell[] c5 = null;

        int i = 2;

        firstrow = sheet.createRow(0);
        firstcell = new HSSFCell[15];
        firstcell[0] = firstrow.createCell(0);
        firstcell[0].setCellValue("电话号码");


        firstcell[1] = firstrow.createCell(1);
        firstcell[1].setCellValue("score");



        firstcell[2] = firstrow.createCell(4);
        firstcell[2].setCellValue("first_dim");


        firstcell[3] = firstrow.createCell(9);
        firstcell[3].setCellValue("second_dim");



        firstcell[0] = firstrow.createCell(14);
        firstcell[0].setCellValue("third_dim");

        firstrow = sheet.createRow(1);

        firstcell[0] = firstrow.createCell(2);
        firstcell[0].setCellValue("overdue_cnt");
        firstcell[0] = firstrow.createCell(3);
        firstcell[0].setCellValue("gray_cnt");
        firstcell[0] = firstrow.createCell(4);
        firstcell[0].setCellValue("black_cnt");
        firstcell[0] = firstrow.createCell(5);
        firstcell[0].setCellValue("all_cnt");
        firstcell[0] = firstrow.createCell(6);
        firstcell[0].setCellValue("client_cnt");

        firstcell[0] = firstrow.createCell(7);
        firstcell[0].setCellValue("overdue_cnt");
        firstcell[0] = firstrow.createCell(8);
        firstcell[0].setCellValue("gray_cnt");
        firstcell[0] = firstrow.createCell(9);
        firstcell[0].setCellValue("black_cnt");
        firstcell[0] = firstrow.createCell(10);
        firstcell[0].setCellValue("all_cnt");
        firstcell[0] = firstrow.createCell(11);
        firstcell[0].setCellValue("client_cnt");


        firstcell[0] = firstrow.createCell(12);
        firstcell[0].setCellValue("overdue_cnt");
        firstcell[0] = firstrow.createCell(13);
        firstcell[0].setCellValue("gray_cnt");
        firstcell[0] = firstrow.createCell(14);
        firstcell[0].setCellValue("black_cnt");
        firstcell[0] = firstrow.createCell(15);
        firstcell[0].setCellValue("all_cnt");
        firstcell[0] = firstrow.createCell(16);
        firstcell[0].setCellValue("client_cnt");



        while ((lineTxt = br.readLine()) != null) {
            Thread.sleep(1);
            System.out.println(lineTxt);
            String[] split = lineTxt.split("@");
            String phone = split[0];
            String body = split[1];
            if(phone!=null&&!phone.equals("")){

                JSONObject jsonObject = JSON.parseObject(body);
                String body1 = jsonObject.get("body").toString();
                JSONObject jsonObject1 = JSON.parseObject(body1);

                firstrow = sheet.createRow(i);
                firstcell[0] = firstrow.createCell(0);
                firstcell[0].setCellValue(phone);
                //

                String score = jsonObject1.get("score").toString();
                firstcell[0] = firstrow.createCell(1);
                firstcell[0].setCellValue(score);
                //
                if(jsonObject1.containsKey("first_dim")) {
                    String first_dim = jsonObject1.get("first_dim").toString();
                    if(first_dim!=null&&!first_dim.equals("")&&!first_dim.equals("{}")){
                        JSONObject first = JSON.parseObject(first_dim);
                        String foverdue_cnt = first.get("overdue_cnt").toString();
                        String fgray_cnt = first.get("gray_cnt").toString().toString();
                        String fblack_cnt = first.get("black_cnt").toString().toString();
                        String fall_cnt = first.get("all_cnt").toString().toString();
                        String fclient_cnt = first.get("client_cnt").toString().toString();
                        firstcell[0] = firstrow.createCell(2);
                        firstcell[0].setCellValue(foverdue_cnt);
                        firstcell[0] = firstrow.createCell(3);
                        firstcell[0].setCellValue(fgray_cnt);
                        firstcell[0] = firstrow.createCell(4);
                        firstcell[0].setCellValue(fblack_cnt);
                        firstcell[0] = firstrow.createCell(5);
                        firstcell[0].setCellValue(fall_cnt);
                        firstcell[0] = firstrow.createCell(6);
                        firstcell[0].setCellValue(fclient_cnt);
                    }

                }
                //
                if(jsonObject1.containsKey("second_dim")){

                    String second_dim = jsonObject1.get("second_dim").toString();
                    if(second_dim!=null&&!second_dim.equals("")&&!second_dim.equals("{}")){
                        JSONObject second = JSON.parseObject(second_dim);
                        String soverdue_cnt = second.get("overdue_cnt").toString();
                        String sgray_cnt = second.get("gray_cnt").toString().toString();
                        String sblack_cnt = second.get("black_cnt").toString().toString();
                        String sall_cnt = second.get("all_cnt").toString().toString();
                        String sclient_cnt = second.get("client_cnt").toString().toString();

                        firstcell[0] = firstrow.createCell(7);
                        firstcell[0].setCellValue(soverdue_cnt);
                        firstcell[0] = firstrow.createCell(8);
                        firstcell[0].setCellValue(sgray_cnt);
                        firstcell[0] = firstrow.createCell(9);
                        firstcell[0].setCellValue(sblack_cnt);
                        firstcell[0] = firstrow.createCell(10);
                        firstcell[0].setCellValue(sall_cnt);
                        firstcell[0] = firstrow.createCell(11);
                        firstcell[0].setCellValue(sclient_cnt);
                    }

                }

                //
                if(jsonObject1.containsKey("third_dim")) {
                    String third_dim = jsonObject1.get("third_dim").toString();
                    if(third_dim!=null&&!third_dim.equals("")&&!third_dim.equals("{}")){
                        JSONObject third = JSON.parseObject(third_dim);
                        String tsoverdue_cnt = third.get("overdue_cnt").toString();
                        String tsgray_cnt = third.get("gray_cnt").toString().toString();
                        String tsblack_cnt = third.get("black_cnt").toString().toString();
                        String tsall_cnt = third.get("all_cnt").toString().toString();
                        String tsclient_cnt = third.get("client_cnt").toString().toString();

                        firstcell[0] = firstrow.createCell(12);
                        firstcell[0].setCellValue(tsoverdue_cnt);
                        firstcell[0] = firstrow.createCell(13);
                        firstcell[0].setCellValue(tsgray_cnt);
                        firstcell[0] = firstrow.createCell(14);
                        firstcell[0].setCellValue(tsblack_cnt);
                        firstcell[0] = firstrow.createCell(15);
                        firstcell[0].setCellValue(tsall_cnt);
                        firstcell[0] = firstrow.createCell(16);
                        firstcell[0].setCellValue(tsclient_cnt);
                    }

                }
                i++;
            }else{
                continue;
            }
            
        }
        OutputStream out = new FileOutputStream("d:/000/du_1.xls");
        hwb.write(out);
        out.close();
        br.close();
    } catch (Exception e) {
        System.err.println("read errors :" + e);
    }
}

很久没用,突然用到熟悉一番,写到将示例代码记录下来一便以后用到

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值