excel文件转换为txt文件

package 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;
import org.apache.commons.lang.StringUtils;

public class DownResultAPI{
    //excel文件转换为txt文件
    public static void main(String[] args) {
        // 找到excel文件的路径
        String filepath = "C:\\result\\20211126\\WindUV_1637917365515.xls";
        try {
            Workbook workbook = Workbook.getWorkbook(new File(filepath));
            Sheet sheet = workbook.getSheet(0);
            //转成txt要保存的位置以及文件名
            File file = new File("C:\\result\\20211126\\WindUV_1637917365515.txt");
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            // j为行数,getCell("列号","行号")
            int j = sheet.getRows();
            int y = sheet.getColumns();
            for (int i = 0; i < j; i++) {
                for(int x=0; x<y; x++){
                    Cell c = sheet.getCell(x, i);
                    String s = c.getContents();
                //每一列用逗号隔开,并且第一列的最前边和最后一列的最后边不加逗号
                    if(x!=0 && StringUtils.isNotBlank(s)){  bw.write(","+s);}
                    else {
                    bw.write(s);
                    bw.flush();
               }
                }
                bw.newLine();
                bw.flush();
            }
            System.out.println("写入转换结束");
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值