def render_data(self, request): from report import Report, Paragraph, Spacer, Table from django.http import HttpResponseRedirect from mysite.urls import tmpDir, tmp_url cells=[cell_format(a) for a in self.cells] rn=request.REQUEST.get("reportname",'') title=rn and rn or self.title if self.head=="": heads=[k for k in self.data[0].keys()] else: heads=self.head file_name=u"%s_%s.pdf"%(title, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) filecode=request.REQUEST.get("fielcode",'gb18030') whs=colwidths = [len(i.encode(filecode)) for i in heads] wcells=[[len(item.encode("gb18030")) for item in line] for line in cells[:40]] wcells.insert(0, whs) chars_cnt=sum(wcs) page_size=get_page_size(chars_cnt) p=Report() p.set_page_size(page_size[1], page_size[2]) allws=min(6, max(3.7, 2.85*(p.width-23)/chars_cnt)) #计算每个字符的最大可能宽度 page_head=(Paragraph(title, p.style_title), ) grid_head=[Paragraph(col_text, p.style_grid_head) for col_text in heads] p.colwidths=[(allws*item or 20) for item in wcs] p.grid_head_height=20 p.row_height=15 p.print_report(cells, page_size[3], grid_head, page_head, file_name=u"%s/%s"%(tmpDir(), file_name)) f="/"+tmp_url()+file_name return HttpResponseRedirect(f.encode("utf-8"))
def render_data(self, request): import xlwt from django.http import HttpResponse,HttpResponseRedirect from mysite.urls import tmpDir, tmp_url rn=request.REQUEST.get("reportname",'') title=rn and rn or self.title sheet_name=u"%s_%s"%(title, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) if self.head=="": heads=[k for k in self.data[0].keys()] else: heads=self.head lines=[xls_format(a) for a in self.cells] lines.insert(0, xls_format(self.head)) filecode=request.REQUEST.get('filecode','gb18030')#编码 wb = xlwt.Workbook(encoding=u"%s"%filecode) ws = wb.add_sheet(sheet_name) row_index=0 for row in lines: col_index=0 for col in row: ws.write(row_index,col_index,col) col_index+=1 row_index+=1 filename="%(d1)s_%(d2)s.xls"%{ "d1":u"%s"%title, "d2":datetime.datetime.now().strftime("%Y%m%d%H%M%S") } wb.save(u"%s/%s"%(tmpDir(), filename)) f="/"+tmp_url()+filename return HttpResponseRedirect(f.encode("utf-8"))
def render_data(self, request): import xlwt from django.http import HttpResponse, HttpResponseRedirect from mysite.urls import tmpDir, tmp_url rn = request.REQUEST.get("reportname", '') title = rn and rn or self.title sheet_name = u"%s_%s" % ( title, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) if self.head == "": heads = [k for k in self.data[0].keys()] else: heads = self.head lines = [xls_format(a) for a in self.cells] lines.insert(0, xls_format(self.head)) filecode = request.REQUEST.get('filecode', 'gb18030') #编码 wb = xlwt.Workbook(encoding=u"%s" % filecode) ws = wb.add_sheet(sheet_name) row_index = 0 for row in lines: col_index = 0 for col in row: ws.write(row_index, col_index, col) col_index += 1 row_index += 1 filename = "%(d1)s_%(d2)s.xls" % { "d1": u"%s" % title, "d2": datetime.datetime.now().strftime("%Y%m%d%H%M%S") } wb.save(u"%s/%s" % (tmpDir(), filename)) f = "/" + tmp_url() + filename return HttpResponseRedirect(f.encode("utf-8"))
def render_data(self, request): from report import Report, Paragraph, Spacer, Table from django.http import HttpResponseRedirect from mysite.urls import tmpDir, tmp_url import os cells = [cell_format(a) for a in self.cells] rn = request.REQUEST.get("reportname", '') file_path = tmpDir() + "/report_file" if not os.path.exists(file_path): os.makedirs(file_path) title = rn and rn or self.title if self.head == "": #print self.data[0] heads = [k for k in self.data[0].keys()] else: heads = self.head file_name = u"%s_%s.pdf" % ( title, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) filecode = request.REQUEST.get("fielcode", 'gb18030') whs = colwidths = [len(i.encode(filecode)) for i in heads] wcells = [[len(item.encode("gb18030")) for item in line] for line in cells[:40]] wcells.insert(0, whs) wcs = map(max, zip(*wcells)) #计算每列的最大值 chars_cnt = sum(wcs) page_size = get_page_size(chars_cnt) p = Report() p.set_page_size(page_size[1], page_size[2]) allws = min(6, max(3.7, 2.85 * (p.width - 23) / chars_cnt)) #计算每个字符的最大可能宽度 page_head = (Paragraph(title, p.style_title), ) grid_head = [ Paragraph(col_text, p.style_grid_head) for col_text in heads ] p.colwidths = [(allws * item or 20) for item in wcs] p.grid_head_height = 20 p.row_height = 15 p.print_report(cells, page_size[3], grid_head, page_head, file_name=u"%s/%s" % (file_path, file_name)) f = "/" + tmp_url() + "report_file/" + file_name return HttpResponseRedirect(f.encode("utf-8"))
def render_data(self, request): import xlwt from django.http import HttpResponse, HttpResponseRedirect from mysite.urls import tmpDir, tmp_url rn = request.REQUEST.get("reportname", '') title = rn and rn or self.title sheet_name = u"%s_%s" % ( 'Sheet', datetime.datetime.now().strftime("%Y%m%d%H%M%S")) file_path = tmpDir() + "/report_file" if not os.path.exists(file_path): os.makedirs(file_path) if self.head == "": #print self.data[0] heads = [k for k in self.data[0].keys()] else: heads = self.head lines = [xls_format(a) for a in self.cells] filecode = request.REQUEST.get('filecode', 'gb18030') #编码 wb = xlwt.Workbook(encoding=u"%s" % filecode) ws = wb.add_sheet(sheet_name) fnt = xlwt.Font() fnt.name = 'Arial' #fnt.colour_index = 4 #fnt.bold = True borders = xlwt.Borders() borders.left = 1 borders.right = 1 borders.top = 1 borders.bottom = 1 align = xlwt.Alignment() align.horz = xlwt.Alignment.HORZ_CENTER align.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() style.font = fnt style.borders = borders style.alignment = align row_index = 0 for row in lines: col_index = 0 for col in row: ws.write(row_index, col_index, col, style) ws.col(col_index).width = 0x0d00 + 2000 col_index += 1 row_index += 1 filename = "%(d1)s_%(d2)s.xls" % { "d1": u"%s" % title, "d2": datetime.datetime.now().strftime("%Y%m%d%H%M%S") } # response = HttpResponse(mimetype=u"application/vnd.ms-excel; charset=utf-8") # response["Content-Disposition"] = u"attachment;" # filename=%(d1)s_%(d2)s.xls"%{ # "d1":title, # "d2":datetime.datetime.now().strftime("%Y%m%d%H%M%S") # } # wb.save(response) # return response wb.save(u"%s/%s" % (file_path, filename)) f = "/" + tmp_url() + "report_file/" + filename return HttpResponseRedirect(f.encode("utf-8"))