读取excel数据到数据库里

时间:2022-02-08 22:59:19

用的是jxlJar

/**

 * 读取excel数据到数据库里

 */

private void readExcelToDB() {

    new Thread(new Runnable() {

        @Override

        public void run() {

            try {

                String filePath = "/sdcard/";

                String fileName = "307322.xlsx";

                File file = new File(filePath, fileName);

                FileInputStream is = new FileInputStream(file);

                Workbook book = Workbook.getWorkbook(is);

                // 获取表页数

                final int bookPage = book.getNumberOfSheets();

                Log.e("bookPage", "bookPage = " bookPage);

                // 获得第一个工作表对象

                Sheet sheet = book.getSheet(0);

                int Rows = sheet.getRows();

                for (int i = 1; i < Rows; i) {

                    String assetNumber = (sheet.getCell(0, i)).getContents();

                    String assetName = (sheet.getCell(3, i)).getContents();

                    String sTmp8 = sheet.getCell(8, i).getContents();

                    String sTmp9 = sheet.getCell(9, i).getContents();

                }

                book.close();

                runOnUiThread(new Runnable() {

                    @Override

                    public void run() {

                        Toast.makeText(ActivityMain.this, "导入成功", Toast.LENGTH_SHORT).show();

                    }

                });

            } catch (Exception e) {

                runOnUiThread(new Runnable() {

                    @Override

                    public void run() {

                        Toast.makeText(ActivityMain.this, "导入失败", Toast.LENGTH_SHORT).show();

                    }

                });

            }

        }

    }).start();

}