@@ -129,21 +129,17 @@ def export_query(): | |||||
file_format_type = data["file_format_type"] | file_format_type = data["file_format_type"] | ||||
if file_format_type == "Excel": | if file_format_type == "Excel": | ||||
data = run(report_name, filters) | data = run(report_name, filters) | ||||
data = frappe._dict(data) | data = frappe._dict(data) | ||||
# convert to xlsx | |||||
import openpyxl | |||||
from cStringIO import StringIO | |||||
wb = openpyxl.Workbook() | |||||
ws = wb.active | |||||
ws.append(data.columns) | |||||
for row in data.result: | |||||
ws.append(row) | |||||
columns = get_columns_dict(data.columns) | |||||
content = [] | |||||
for col in columns.values(): | |||||
content.append(col["label"]) | |||||
xlsx_file = StringIO() | |||||
wb.save(xlsx_file) | |||||
from frappe.utils.xlsxutils import make_xlsx | |||||
xlsx_file = make_xlsx([content] + data.result, "Query Report") | |||||
frappe.response['filename'] = report_name + '.xlsx' | frappe.response['filename'] = report_name + '.xlsx' | ||||
frappe.response['filecontent'] = xlsx_file.getvalue() | frappe.response['filecontent'] = xlsx_file.getvalue() | ||||
@@ -338,6 +334,7 @@ def get_columns_dict(columns): | |||||
else: | else: | ||||
col_dict["fieldtype"] = col[1] | col_dict["fieldtype"] = col[1] | ||||
col_dict["label"] = col[0] | |||||
col_dict["fieldname"] = frappe.scrub(col[0]) | col_dict["fieldname"] = frappe.scrub(col[0]) | ||||
# dict | # dict | ||||
@@ -112,13 +112,11 @@ def export_query(): | |||||
for i, row in enumerate(ret): | for i, row in enumerate(ret): | ||||
data.append([i+1] + list(row)) | data.append([i+1] + list(row)) | ||||
from cStringIO import StringIO | |||||
if file_format_type == "CSV": | if file_format_type == "CSV": | ||||
# convert to csv | # convert to csv | ||||
import csv | import csv | ||||
from cStringIO import StringIO | |||||
f = StringIO() | f = StringIO() | ||||
writer = csv.writer(f) | writer = csv.writer(f) | ||||
@@ -133,16 +131,8 @@ def export_query(): | |||||
elif file_format_type == "Excel": | elif file_format_type == "Excel": | ||||
# convert to xlsx | |||||
import openpyxl | |||||
wb = openpyxl.Workbook() | |||||
ws = wb.active | |||||
for row in data: | |||||
ws.append(row) | |||||
xlsx_file = StringIO() | |||||
wb.save(xlsx_file) | |||||
from frappe.utils.xlsxutils import make_xlsx | |||||
xlsx_file = make_xlsx(data, doctype) | |||||
frappe.response['filename'] = doctype + '.xlsx' | frappe.response['filename'] = doctype + '.xlsx' | ||||
frappe.response['filecontent'] = xlsx_file.getvalue() | frappe.response['filecontent'] = xlsx_file.getvalue() | ||||
@@ -0,0 +1,26 @@ | |||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors | |||||
# MIT License. See license.txt | |||||
from __future__ import unicode_literals | |||||
import frappe | |||||
from frappe.utils import encode, cstr, cint, flt, comma_or | |||||
import openpyxl | |||||
from cStringIO import StringIO | |||||
from openpyxl.styles import Font | |||||
# return xlsx file object | |||||
def make_xlsx(data, sheet_name): | |||||
wb = openpyxl.Workbook(write_only=True) | |||||
ws = wb.create_sheet(sheet_name, 0) | |||||
row1 = ws.row_dimensions[1] | |||||
row1.font = Font(name='Calibri',bold=True) | |||||
for row in data: | |||||
ws.append(row) | |||||
xlsx_file = StringIO() | |||||
wb.save(xlsx_file) | |||||
return xlsx_file |