用java将excel数据导入txt

package com.alibaba.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class WriteTxt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String filepath = "e:\\demo.xls";

        try {
            Workbook workbook = Workbook.getWorkbook(new File(filepath));
            Sheet sheet = workbook.getSheet(0);
            File fileSuccess = new File("success.txt");
            File fileError = new File("error.txt");
            FileWriter fwSuccess = new FileWriter(fileSuccess);
            BufferedWriter bwSuccess = new BufferedWriter(fwSuccess);

            FileWriter fwError = new FileWriter(fileError);
            BufferedWriter bwError = new BufferedWriter(fwError);
            // j为行数,getCell("列号","行号")
            int j = sheet.getRows();
            int y = sheet.getColumns();
            Cell c = null;
            for (int i = 0; i < j; i++) {
                String s = "";
                for (int x = 0; x < y; x++) {
                    c = sheet.getCell(x, i);
                    s += c.getContents() + "    ";
                }

                if (s.indexOf("&") != -1 || s.indexOf("@") != -1) {
                    bwSuccess.write(s);
                    bwSuccess.write(" ");
                    bwSuccess.flush();
                    // 读取一行后换行
                    bwSuccess.newLine();
                    // 刷新
                    bwSuccess.flush();
                } else {
                    bwError.write(s);
                    bwError.write(" ");
                    bwError.flush();
                    bwError.newLine();
                    bwError.flush();
                }
            }
            System.out.println("写入结束");
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

转载于:https://my.oschina.net/u/3744350/blog/1604269

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值