Python批量匯入Excel資料到MySQL
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/11/20 14:30
# @Author : lumia98@vip.qq.com
# @File : Excel
# @Software: PyCharm
import xlrd
import pymysql
class Add_Excel_Data_To_MySQL(object):
"""
Excel表格資料批量匯入到MySQL庫
"""
# pymysql連線
def conn_pymysql(self, host, dbuser, dbpass, dbname):
# 連線MySQL
self.conn = pymysql.connect(host=host, user=dbuser, passwd=dbpass, db=dbname)
# 獲得遊標物件, 用於逐行遍歷資料庫資料
self.cursor = self.conn.cursor()
def excel_path(self, file_path, excle_table_name):
self.excel = xlrd.open_workbook(file_path)
self.sheet_name = self.excel.sheet_by_name(excle_table_name)
# 迴圈Excel表格資料,並且寫入到MySQL
def for_excel_insert_mysql(self, db01, db02, db03, db04, db05, db06, db07):
"""
如果有其他欄位請新增
:param db01: MySQL表裡的第一個需要手動新增資料的欄位
:param db02: MySQL表裡的第二個需要手動新增資料的欄位
:param db03: MySQL表裡的第三個需要手動新增資料的欄位
:param db04: MySQL表裡的第四個需要手動新增資料的欄位
:param db05: MySQL表裡的第五個需要手動新增資料的欄位
:param db06: MySQL表裡的第六個需要手動新增資料的欄位
:param db07: MySQL表裡的第七個需要手動新增資料的欄位
:return:
"""
# 插入到MySQL
self.query = "INSERT INTO machine ({}, {}, {}, {}, {}, {}, {}) VALUES (%s, %s, %s, %s, %s, %s, %s)".format(db01,db02,db03,db04,db05,db06,db07)
# 建立一個for迴圈迭代讀取xls檔案每行資料的;
# 從第二行開始匯入;
# 如果沒有標題,則1改成0
for r in range(1, self.sheet_name.nrows):
# 匯入7列Excel資料(從第一列開始匯入);
# 如果匯入的列增加或者減少,請根據實情;
excel_01 = self.sheet_name.cell(r, 0).value,
excel_02 = self.sheet_name.cell(r, 1).value,
excel_03 = self.sheet_name.cell(r, 2).value,
excel_04 = self.sheet_name.cell(r, 3).value,
excel_05 = self.sheet_name.cell(r, 4).value,
excel_06 = self.sheet_name.cell(r, 5).value,
excel_07 = self.sheet_name.cell(r, 6).value
self.values = (excel_01, excel_02, excel_03, excel_04, excel_05, excel_06, excel_07)
# 執行sql語句
self.cursor.execute(self.query, self.values)
# 提交
self.conn.commit()
# 關閉遊標
self.cursor.close()
# 關閉資料庫連線
self.conn.close()
self.columns = str(self.sheet_name.ncols)
self.rows = str(self.sheet_name.nrows)
print("一共匯入了{}列, {}行資料!".format(self.columns, self.rows))
if __name__ == '__main__':
a = Add_Excel_Data_To_MySQL()
# 連線MySQL
a.conn_pymysql(host="172.211.13.11",dbuser="root",dbpass="123456",dbname="manage")
# Excel檔案路徑
a.excel_path(r"C:\Users\Super\Desktop\machine.xls", "table")
# Excel資料插入MySQL
a.for_excel_insert_mysql(
db01="cname_id",
db02="machine_name",
db03="machine_sn",
db04="monitor_name",
db05="monitor_sn",
db06="to_username",
db07="depart_id"
)
相關文章
- 從EXCEL匯入資料到SQL SERVERExcelSQLServer
- [pb]從excel匯入資料到datawindowExcel
- SQLServer匯出匯入資料到MySQLServerMySql
- 【Mysql】匯出資料到excel檔案中MySqlExcel
- 使用mysqlimport匯入資料到mysqlMySqlImport
- 利用Excel匯入資料到SAP C4CExcel
- 使用load data匯入資料到mysqlMySql
- .NET Core使用NPOI將Excel中的資料批量匯入到MySQLExcelMySql
- PHP匯出大量資料到excel表格PHPExcel
- MySQL 批量匯入資料優化MySql優化
- 使用Excel匯入資料到SAP Cloud for Customer系統ExcelCloud
- Java 匯入資料到Excel並提供檔案下載介面JavaExcel
- spark sql與mysql 資料載入與匯出資料到mysqlSparkMySql
- Laravel- 後臺批量匯入 ExcelLaravelExcel
- Vue匯出資料到Excel電子表格VueExcel
- 把Excel表資料匯入到mysqlExcelMySql
- 從Excel到匯入MYSQL資料庫ExcelMySql資料庫
- Java不寫檔案,LOAD DATA LOCAL INFILE大批量匯入資料到MySQL的實現JavaMySql
- excel 表匯入到mysqlExcelMySql
- Elasticsearch批量匯入資料指令碼(python)Elasticsearch指令碼Python
- C#快速匯出百萬級資料到Excel方法C#Excel
- SQL通過bcp匯出資料到excel檔案SQLExcel
- 用Perl從oracle匯出百萬級資料到excelOracleExcel
- Python匯出資料到Excel表格-NotImplementedError: formatting_info=True not yet implementedPythonExcelErrorORM
- 如何從網頁中匯入外部資料到Excel2003網頁Excel
- 匯入網頁資料到 Google Sheet網頁Go
- 使用sqlldr匯入文字資料到oracleSQLOracle
- Timesten匯入資料到Cache Group
- MATLAB匯入txt和excel檔案技巧彙總:批量匯入、單個匯入MatlabExcel
- SSIS中匯入Excel資料到資料庫的一個常見小問題Excel資料庫
- asp.net中利用NPOI匯出資料到excel中ASP.NETExcel
- Python使用pymysql和xlrd2將Excel資料匯入MySQL資料庫PythonMySqlExcel資料庫
- mysql匯入文字或excel檔案MySqlExcel
- Redis批量匯入文字資料Redis
- Excel 表匯入資料Excel
- Oracle 資料匯入ExcelOracleExcel
- excel導資料到PostgresqlExcelSQL
- Excel資料快速匯入mysql的幾個辦法ExcelMySql