java读取excel表中的数据

导入依赖


    org.apache.poi
    poi
    3.17-beta1



    org.apache.poi
    poi-ooxml
    3.17-beta1



    org.apache.poi
    poi-ooxml-schemas
    3.17-beta1

核心代码

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class Excel{
public List>  read(String filePath,String columns[]) {
        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List> list = null;
        String cellData = null;
        wb = readExcel(filePath);
        if(wb != null){
        //用来存放表中数据
 list = new ArrayList>();
        //获取第一个sheet
 sheet = wb.getSheetAt(0);
        //获取最大行数
 int rownum = sheet.getPhysicalNumberOfRows();
        //获取第一行
 row = sheet.getRow(0);
        //获取最大列数
 int colnum = row.getPhysicalNumberOfCells();
        for (int i = 1; i map = new LinkedHashMap();
        row = sheet.getRow(i);
        if(row !=null){
        for (int j=0;j> list=ex.read("文件路径",c);
        DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
        for (Map map : list) {
              //通过健去获取值
              String carNo = map.get("carNo");
            }
        }
}

去除数据库中某个字段相同,其他字段不相同的记录

创建中间表,并获取值
create table tmp as select min(主键) as col1 from 去重表名 GROUP BY 去重字段;
删除重复值,已中间表中记录作为条件
delete from 去重表名 where 主键 not in (select col1 from tmp); 
删除中间表
drop table tmp;

你可能感兴趣的:(java)