写一个用Java实现的代码,功能是导入excel,其中excel有以下这些列:name,nameFull,sex,cardType,cardNumber,birthday,phoneNumber,emai
时间: 2023-09-19 19:06:47 浏览: 123
### 回答1:
public static void main(String[] args) {
try {
String filePath = "D://xxx.xlsx";
FileInputStream fis = new FileInputStream(new File(filePath));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
continue;
}
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("name", row.getCell(0).getStringCellValue());
dataMap.put("nameFull", row.getCell(1).getStringCellValue());
dataMap.put("sex", row.getCell(2).getStringCellValue());
dataMap.put("cardType", row.getCell(3).getStringCellValue());
dataMap.put("cardNumber", row.getCell(4).getStringCellValue());
dataMap.put("birthday", row.getCell(5).getStringCellValue());
dataMap.put("phoneNumber", row.getCell(6).getStringCellValue());
dataMap.put("email", row.getCell(7).getStringCellValue());
dataList.add(dataMap);
}
System.out.println(dataList);
workbook.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
### 回答2:
Java实现导入Excel功能的代码可以使用Apache POI库来操作Excel文件。下面是一个简单示例代码:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelImporter {
public static void main(String[] args) {
String filePath = "path/to/excel/file.xlsx";
try (FileInputStream fis = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 假设要导入的数据在第一个Sheet中
for (Row row : sheet) {
String name = "";
String nameFull = "";
String sex = "";
String cardType = "";
String cardNumber = "";
String birthday = "";
String phoneNumber = "";
String email = "";
// 获取每行中的单元格数据
for (Cell cell : row) {
int cellIndex = cell.getColumnIndex();
String cellValue = "";
switch (cellIndex) {
case 0:
cellValue = getStringCellValue(cell);
name = cellValue;
break;
case 1:
cellValue = getStringCellValue(cell);
nameFull = cellValue;
break;
case 2:
cellValue = getStringCellValue(cell);
sex = cellValue;
break;
case 3:
cellValue = getStringCellValue(cell);
cardType = cellValue;
break;
case 4:
cellValue = getStringCellValue(cell);
cardNumber = cellValue;
break;
case 5:
cellValue = getStringCellValue(cell);
birthday = cellValue;
break;
case 6:
cellValue = getStringCellValue(cell);
phoneNumber = cellValue;
break;
case 7:
cellValue = getStringCellValue(cell);
email = cellValue;
break;
default:
break;
}
// 这里可以根据需要按列处理单元格数据
// ...
}
// 这里可以根据需要对导入的数据进行处理
// ...
// 输出导入的数据
System.out.println("Name: " + name);
System.out.println("Name Full: " + nameFull);
System.out.println("Sex: " + sex);
System.out.println("Card Type: " + cardType);
System.out.println("Card Number: " + cardNumber);
System.out.println("Birthday: " + birthday);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Email: " + email);
System.out.println("----------");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getStringCellValue(Cell cell) {
String cellValue = "";
if (cell.getCellType() == CellType.STRING) {
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
cellValue = String.valueOf(cell.getNumericCellValue());
} else if (cell.getCellType() == CellType.BOOLEAN) {
cellValue = String.valueOf(cell.getBooleanCellValue());
}
return cellValue;
}
}
```
以上代码利用Apache POI库读取Excel文件,并将需要的每列数据存储在相应变量中。根据实际需求,你可以对导入的数据进行处理,比如存储到数据库中或进行其他操作。代码中的`path/to/excel/file.xlsx`需要替换为实际的Excel文件路径。请确保引入了Apache POI库的依赖。
### 回答3:
以下是用Java实现导入Excel功能的代码示例:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
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.xssf.usermodel.XSSFWorkbook;
public class ExcelImporter {
public static void main(String[] args) {
String excelFilePath = "path/to/excel/file.xlsx";
try {
ArrayList<Person> persons = readExcel(excelFilePath);
// 对导入的数据进行处理
for (Person person : persons) {
System.out.println(person.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<Person> readExcel(String excelFilePath) throws IOException {
ArrayList<Person> persons = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> iterator = sheet.iterator();
// 遍历每一行数据
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Person person = new Person();
// 遍历每个单元格数据
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 0: // name列
person.setName(cell.getStringCellValue());
break;
case 1: // nameFull列
person.setNameFull(cell.getStringCellValue());
break;
case 2: // sex列
person.setSex(cell.getStringCellValue());
break;
case 3: // cardType列
person.setCardType(cell.getStringCellValue());
break;
case 4: // cardNumber列
person.setCardNumber(cell.getStringCellValue());
break;
case 5: // birthday列
person.setBirthday(cell.getStringCellValue());
break;
case 6: // phoneNumber列
person.setPhoneNumber(cell.getStringCellValue());
break;
case 7: // email列
person.setEmail(cell.getStringCellValue());
break;
default:
break;
}
}
persons.add(person);
}
workbook.close();
inputStream.close();
return persons;
}
}
class Person {
private String name;
private String nameFull;
private String sex;
private String cardType;
private String cardNumber;
private String birthday;
private String phoneNumber;
private String email;
// 自定义构造方法、getter和setter方法
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", nameFull='" + nameFull + '\'' +
", sex='" + sex + '\'' +
", cardType='" + cardType + '\'' +
", cardNumber='" + cardNumber + '\'' +
", birthday='" + birthday + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
", email='" + email + '\'' +
'}';
}
}
```
以上代码通过Apache POI库实现了导入Excel的功能。主要的逻辑是通过XSSFWorkbook类和Sheet类读取Excel文件,并通过Row类和Cell类遍历每个单元格中的数据,然后将数据存储到自定义的Person类对象中。最后将所有的Person对象存储在ArrayList<Person>中,并进行进一步的数据处理或展示。
阅读全文