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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Temp_1998_H/article/details/122328299
今日推荐