Java操作Excel实现批量添加

1、引入依赖包

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

2、Java语句

		try {
			File file = new File("D:\\zhiwu.xlsx");
			FileInputStream fileInput = null;//创建文件输入流
			fileInput = new FileInputStream(file);
			XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象
			XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
			int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
			String family = null;
			String muId = null;
			String gangid = null;
			String menid = null;
			String jieid = null;
			for (int i = 0; i < lastRowNum; i++) {
				XSSFRow row = sheet.getRow(i);//获取每一行
				String cell = row.getCell(0).getStringCellValue();
				String cell1 = row.getCell(1).getStringCellValue();
				String cell2 = row.getCell(2).getStringCellValue();
				if(cell.substring(cell.length()-1).equals("科")){
					QueryWrapper<Taxonomy> queryWrapper = new QueryWrapper<>();
					queryWrapper.like("name", cell);
					List<Taxonomy> taxonomies = taxonomyService.list(queryWrapper);
					if (!taxonomies.isEmpty()) {
						//科
						family = taxonomies.get(0).getId();
						//目
						Taxonomy taxonomy = taxonomyService.queryTaxonomyById(family);
						muId = taxonomy.getParentId();
						//纲
						Taxonomy taxonomy1 = taxonomyService.queryTaxonomyById(muId);
						gangid = taxonomy1.getParentId();
						//门
						Taxonomy taxonomy2 = taxonomyService.queryTaxonomyById(gangid);
						menid = taxonomy2.getParentId();
						//界
						Taxonomy taxonomy3 = taxonomyService.queryTaxonomyById(menid);
						jieid = taxonomy3.getParentId();
					}else{
						Taxonomy taxonomy = new Taxonomy();
						taxonomy.setId(UUID.randomUUID().toString());
						taxonomy.setName(cell);
						taxonomy.setRank("科");
						taxonomy.setParentId("999");
						taxonomy.setIsParent(Boolean.valueOf("1"));
						taxonomy.setDeleteFlag("N");
						service.save(model);
					}
				}else{
					for(int j = 0;j < cell.length();j++){
						QueryWrapper<Taxonomy> queryWrapper1 = new QueryWrapper<>();
						queryWrapper1.like("name", cell);
						List<Taxonomy> taxonomies1 = taxonomyService.list(queryWrapper1);
						if (taxonomies1.isEmpty()) {
							model.setViewer((long) 2);
							model.setName(cell);
							model.setLatinName(cell1);
							if (cell2.contains("省级")) {
								model.setShanxiConservation("1");
							} else if (cell2.contains("国家II级")) {
								model.setNationConservation("Ⅱ级");
							}
							model.setKingdom(jieid);
							model.setPhylum(menid);
							model.setClasss(gangid);
							model.setOrders(muId);
							model.setFamily(family);
							model.setGuid(UUID.randomUUID().toString());
							addSpecies(model);
						}
					}
				}
			}
			wb.close();
			fileInput.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Apache POI库来操作Excel文件,实现数据批量导入功能。 以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; 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.ss.usermodel.WorkbookFactory; public class ExcelReader { public static void main(String[] args) throws IOException { String excelFilePath = "path/to/excel/file.xlsx"; List<List<String>> data = readExcel(excelFilePath); // 处理导入的数据 } public static List<List<String>> readExcel(String excelFilePath) throws IOException { List<List<String>> data = new ArrayList<>(); InputStream inputStream = new FileInputStream(excelFilePath); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.iterator(); List<String> rowData = new ArrayList<>(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case STRING: rowData.add(cell.getStringCellValue()); break; case NUMERIC: rowData.add(String.valueOf(cell.getNumericCellValue())); break; case BOOLEAN: rowData.add(String.valueOf(cell.getBooleanCellValue())); break; default: rowData.add(""); } } data.add(rowData); } workbook.close(); inputStream.close(); return data; } } ``` 此示例将Excel文件的第一个工作表的所有数据读取到一个二维列表,可以根据需要进行处理。需要注意的是,此示例没有对异常进行处理。在实际使用时需要添加异常处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值