如何将Excel工作表插入数据库

时间:2018-07-19 11:06:14

标签: python sql-server excel pandas dataframe

我想用熊猫将excel表插入数据库(Microsoft SQL Server)。我首先使用ExcelFile而不是read_excel并使用了解析,但是解析不适用于read_excel。 我更喜欢使用read_excel,因为文件是根据需要形成的

这是我的代码:

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))

这是我要插入数据库的文件:

  --------------------------------------------------------------------------
   |         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  |
   -----------------------------------------------------------------------------

我很感谢所有答案

1 个答案:

答案 0 :(得分:0)

如果您拥有Excel文件中的所有数据,则只需将其粘贴到数据库中。 您可以使用

log.go

这是链接: to_sql
希望对您有帮助。