java遍歷Excel文件 並將信息存入數據庫

Excel格式:

 

代碼:

package com.mingwen.imagetest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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 com.google.gson.Gson;

public class Excel {
    static Gson gs = new Gson();
    static Connection conn = null;
    static String sql;
    static Statement stmt;
    static ResultSet rs = null;

    public static void main(String[] args) {
        try {
            abc();
            readExcel();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void abc() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://60.168.130.76:3306/gudi?characterEncoding=utf8&useSSL=true", "數據庫賬號",
                    "數據庫密碼");
            stmt = conn.createStatement();
        } catch (Exception e) {

        }

    }

    public static void readExcel() throws SQLException {
        Workbook wb = null;
        Sheet sheet = null;
        Row row = null;
        String cellData = null;
        String filePath = "C:\\Users\\Administrator\\Desktop\\12.xlsx";
        wb = readExcel(filePath);
        if (wb != null) {
            // 獲取第一個sheet
            sheet = wb.getSheetAt(0);
            // 獲取最大行數
            int rownum = sheet.getPhysicalNumberOfRows();
            // 獲取第一行
            row = sheet.getRow(0);
            // 獲取最大列數
            int colnum = row.getPhysicalNumberOfCells();
            for (int i = 1; i < rownum; i++) {
                Map<String, String> map = new LinkedHashMap<String, String>();
                row = sheet.getRow(i);
                if (row != null) {
                    for (int j = 0; j < colnum; j++) {
                        cellData = (String) getCellFormatValue(row.getCell(j));
                        String a = (String) getCellFormatValue(row.getCell(j + 1));
                        j = j + 1;
                        // map.put(a, cellData);
                        String sql2 = "SELECT * FROM `express_code` where name =\"" + cellData + "\"";
                        rs = stmt.executeQuery(sql2);
                        List<Integer> levels = new ArrayList<Integer>();
                        while (rs.next()) {
                            levels.add(rs.getInt("id"));
                        }

                        if (levels.size() > 0) {
                            for (Integer integer : levels) {
                                sql = "update express_code set kdniao=\"" + a + "\" where id=\"" + integer + "\"";
                                stmt.execute(sql);
                            }
                        } else {
                            sql = "INSERT INTO express_code(kdniao,name) VALUES(\"" + a + "\",\"" + cellData + "\")";
                            stmt.execute(sql);
                        }

                    }
                } else {
                    break;
                }
            }
        }
    }

    // 讀取excel
    public static Workbook readExcel(String filePath) {
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                return wb = new XSSFWorkbook(is);
            } else {
                return wb = null;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    public static Object getCellFormatValue(Cell cell) {
        Object cellValue = null;
        if (cell != null) {
            // 判斷cell類型
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC: {
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA: {
                // 判斷cell是否爲日期格式
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 轉換爲日期格式YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                } else {
                    // 數字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING: {
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        } else {
            cellValue = "";
        }
        return cellValue;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章