Posts

Showing posts from August, 2022

saving and dwnloading excel file in django

  import pandas as pd import os import openpyxl import xlwt def Category1_excel ( request ):      #saving file  category1 =Download . objects . all () file =[] for i in category1 : p = i .column1 s = i .column2 q = i .column3 row =[ p , s , q , e , l ] file . append ( row ) document = pd . DataFrame ( file ) document . to_excel ( 'media/file_downloads/category1.xlsx' )           #downloading file      category1 =Download . objects . all () response = HttpResponse ( content_type = 'application/ms-excel' ) response [ 'Content-Disposition' ] = 'attachment; filename="file.xls"' wb = xlwt . Workbook ( encoding = 'utf-8' ) ws = wb . add_sheet ( "sheet1" ) font_style = xlwt . XFStyle () font_style . font . bold = True row_num = 0 ws .write( row_num , 0 , 'PURCHASED GOODS' , font_style ) ws .write( row_n

How to save and download a csv file in django

  import pandas as pd   def Category1_csv ( request ): category1 =Database . objects . all () file =[] for i in category1 : p = i .abc row =[ p ,... ] file . append ( row )      #to save a file document = pd . DataFrame ( file ) document . to_csv ( 'media/file_downloads/category1.csv' ) #return HttpResponse("file createrd")      # to download a file data = open ( os . path . join ( settings .BASE_DIR, 'media/file_downloads/category1.csv' ), 'r' ). read () resp = HttpResponse ( data ) resp [ 'Content-Disposition' ] = 'attachment;filename=csv file.csv' return resp