简体   繁体   中英

How can I insert the my excel sheet to the database

I want to insert my excel table to the database(Microsoft SQL Server) with pandas. I used first ExcelFile rather then read_excel and used parse, but parse dont work with read_excel. I prefer to use read_excel since the file is formed as desired

This is my code:

if __name__ == '__main__':
    cursor = get_sql_conn().cursor()
    local_files = 'C:\\Users\\dersimw\\Source\Repos\\nordpoolAnalyse\\data\\2011-3.xlsx'
    cols = [1,7,13,19,25, 31, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91, 97, 103, 109, 115, 121, 127, 133, 139]
    excelFile = pd.read_excel(local_files, sheet_name= "01", header = None, usecols= cols)
    print(excelFile.head(6))

and this is my file that I want to insert to the database:

  --------------------------------------------------------------------------
   |         0       1       2       3    ...        20      21      22      23 |
    ---------------------------------------------------------------------------
   | 0       0       1       2       3   ...        20      21      22      23  |
   | 1     112     112     112     112   ...     227.5     227      52      52  |
   | 2  1573.2  1575.2  1596.1  1603.1   ...    1896.8  1833.8  1728.3  1649.3  |
   | 3     NaN     NaN     NaN     NaN   ...       NaN     NaN     NaN     NaN  |
   | 4     NaN     NaN     NaN     NaN   ...       NaN     NaN     NaN     NaN  |
   | 5  2652.3  2505.9  2662.6  2712.7   ...    1613.2    2932    2962    2897  |
   -----------------------------------------------------------------------------

I appreciate all answer

If you have all the data from the Excel file, you just have to paste it into the database. You can use

pandas.DataFrame.to_sql

here is the link: to_sql
I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM