如何在excel文件中添加超链接到单元格的内容?

时间:2021-12-09 20:24:27

I export my data from database in an excel file:

我从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

It export my excel file. How to add an hyperlink to the content of a cell in this file? (country_name for example is a link to open a card in browser)

它导出我的excel文件。如何在此文件中添加指向单元格内容的超链接? (country_name例如是在浏览器中打开卡片的链接)

2 个解决方案

#1


0  

Taken from this thread:

取自这个主题:

from xlwt import Workbook, Formula

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

#2


1  

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

#1


0  

Taken from this thread:

取自这个主题:

from xlwt import Workbook, Formula

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

#2


1  

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