android通過jxl讀excel存入sqlite3資料庫_Android

來源:互聯網
上載者:User

複製代碼 代碼如下:

package com.demo.day20140228;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class DictExcelDemo {
 public static void main(String[] args) {
  DictExcelDemo ded = new DictExcelDemo();
  Connection conn = ded.getConnection();
  ded.readExcel_(conn);
 }

 private Connection getConnection(){
  Connection conn = null;
  try {
   Class.forName("org.sqlite.JDBC");
   conn = DriverManager.getConnection("jdbc:sqlite:database.db");
   Statement stat = conn.createStatement();
   stat.executeUpdate("create  table if not exists dictionary(enword varchar(200), cnword varchar(200));");// 建立一個表,兩列

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }

 private void readExcel_(Connection conn) {
  try {
   Workbook book = Workbook.getWorkbook(new File("English.xls"));
   PreparedStatement prep = conn.prepareStatement("insert into dictionary(enword,cnword) values(?,?);");

   for (int a = 0; a < 26; a++) {
    // 獲得第一個工作表對象
    Sheet sheet = book.getSheet(a);
    // 得到第一列第一行的儲存格
    // 得到第一列第一行的儲存格
    int columnum = sheet.getColumns();// 得到列數
    int rownum = sheet.getRows();// 得到行數
    for (int i = 1; i < rownum; i++)// 迴圈進行讀寫
    {// 行
     String key = "";
     String value = "";
     for (int j = 0; j < columnum; j++) {// 列
      Cell cell1 = sheet.getCell(j, i);
      String result = cell1.getContents();
      if (j == 0) {
       key += result;
      } else {
       value += result;
      }
     }
     // System.out.println(key+"=="+value);
     prep.setString(1, key);
     prep.setString(2, value);
     prep.addBatch();
    }
   }
   conn.setAutoCommit(false);
   prep.executeBatch();
   conn.setAutoCommit(true);
   conn.close();
   book.close();
  } catch (Exception e) {
   System.out.println(e);
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.