实现excel读取成txt文件

 <!--excel读取1-->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
package com.dxy.study_0628.util.excel;

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

import java.io.*;

public class ExceltoTxt_1124 {
    
    
    public static void ToAnalysisExcel() {
    
    
        // Unable to recognize OLE stream 不支持xlsx格式 支持xls格式
        try {
    
    
            Workbook workbook = null;
            File Inputfile = new File("F:\\xxxx\\test\\user\\myInfo.xls");
            File outputfile = new File("F:\\xxxx\\test\\user\\mtInfo.txt");

            File outputfile2 = new File("F:\\xxxx\\test\\user\\mtInfo2.txt");
            FileOutputStream fileOutputStream2 = new FileOutputStream(outputfile2);
            BufferedOutputStream bw2 = new BufferedOutputStream(fileOutputStream2); //输出语句


            FileInputStream fileInputStream = new FileInputStream(Inputfile);
            workbook = Workbook.getWorkbook(fileInputStream);
            FileOutputStream fileOutputStream = new FileOutputStream(outputfile);
            BufferedOutputStream bw = new BufferedOutputStream(fileOutputStream); //输出语句

            Sheet readfirst = workbook.getSheet(0);
            System.out.println(readfirst.getName());
            int rows = readfirst.getRows();
            int clomns = readfirst.getColumns();
            System.out.println("row:" + rows);
            System.out.println("clomns:" + clomns);

            for (int i = 3; i < rows; i++) {
    
    
                //循环得到每一行的单元格对象
                Cell[] cells = readfirst.getRow(i);
                //根据每一个单元格对象的到里面的值
                String ce1 = cells[0].getContents();
                String ce2 = cells[1].getContents();
                String ce3 = cells[2].getContents();
                String ce4 = cells[3].getContents();
                //将得到的值放在一个我需要的格式的string对象中
                String output = ce1 + "," + ce2 + "," + ce3 + "," + ce4 + "\n";
                System.out.print(output);
                //write and flush到 我需要的文件中去,flush后才能成功
                byte[] outputbyte = new String(output).getBytes();
                bw.write(outputbyte);

                System.out.println();
                bw.flush();
            }


            Sheet readfirst1 = workbook.getSheet(1);
            System.out.println(readfirst1.getName());
            int rows1 = readfirst1.getRows();
            int clomns1 = readfirst1.getColumns();
            for (int i = 4; i < rows1; i++) {
    
    
                //循环得到每一行的单元格对象
                Cell[] cells = readfirst1.getRow(i);
                //根据每一个单元格对象的到里面的值
                String ce1 = cells[0].getContents();
                String ce2 = cells[1].getContents();
                String ce3 = cells[2].getContents();
                String ce4 = cells[3].getContents();
                //将得到的值放在一个我需要的格式的string对象中
                String output = ce1 + "," + ce2 + "," + ce3 + "," + ce4 + "\n";
                System.out.print(output);
                //write and flush到 我需要的文件中去,flush后才能成功
                byte[] outputbyte = new String(output).getBytes();
                bw2.write(outputbyte);

                System.out.println();
                bw2.flush();
            }


        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (BiffException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }



    }

    public static void main(String[] args) {
    
    
        ExceltoTxt_1124.ToAnalysisExcel();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/121525749