import MySQLdb
import xlwt
import StringIO
from django.shortcuts import HttpResponse
from MySQLdb.cursors import DictCursor
conn = MySQLdb.connect (host = '192.168.2.4 ', user = 'root', passwd = 'zj88friend', db = 'zz91crm', port = 3306, charset = 'utf8', cursorclass = MySQLdb.cursors.DictCursor) wherein cursorclass # set the return data type as a dictionary
cur = conn.cursor () # Get the cursor
export_xls DEF (Request):
sql = 'the SELECT the above mentioned id, label from category'
. conn.cursor () the Execute (sql) # execute sql statement
conn.cursor () fetchall () # get query results.
the Response = HttpResponse (content_type = ' application / vnd.ms-excel ') # returns excel file specified
response [' Content-Disposition '] =' attachment; filename = export_agencycustomer.xls '# returns the specified file name
wb = xlwt.Workbook (encoding =' utf -8 ') coding type is set to # UTF8
Sheet wb.add_sheet = (U' category ') #excel was added category
sheet.write (0,0,' ID ')
sheet.write (0,1,' name ')
Row . 1 =
for in Result List:
sheet.write (Row, 0, List [ 'ID'])
sheet.write (Row,. 1, List [ 'label'])
Row = Row +. 1
Output = the StringIO.StringIO()
wb.save(output)
output.seek(0)
response.write(output.getvalue())
return response