openpyxl:ValueError:dtype:int64到Excel

时间:2017-06-06 14:52:07

标签: excel merge openpyxl

我正在合并两个.csv文件并编写一个新的.xlsx文件。

代码如下:

    SELECT
    c.session_id, c.net_transport, c.encrypt_option,
    s.status,
    c.auth_scheme, s.host_name, s.program_name,
    s.client_interface_name, s.login_name, s.nt_domain,
    s.nt_user_name, s.original_login_name, c.connect_time,
    s.login_time
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s
    ON c.session_id = s.session_id
--WHERE c.session_id = @@SPID;
--WHERE status = 'sleeping'
ORDER BY c.connect_time ASC 

错误:ValueError:dtype:int64到Excel

1 个答案:

答案 0 :(得分:1)

您目前正在尝试向单元格写入多个pd.Series。要获得一个值,您必须使用.iloc,例如:

for i in range(len(ODNEW)):
   sheet['A'+ str(i+1)].value = ODNEW['comm'].iloc[i] # start writing on A1 not A0
   sheet['B'+ str(i+1)].value = ODNEW['ONode'].iloc[i]
   sheet['C'+ str(i+1)].value = ODNEW['DNode'].iloc[i]
   sheet['D'+ str(i+1)].value = ODNEW['quantity'].iloc[i]
   sheet['E'+ str(i+1)].value = ODNEW['startRR'].iloc[i]

但你可以直接迭代表:

for i,j in ODNEW.iterrows():
   sheet['A'+ str(i+1)].value = j['ONode']
   sheet['C'+ str(i+1)].value = j['DNode']
   sheet['D'+ str(i+1)].value = j['quantity']
   sheet['E'+ str(i+1)].value = j['startRR']

否则请查看此处提供的效用函数:http://openpyxl.readthedocs.io/en/default/pandas.html