JAVA读取Excel数据

下载 jxl.jar

找到一个博主发的,下载好后去掉.zip后缀
jxl.jar下载 - 天青色wy - 博客园 https://www.cnblogs.com/wangyi0419/p/12001258.html

导入jxl.jar

另一个博主的教程,按第一个就可以
https://blog.csdn.net/hwt1070359898/article/details/90517291

读取程序

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

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadExcel {
    private static String TrainNum = "LG1102"; //列车车次号
    private static String QBID = "2464322"; //确保ID
    private static String[] TrainMarshalling = new String[11];
    public static void main(String[] args) throws BiffException, IOException {
        readExcel();
        for(String i : TrainMarshalling){
            System.out.println(i);
        }
    }
    private static void readExcel() throws BiffException, IOException {
        File xlsFile = new File("F:\\marshallingdirectory.xls");
        // 获得工作簿对象
        Workbook workbook = Workbook.getWorkbook(xlsFile);
        // 获得所有工作表
        Sheet[] sheets = workbook.getSheets();
        // 遍历工作表
        if (sheets != null) {
            for (Sheet sheet : sheets) {
                // 获得行数
                int rows = sheet.getRows();
                // 获得列数
                int cols = sheet.getColumns();
                // 读取数据
				for (int row = 0; row < rows; row++) {
	                  for (int col = 0; col < cols; col++) {
	                       Cell cell = sheet.getCell(col, row);
	                       System.out.println(cell.getContents());   //全部输出数据
	                  }
	            }
                for (int row = 0; row < rows; row++) {
                    Cell cell1 = sheet.getCell(0, row);
                    Cell cell2 = sheet.getCell(1, row);
                    //加入判断条件
                    if (cell1.getContents().equals(TrainNum) && cell2.getContents().equals(QBID)) {
                        for (int col = 0; col < cols; col++) {
                            Cell count = sheet.getCell(col, row);
                            //得到想要的数组,注意此时是String,可根据自己需要进行类型的替换
                            TrainMarshalling[col] = count.getContents();
                        }
                    }
                }
            }
        }
        workbook.close();
    }
}
写入Excel

二维数组

 // 导出到 excel的代码其实跟导出到 txt 的代码一样
    public void writeArrayToExcel(double[][] data, String string) {
        int rowNum = data.length;
        int columnNum = data[0].length;
        try {
            FileWriter fw = new FileWriter(string);
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < columnNum; j++)
                    fw.write(data[i][j] + "\t"); // tab 间隔
                fw.write("\n"); // 换行
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

单列的

 //将单列数组写入Excel表中
    public static void writeListToExcel(List<Float> data, String string) {
        int rowNum = data.size();
        //int columnNum = data[0].length;
        try {
            FileWriter fw = new FileWriter(string);
            for (float datum : data) {
                //for (int j = 0; j < columnNum; j++)
                //fw.write(data[i][j] + "\t"); // tab 间隔
                fw.write(datum + "\t"); // tab 间隔
                fw.write("\n"); // 换行
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
写入txt
public void writeArrayToTxt(double[][] data, String string) {
        int rowNum = data.length;
        int columnNum = data[0].length;
        try {
            FileWriter fw = new FileWriter(string);
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < columnNum; j++)
                    fw.write(data[i][j] + "\t");
                fw.write("\n");
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

读txt

 public static String txt2String(File file) throws IOException {
        StringBuilder result = new StringBuilder();

            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
                result.append(System.lineSeparator()).append(s);
            }
            br.close();
        return result.toString();
    }

有些来自
Java读写Excel表格数据 - lishbo - 博客园 https://www.cnblogs.com/lishbo/p/9955993.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值