Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用openpyxl将数组添加到excel(格式问题)_Python_Python 3.x_Django_Openpyxl - Fatal编程技术网

Python 使用openpyxl将数组添加到excel(格式问题)

Python 使用openpyxl将数组添加到excel(格式问题),python,python-3.x,django,openpyxl,Python,Python 3.x,Django,Openpyxl,我使用“openpyxl”将一个数组放入excel文件中,供用户在django网站上下载 数据通过以下循环进入excel fine: for row_num, row in enumerate(matrix, 3): for col_num, item in enumerate(row, col_start - 12): ws.cell(column=col_num, row=row_num, value=item) wb.save(excel_file_path_from_d

我使用“openpyxl”将一个数组放入excel文件中,供用户在django网站上下载

数据通过以下循环进入excel fine:

for row_num, row in enumerate(matrix, 3):
 for col_num, item in enumerate(row, col_start - 12):
        ws.cell(column=col_num, row=row_num, value=item)
wb.save(excel_file_path_from_db)
我的问题是,当数据进入excel时,它不是数字格式。我认为这可能是因为我通过以下方式从用户输入中获取atix值:

matrix = ast.literal_eval(list(request.GET)[0])
print(matrix)

有没有办法让我的矩阵以数字格式进入excel文件?

听起来你有字符串值。函数的作用是:设置生成的openpyxl.cell.cell.cell.cell对象的data_type属性,以尝试匹配该属性

如果将cell()的输出分配给变量(例如,cell):
cell=ws.cell(列=col\u num,行=row\u num,值=item)

然后
打印(cell.data\u type)
,您可能会得到文本的“t”

调用ws.cell()时,应该能够在
项上使用强制转换运算符:

另一个对我有效的选择:

for row_num, row in enumerate(matrix, 3):
 for col_num, item in enumerate(row, col_start - 12):
    cell = ws.cell(column=col_num, row=row_num, value=item)
    cell.data_type = "n"
wb.save(excel_file_path_from_db)
for row_num, row in enumerate(matrix, 3):
 for col_num, item in enumerate(row, col_start - 12):
    cell = ws.cell(column=col_num, row=row_num, value=item)
    cell.data_type = "n"
wb.save(excel_file_path_from_db)