已有小程序代码,本人不懂Java需要远程帮忙解决。无须给出解决方案。
eclipse平台下,如何正确获取EXCEL里对应的想要的表格数据?如何实现导入Excel表后,输入某个工作号后自动带入其他的Excel表格里的信息出来?
可以使用 Apache POI 库来读取 Excel 文件中的数据。具体步骤如下:
导入 Apache POI 库
在项目中导入以下 jar 包:
poi-.jar
poi-ooxml-.jar
poi-ooxml-schemas-.jar
xmlbeans-.jar
commons-collections4-.jar
commons-compress-.jar
commons-math3-.jar
其中 是 Apache POI 库的版本号。
读取 Excel 文件
请参考以下示例代码:
java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile {
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File("example.xlsx"));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + ",");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + ",");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
其中 example.xlsx 是需要读取的 Excel 文件名。该示例代码将输出表格数据到控制台。
实现输入工作号后自动带入其他 Excel 表格里的信息
要实现输入工作号后自动带入其他 Excel 表格里的信息,可以在读取 Excel 文件时,将表格数据存储到一个 Map 中,工作号作为键,对应的信息作为值。然后在用户输入工作号时,从该 Map 中获取对应的信息即可。具体代码实现可以参考以下示例:
java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile {
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File("example.xlsx"));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
// 存储 Excel 表格数据的 Map
Map<String, String[]> dataMap = new HashMap<>();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
// 解析表格数据,存储到 Map 中
String workNo = "";
String[] rowData = new String[3];
int i = 0;
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.STRING) {
if (i == 0) {
workNo = currentCell.getStringCellValue();
} else {
rowData[i-1] = currentCell.getStringCellValue();
}
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
rowData[i-1] = String.valueOf(currentCell.getNumericCellValue());
}
i++;
}
dataMap.put(workNo, rowData);
}
// 获取用户输入的工作号,从 Map 中获取对应信息并输出
String workNo = "123";
String[] rowData = dataMap.get(workNo);
System.out.println(rowData[0] + "," + rowData[1] + "," + rowData[2]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
该示例代码可以解析输入的 Excel 文件,并将表格数据存储到一个 Map 中。在实现输入工作号后自动带入其他 Excel 表格里的信息时,只需要从该 Map 中获取对应的信息即可。