一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java實現批量導入excel表格數據到數據庫中的方法

Java實現批量導入excel表格數據到數據庫中的方法

2021-01-27 11:25CharlinGod JAVA教程

這篇文章主要介紹了Java實現批量導入excel表格數據到數據庫中的方法,結合實例形式詳細分析了java導入Excel數據到數據庫的具體步驟與相關操作技巧,需要的朋友可以參考下

本文實例講述了Java實現批量導入excel表格數據到數據庫中的方法。分享給大家供大家參考,具體如下:

1、創建導入抽象類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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、創建導入接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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、創建實現類, 在這個方法實現把導入的數據添加到數據庫中

?
1
2
3
4
5
6
7
8
9
10
11
12
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、導入工具實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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、導入實現方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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程序設計有所幫助。

原文鏈接:http://blog.csdn.net/lovoo/article/details/77905613

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年人黄视频在线观看 | 欧美日韩亚洲区久久综合 | 毛片视频在线免费观看 | 欧美人禽杂交av片 | 欧美穿高跟鞋做爰 | 近亲乱中文字幕 | 果冻传媒mv在线观看入口免费 | 午夜伦理 第1页 | 国产成人一区二区三区在线视频 | 男女乱淫真视频播放网站 | 欧美亚洲第一页 | 99精品国产综合久久久久 | 小伙无套内射老女人 | videos变态极端 | 黑人操日本妞 | 男人狂躁女人下面的视频免费 | 韩国男女做性全过程视频 | www亚洲国产 | 国产欧美精品专区一区二区 | 精品无人区麻豆乱码1区2 | 国产青青草 | a级片在线播放 | 亚洲国产美女精品久久久久 | 国产91成人精品亚洲精品 | 极品丝袜小说全集 | 欧美丰满大乳大屁在线观看股 | 午夜影院和视费x看 | 久久中文字幕免费高清 | 国产精品青青青高清在线密亚 | 农村妇女野外牲交一级毛片 | 91久久综合九色综合欧美98 | 欧美成人精品福利网站 | 三级理论在线播放大全 | 国产精品视频二区不卡 | 波多野结衣在线中文 | 欧美美女一区二区三区 | sss视频在线精品 | 古代翁熄系小说辣文 | 免费成年人在线视频 | 色综合久久综精品 | 暖暖视频免费观看视频中国.韩剧 |