2013-07-04 109 views
1

我從數據庫中一個Excel文件導出我的數據:如何在Excel文件中添加超鏈接到單元格的內容?

response = HttpResponse(mimetype="application/ms-excel") 
response['Content-Disposition'] = 'attachment; filename=Countries.xls' 
wb = xlwt.Workbook() 
ws = wb.add_sheet('Countries') 
ws.write(0, 0, 'Country ID') 
ws.write(0, 1, 'Country Name') 
index = 1 
for country in countries: 
    ws.write(index, 1, country.country_id) 
    ws.write(index, 1, country.country_name) 
    index += 1 
wb.save(response) 
return response 

導出我的Excel文件。如何在此文件中添加超鏈接到單元格的內容? (country_name例如在瀏覽器中打開一個卡的鏈接)從this thread兩者

回答

0

from xlwt import Workbook, Formula 

wb = Workbook() 
sheet = wb.add_sheet('testing links') 
link = 'HYPERLINK("http://stackoverflow.com/"; "SO")' 
sheet.write(0, 0, Formula(link)) 
wb.save('test.xls') 
+0

這使該全單元可點擊!那麼單元格的內容呢? – Drwhite

+0

我不認爲這是可能的。雖然會很高興成爲錯誤。 – alecxe

+0

不要高興! matino製作解決方案 – Drwhite

1
worksheet.write(index, 1, xlwt.Formula('HYPERLINK("%s";"TITLE")' % country_name)) 
相關問題