Java实现批量导入excel表格数据到数据库中的方法

本文实例讲述了Java实现批量导入excel表格数据到数据库中的方法。分享给大家供大家参考,具体如下:

1、创建导入抽象类

package com.gcloud.common.excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
 * 导入抽象类
 * Created by charlin on 2017/9/7.
 */
public abstract class HxlsAbstract implements HSSFListener {
  private int minColumns;
  private POIFSFileSystem fs;
  private PrintStream output;
  private int lastRowNumber;
  private int lastColumnNumber;
  /** Should we output the formula, or the value it has? */
  private boolean outputFormulaValues = true;
  /** For parsing Formulas */
  private SheetRecordCollectingListener workbookBuildingListener;
  private HSSFWorkbook stubWorkbook;
  // Records we pick up as we process
  private SSTRecord sstRecord;
  private FormatTrackingHSSFListener formatListener;
  /** So we known which sheet we're on */
  private int sheetIndex = -1;
  private BoundSheetRecord[] orderedBSRs;
  @SuppressWarnings("unchecked")
  private ArrayList boundSheetRecords = new ArrayList();
  // For handling formulas with string results
  private int nextRow;
  private int nextColumn;
  private boolean outputNextStringRecord;
  private int curRow;
  private List<String> rowlist;
  @SuppressWarnings( "unused")
  private String sheetName;
  public HxlsAbstract(POIFSFileSystem fs)
      throws SQLException {
    this.fs = fs;
    this.output = System.out;
    this.minColumns = -1;
    this.curRow = 0;
    this.rowlist = new ArrayList<String>();
  }
  public HxlsAbstract(String filename) throws IOException,
      FileNotFoundException, SQLException {
    this(new POIFSFileSystem(new FileInputStream(filename)));
  }
  //excel记录行操作方法,以行索引和行元素列表为参数,对一行元素进行操作,元素为String类型
// public abstract void optRows(int curRow, List<String> rowlist) throws SQLException ;
  //excel记录行操作方法,以sheet索引,行索引和行元素列表为参数,对sheet的一行元素进行操作,元素为String类型
  public abstract void optRows(int sheetIndex,int curRow, List<String> rowlist) throws Exception;
  /**
   * 遍历 excel 文件
   */
  public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(
        this);
    formatListener = new FormatTrackingHSSFListener(listener);
    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();
    if (outputFormulaValues) {
      request.addListenerForAllRecords(formatListener);
    } else {
      workbookBuildingListener = new SheetRecordCollectingListener(
          formatListener);
      request.addListenerForAllRecords(workbookBuildingListener);
    }
    factory.processWorkbookEvents(request, fs);
  }
  /**
   * HSSFListener 监听方法,处理 Record
   */
  @SuppressWarnings("unchecked")
  public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;
    String value = null;
    switch (record.getSid()) {
    case BoundSheetRecord.sid:
      boundSheetRecords.add(record);
      break;
    case BOFRecord.sid:
      BOFRecord br = (BOFRecord) record;
      //进入sheet
      if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
        // Create sub workbook if required
        if (workbookBuildingListener != null && stubWorkbook == null) {
          stubWorkbook = workbookBuildingListener
              .getStubHSSFWorkbook();
        }
        // Works by ordering the BSRs by the location of
        // their BOFRecords, and then knowing that we
        // process BOFRecords in byte offset order
        sheetIndex++;
        if (orderedBSRs == null) {
          orderedBSRs = BoundSheetRecord
              .orderByBofPosition(boundSheetRecords);
        }
        sheetName = orderedBSRs[sheetIndex].getSheetname();
      }
      break;
    case SSTRecord.sid:
      sstRecord = (SSTRecord) record;
      break;
    case BlankRecord.sid:
      BlankRecord brec = (BlankRecord) record;
      thisRow = brec.getRow();
      thisColumn = brec.getColumn();
      thisStr = "";
      break;
    case BoolErrRecord.sid:
      BoolErrRecord berec = (BoolErrRecord) record;
      thisRow = berec.getRow();
      thisColumn = berec.getColumn();
      thisStr = "";
      break;
    case FormulaRecord.sid:
      FormulaRecord frec = (FormulaRecord) record;
      thisRow = frec.getRow();
      thisColumn = frec.getColumn();
      if (outputFormulaValues) {
        if (Double.isNaN(frec.getValue())) {
          // Formula result is a string
          // This is stored in the next record
          outputNextStringRecord = true;
          nextRow = frec.getRow();
          nextColumn = frec.getColumn();
        } else {
          thisStr = formatListener.formatNumberDateCell(frec);
        }
      } else {
        thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook,
            frec.getParsedExpression()) + '"';
      }
      break;
    case StringRecord.sid:
      if (outputNextStringRecord) {
        // String for formula
        StringRecord srec = (StringRecord) record;
        thisStr = srec.getString();
        thisRow = nextRow;
        thisColumn = nextColumn;
        outputNextStringRecord = false;
      }
      break;
    case LabelRecord.sid:
      LabelRecord lrec = (LabelRecord) record;
      curRow = thisRow = lrec.getRow();
      thisColumn = lrec.getColumn();
      value = lrec.getValue().trim();
      value = value.equals("")?" ":value;
      this.rowlist.add(thisColumn, value);
      break;
    case LabelSSTRecord.sid:
      LabelSSTRecord lsrec = (LabelSSTRecord) record;
      curRow = thisRow = lsrec.getRow();
      thisColumn = lsrec.getColumn();
      if (sstRecord == null) {
        rowlist.add(thisColumn, " ");
      } else {
        value = sstRecord
        .getString(lsrec.getSSTIndex()).toString().trim();
        value = value.equals("")?" ":value;
        rowlist.add(thisColumn,value);
      }
      break;
    case NoteRecord.sid:
      NoteRecord nrec = (NoteRecord) record;
      thisRow = nrec.getRow();
      thisColumn = nrec.getColumn();
      // TODO: Find object to match nrec.getShapeId()
      thisStr = '"' + "(TODO)" + '"';
      break;
    case NumberRecord.sid:
      NumberRecord numrec = (NumberRecord) record;
      curRow = thisRow = numrec.getRow();
      thisColumn = numrec.getColumn();
      value = formatListener.formatNumberDateCell(numrec).trim();
      value = value.equals("")?" ":value;
      // Format
      rowlist.add(thisColumn, value);
      break;
    case RKRecord.sid:
      RKRecord rkrec = (RKRecord) record;
      thisRow = rkrec.getRow();
      thisColumn = rkrec.getColumn();
      thisStr = '"' + "(TODO)" + '"';
      break;
    default:
      break;
    }
    // 遇到新行的操作
    if (thisRow != -1 && thisRow != lastRowNumber) {
      lastColumnNumber = -1;
    }
    // 空值的操作
    if (record instanceof MissingCellDummyRecord) {
      MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
      curRow = thisRow = mc.getRow();
      thisColumn = mc.getColumn();
      rowlist.add(thisColumn," ");
    }
    // 如果遇到能打印的东西,在这里打印
    if (thisStr != null) {
      if (thisColumn > 0) {
        output.print(',');
      }
      output.print(thisStr);
    }
    // 更新行和列的值
    if (thisRow > -1)
      lastRowNumber = thisRow;
    if (thisColumn > -1)
      lastColumnNumber = thisColumn;
    // 行结束时的操作
    if (record instanceof LastCellOfRowDummyRecord) {
      if (minColumns > 0) {
        // 列值重新置空
        if (lastColumnNumber == -1) {
          lastColumnNumber = 0;
        }
      }
      // 行结束时, 调用 optRows() 方法
      lastColumnNumber = -1;
      try {
        optRows(sheetIndex,curRow, rowlist);
      } catch (Exception e) {
        e.printStackTrace();
      }
      rowlist.clear();
    }
  }
}

