Java language to read Excel file information

Background: In a recent project, there is a table whose information is too large and needs to be deleted manually. Because the amount of information is relatively large, it is troublesome to delete manually, so the method of using excel to read the data and then operating the database to delete the data, in fact, as long as you want to delete the data from Excel You can use this method to operate Excel to obtain information, and the code is very simple.

  1. Step 1: Import the required jar package
    . Net.sourceforge.jexcelapi:jxl-2.6.12.jar is used here. Special attention should be paid to that Excel above version 2003 is not supported, because this jar package will be available after 2011 There is no new version update, only Excel2003 is supported, so you want to operate the latest version of Excel. The suffix of the 2003 version of the Excel file is xls. Although it only supports Excel 2003, the functions are well realized, the operation is simple, and the use is convenient.
    The following is the pom package introduced in the pom file

    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>
    
  2. Step 2: Select the dependent library (if it has been configured, there is no need to change it)

    <mirror>
        <id>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
    
  3. Step 3: Implement the code

    public ResultDTO dealExcelToDb(File file) {
          
          
        	Workbook workbook = null;
        	try {
          
          
            	//转化文件对象
            	workbook = Workbook.getWorkbook(file);
            	//获取第一个sheet
            	Sheet sheet = workbook.getSheet(0);
            	//获取行数
            	log.info("该Excel存在" + (sheet.getRows() - 1) + "行 ");
            	//获取列数
            	log.info("该Excel存在" + sheet.getColumns() + "列 ");
            	log.info("每次处理500条,共需处理" + (sheet.getRows() / 500 + (sheet.getRows() % 500 > 0 ? 1 : 0)) + "次 ");
    
            	int num = 0;//处理批次
           		int allRows = 0;//处理的总条数
            	int batchRows = 0;//每次处理的次数
            	StringBuilder stringBuilder  = new StringBuilder();
            	for (int i = 0; i < sheet.getRows(); i++) {
          
          
                	if (i > 0) {
          
          
                    	allRows++;
                	}
                    for (int j = 0; j < sheet.getColumns(); j++) {
          
          
    
                   	 	//第一行为字段名,oid,不处理
                    	if (i > 0 && i< sheet.getRows()) {
          
          
                        	batchRows++;
                        	//获取第i行,第j列的单元格的值
                        	stringBuilder.append(sheet.getCell(j, i).getContents());
                        	stringBuilder.append(",");
                    	} else {
          
          
                        	break;
                    	}
    
                	}
    
                	//数据达到500条处理一次或者到达文件末尾
                	if (batchRows == 500 || allRows == sheet.getRows() - 1) {
          
          
    
                    	log.info("第" + (++num) + "批次数据开始处理");
                    	//开始调用批处理程序
                    	Map<String,String> map = new HashMap();
    
                    	map.put("jsonParams",stringBuilder.toString().substring(0,stringBuilder.lastIndexOf(",")));
                    	map.put("tableName","supplier_settled_synchronization_data_log ");
                    	String result = HttpClientUtil.sendPostDataByMap("http://www.baidu.com", map, CommonConstant.ENCODING);
                    	log.info("调用中台处理结果:" + result.toString());
                    	batchRows = 0;//清空处理条数,下次循环重新计数
                    	log.info("第" + num + "批次数据结束处理");
                	}
    
    
            	}
        	} catch (IOException e) {
          
          
            	e.printStackTrace();
        	} catch (BiffException e) {
          
          
            	log.info("文件解析出错,请使用1997-2003版本Excel文件");
            	e.printStackTrace();
        	} finally {
          
          
            	if (workbook != null) {
          
          
                	workbook.close();
            	}
        	}
    
        	return new ResultDTO(ExceptionEnum.COMMON_SUCCESS_30.getResultCode(), null, "数据导入成功!");
    	}
    

    From the code above, it can be seen that the operation is very simple (most of them are my own processing code), first convert the file into a workbook, and then get the sheet page, then you can get the rows, get the columns, and also get the cell data. , So I got the data in Excel, the only regret is that no one has updated this jar, only Excel2003 is supported.

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/114382882