from openpyxl import Workbook
from django.http import HttpResponse
from openpyxl.writer.excel import save_virtual_workbook

def xls_export(data, filename):
    print('xls_export')

    # wb = Workbook()
    # ws = wb.active
    #
    # r = 1
    # for row in data:
    #     c = 1
    #     for val in row.values():
    #         try:
    #             ws.cell(row=r, column=c).value = val
    #         except:
    #             ws.cell(row=r, column=c).value = str(val)
    #         c += 1
    #
    #     r += 1
    #
    # dims = {}
    # for row in ws.rows:
    #     for cell in row:
    #         if cell.value:
    #             dims[cell.column] = max((dims.get(cell.column, 0), len(str(cell.value))))
    # for col, value in dims.items():
    #     ws.column_dimensions[col].width = value

    # filepath = "/demo.xlsx"
    # wb.save(filepath)

    # output = BytesIO()
    # wb.save(output)

    from ..office_documents_utils import get_xls_file_by_data_list
    xls_file = get_xls_file_by_data_list(data)

    response = HttpResponse(xls_file, content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)

    return response