2、创建导入接口

package com.gcloud.common.excel;
import java.util.List;
public interface HxlsOptRowsInterface {
  public static final String SUCCESS="success";
  /**
   * 处理excel文件每行数据方法
   * @param sheetIndex
   * @param curRow
   * @param rowlist
   * @return success:成功,否则为失败原因
   * @throws Exception
   */
  public String optRows(int sheetIndex, int curRow, List<String> rowlist) throws Exception;
}

3、创建实现类, 在这个方法实现把导入的数据添加到数据库中

package com.gcloud.common.excel;
import java.util.List;
public class HxlsInterfaceImpl implements HxlsOptRowsInterface {
  @Override
  public String optRows(int sheetIndex, int curRow, List<String> datalist)
      throws Exception {
    //在这里执行数据的插入
    //System.out.println(rowlist);
    //saveData(datalist);
    return "";
  }
}

4、导入工具实现

package com.gcloud.common.excel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * excel导入工具
 * Created by charlin on 2017/9/7.
 */
public class ExcelImportUtil extends HxlsAbstract{
  //数据处理bean
  private HxlsOptRowsInterface hxlsOptRowsInterface;
  //处理数据总数
  private int optRows_sum = 0;
  //处理数据成功数量
  private int optRows_success = 0;
  //处理数据失败数量
  private int optRows_failure = 0;
  //excel表格每列标题
  private List<String> rowtitle ;
  //失败数据
  private List<List<String>> failrows;
  //失败原因
  private List<String> failmsgs ;
  //要处理数据所在的sheet索引,从0开始
  private int sheetIndex;
  public ExcelImportUtil(String filename, int sheetIndex, HxlsOptRowsInterface hxlsOptRowsInterface) throws IOException,
      FileNotFoundException, SQLException {
    super(filename);
    this.sheetIndex = sheetIndex;
    this.hxlsOptRowsInterface = hxlsOptRowsInterface;
    this.rowtitle = new ArrayList<String>();
    this.failrows = new ArrayList<List<String>>();
    this.failmsgs = new ArrayList<String>();
  }
  @Override
  public void optRows(int sheetIndex,int curRow, List<String> rowlist) throws Exception {
    /*for (int i = 0 ;i< rowlist.size();i++){
      System.out.print("'"+rowlist.get(i)+"',");
    }
    System.out.println();*/
    //将rowlist的长度补齐和标题一致
    int k=rowtitle.size()-rowlist.size();
    for(int i=0;i<k;i++){
      rowlist.add(null);
    }
    if(sheetIndex == this.sheetIndex){
      optRows_sum++;
      if(curRow == 0){//记录标题
        rowtitle.addAll(rowlist);
      }else{
        String result = hxlsOptRowsInterface.optRows(sheetIndex, curRow, rowlist);
        if(!result.equals(hxlsOptRowsInterface.SUCCESS)){
          optRows_failure++;
          //失败数据
          failrows.add(new ArrayList<String>(rowlist));
          failmsgs.add(result);
        }else{
          optRows_success++;
        }
      }
    }
  }
  public long getOptRows_sum() {
    return optRows_sum;
  }
  public void setOptRows_sum(int optRows_sum) {
    this.optRows_sum = optRows_sum;
  }
  public long getOptRows_success() {
    return optRows_success;
  }
  public void setOptRows_success(int optRows_success) {
    this.optRows_success = optRows_success;
  }
  public long getOptRows_failure() {
    return optRows_failure;
  }
  public void setOptRows_failure(int optRows_failure) {
    this.optRows_failure = optRows_failure;
  }
  public List<String> getRowtitle() {
    return rowtitle;
  }
  public List<List<String>> getFailrows() {
    return failrows;
  }
  public List<String> getFailmsgs() {
    return failmsgs;
  }
  public void setFailmsgs(List<String> failmsgs) {
    this.failmsgs = failmsgs;
  }
}

