JAVA读取Excel数据
下载 jxl.jar
找到一个博主发的,下载好后去掉.zip后缀
jxl.jar下载 - 天青色wy - 博客园 https://www.cnblogs.com/wangyi0419/p/12001258.html
导入jxl.jar
另一个博主的教程,按第一个就可以
https://blog.youkuaiyun.com/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