如何转换Django excel导出文件中的行和列

如何转换Django excel导出文件中的行和列,django,python-3.x,django-views,xlwt,django-excel,Django,Python 3.x,Django Views,Xlwt,Django Excel,下面是我正在努力转换数据的示例代码 def export_users_xls(request): response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename="users.xls"' wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('User

下面是我正在努力转换数据的示例代码

def export_users_xls(request):
   response = HttpResponse(content_type='application/ms-excel')
   response['Content-Disposition'] = 'attachment; filename="users.xls"'

  wb = xlwt.Workbook(encoding='utf-8')
  ws = wb.add_sheet('Users')

   # Sheet header, first row
   row_num = 0

   font_style = xlwt.XFStyle()
   font_style.font.bold = True

   columns = ['Username', 'First name', 'Last name', 'Email address', ]

   for col_num in range(len(columns)):
    ws.write(row_num, col_num, columns[col_num], font_style)

   # Sheet body, remaining rows
   font_style = xlwt.XFStyle()

    rows = User.objects.all().values_list('username', 'first_name', 'last_name', 'email')
   for row in rows:
    row_num += 1
    for col_num in range(len(row)):
        ws.write(row_num, col_num, row[col_num], font_style)

   wb.save(response)
   return response
所有行缩进都是正确的 我的目标是从上面的代码中创建一个excel格式的文件。这很好,但问题是行和列没有相互改变


如果您建议我使用任何其他库来执行excel导出文件,该文件可以转置数据

假设您只想转置工作表中的数据(并且假设django excel具有相同的功能):


所有行缩进都是正确的

在执行上述代码后,我得到了这个错误异常值:“Sheet”对象没有属性“get”@Access\u Doomer,您的输入数据有问题,需要一个字典。rows=User.objects.All().values\u list('username','first\u name','last\u name…)。。。。。。。
import pyexcel as p
my_dic = { "col1": [1, 2, 3], "col2": [4, 5, 6], "col3": [7, 8, 9]}
sheet = p.get_sheet(adict=my_dic)

# sheet now is this:
# pyexcel_sheet1:
# +------+------+------+
# | col1 | col2 | col3 |
# +------+------+------+
# | 1    | 4    | 7    |
# +------+------+------+
# | 2    | 5    | 8    |
# +------+------+------+
# | 3    | 6    | 9    |
# +------+------+------+

sheet.transpose()

# sheet now is this:
# pyexcel_sheet1:
# +------+---+---+---+
# | col1 | 1 | 2 | 3 |
# +------+---+---+---+
# | col2 | 4 | 5 | 6 |
# +------+---+---+---+
# | col3 | 7 | 8 | 9 |
# +------+---+---+---+
def export_users_xls(request):
   response = HttpResponse(content_type='application/ms-excel')
   response['Content-Disposition'] = 'attachment; 
 filename="users.xls"'

  wb = xlwt.Workbook(encoding='utf-8')
  ws = wb.add_sheet('Users')

   # Sheet header, first row
   row_num = 0

  font_style = xlwt.XFStyle()
  font_style.font.bold = True

  columns = ['Username', 'First name', 'Last name', 'Email address', ]

  for col_num in range(len(columns)):
    ws.write(col_num, row_num, columns[col_num], font_style)

  # Sheet body, remaining rows
 font_style = xlwt.XFStyle()

   rows = User.objects.all().values_list('username', 'first_name', 'last_name', 'email')
 for row in rows:
  row_num += 1
  for col_num in range(len(row)):
      ws.write(col_num+1, row_num, row[col_num], font_style)

 wb.save(response)
 return response