5、导入实现方法:

public static void main(String[] args){
    ExcelImportUtil importUtil;
    try {
      importUtil = new ExcelImportUtil("d:/data.xls",0, new HxlsInterfaceImpl());
      importUtil.process();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java操作Excel技巧总结》、《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》及《Java操作DOM节点技巧总结》

希望本文所述对大家java程序设计有所帮助。

(0)

相关推荐

  • Java 使用poi把数据库中数据导入Excel的解决方法

    Java 利用poi把数据库中数据导入Excel 效果: 使用时先把poi包导入工程的path,注意只需要导入poi包即可,下载后有三个jar包 核心代码: 连接数据库:DBConnection.java 复制代码 代码如下: package org.xg.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;i

  • Java数据导入功能之读取Excel文件实例

    在编程中经常需要使用到表格(报表)的处理主要以Excel表格为主.下面给出用java读取excel表格方法: 1.添加jar文件 java导入导出Excel文件要引入jxl.jar包,最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件.下载地址:http://www.andykhan.com/jexcelapi/ 2.jxl对Excel表格的认识 (1)每个单元格的位置认为是由一个二维坐标(i,j)给定,其中i表示列,j表示

  • java实现excel导入数据的工具类

    导入Excel数据的工具类,调用也就几行代码,很简单的. 复制代码 代码如下: import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;import org.apache.commons.beanutils.BeanUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java.io.IOExc

  • Java用jxl读取excel并保存到数据库的方法

    项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单. 基本的思路: 把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数据存到list中,通过遍历list,得到map,存到数据库中. 首先导入jar包:在网上都有, 代码: 页面: 新模excel导入 <input type="file" name="excel" id="xinmu"> <input

  • Java如何将Excel数据导入到数据库

    本文实例为大家分享了Java将Excel数据导入到数据库的具体代码,供大家参考,具体内容如下 所用Jar包 1. sqljdbc4.jar 连接数据库的Jar包(根据数据库的不同进行选择,我用的SqlServer2008) 2.Jxl.jar 访问Excel的Jar包 package xsl; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; impo

  • Java实现Excel导入导出数据库的方法示例

    本文实例讲述了Java实现Excel导入导出数据库的方法.分享给大家供大家参考,具体如下: 由于公司需求,想通过Excel导入数据添加到数据库中,而导入的Excel的字段是不固定的,使用得通过动态创建数据表,每个Excel对应一张数据表,怎么动态创建数据表,可以参考前面一篇<java使用JDBC动态创建数据表及SQL预处理的方法>. 下面主要讲讲怎么将Excel导入到数据库中,直接上代码:干货走起~~ ExcellToObjectUtil 类 主要功能是讲Excel中的数据导入到数据库中,有几

  • Java解析Excel文件并把数据存入数据库

    前段时间做一个小项目,为了同时存储多条数据,其中有一个功能是解析Excel并把其中的数据存入对应数据库中.花了两天时间,不过一天多是因为用了"upload"关键字作为URL从而导致总报同一个错,最后在同学的帮助下顺利解决,下面我把自己用"POI"解析的方法总结出来供大家参考(我用的是SpingMVC和hibernate框架). 1.web.xml中的配置文件 web.xml中的配置文件就按照这种方式写,只需要把"application.xml"换

  • java实现Excel的导入、导出

    一.Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性.由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) 方式一.JXL导入  所需jar包 JXL.jar publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){ List<PutStorageInfo> infoList =newArrayList<Put

  • java导出数据库中Excel表格数据的方法

    本篇文章基于java把数据库中的数据以Excel的方式导出,欢迎各位大神吐槽: 1.基于maven jar包引入如下: <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency> 2.首先创建数据库对应的实体类VO :U

  • java导出数据库的全部表到excel

    本文实例为大家分享了java将某个数据库的表全部导出到excel中的方法,供大家参考,具体内容如下 第一步:如何用POI操作Excel @Test public void createXls() throws Exception{ //声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); //声明表 HSSFSheet sheet = wb.createSheet("第一个表"); //声明行 HSSFRow row = sheet.createR

  • Java实现把excel xls中数据转为可直接插入数据库的sql文件

    我的一贯风格,代码说明一切.. 废话不多说了,直接给大家贴代码了,具体代码如下所示: package Tools; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; im

随机推荐