gpt4 book ai didi

python - Django DateField 中 Xlswriter 的输出不正确

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:19 25 4
gpt4 key购买 nike

我使用 xlsxwriter 包从我的 django 项目(完全是我的模型)中获取 excel 输出。这些是我的字段:

date1 = models.DateField(default=datetime.date.today)
date2 = models.DateField(null=True,blank=True)

所有字段的输出都完全正确地嵌入到Excel中,唯一的问题是DateField的字段没有正确输出。这些字段的输出:

43603

我应该怎么做才能解决这个问题?

最佳答案

我终于找到了答案!我阅读了有关 xlsxwriter 的更多信息,我还查看了 this文档。写在这里:

Dates and times in Excel are represented by real numbers, for example “Jan 1 2013 12:00 PM” is represented by the number 41275.5.

The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day.

最后的答案是:

To display the number as a date you must apply an Excel number format to it.

这里有一些例子:

import xlsxwriter    
workbook = xlsxwriter.Workbook('date_examples.xlsx')
worksheet = workbook.add_worksheet()

# Widen column A for extra visibility.
worksheet.set_column('A:A', 30)

# A number to convert to a date.
number = 41333.5

# Write it as a number without formatting.
worksheet.write('A1', number) # 41333.5

format2 = workbook.add_format({'num_format': 'dd/mm/yy'})
worksheet.write('A2', number, format2) # 28/02/13

format3 = workbook.add_format({'num_format': 'mm/dd/yy'})
worksheet.write('A3', number, format3) # 02/28/13


workbook.close()

关于python - Django DateField 中 Xlswriter 的输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com