PYTHON + EXCEL

dan84 marko_m at hotmail.it
Tue Nov 14 08:32:26 EST 2006


I write my code :
#inizializzazioni varie che tralascio


    #mi connetto al mio db mysql e recupero i dati che volgio inserire
nel file excel
    conn = MySQLdb.connect(host = "XXX", port = XXX, user = "XXX",
passwd = "XXX", db= "XXX")
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM XXX")
    allFields = cursor.description
    allRecords = cursor.fetchall()
    cursor.close()
    conn.close()


    #inizializzo gli strumenti per lavorare in python con excel
    xlApp = Dispatch("Excel.Application")
    #xlApp.Visible = 1
    xlApp.Workbooks.Add()


    #specifico che file utilizzare
    xlbook = xlApp.Workbooks.Open('C:/view.xls')
    i = 1
    j = 1


    #popolo il mio file excel
    for field in allFields :
        xlApp.ActiveSheet.Cells(i, j).Value = field[0]
        j = j + 1
    for records in allRecords :
        i = i + 1
        j = 1
        for item in records :
            xlApp.ActiveSheet.Cells(i, j).Value = item
            j = j + 1
    #salvo le modifiche effettuate
    xlbook.SaveAs('C:/view.xls')


    #scrivo le istruzioni che mi permettereanno di aprirlo in una
pagina html
    size = os.path.getsize('C:/view.xls')
    of = open("C:/view.xls","rb")
    content = of.read()
    of.close()


    print"Content-Type:application7vnd.ms-excel"
    print"content-disposition:inline;filename=view.xls"
    print"Content-length:%d \r\n"%size
    print content







I want open an excel document in an apache page.
I want write in excel document the information of a database mySQL and
after open the excel documen in a broswer page
Where is the problem?
Sorry for my english but I'm not speak english very well, I write in
this group because I think that in this forum there are very very good
programmer




More information about the Python-list mailing list