我需要有关此问题的帮助,我正在将数据表导出到可以正常工作的excel文件中。问题是我试图使列之一成为文档的超链接,但是当它开始建立excel文件时,出现此错误。预先感谢您的帮助。
“相对URI不支持此操作。”
using OfficeOpenXml;
using OfficeOpenXml.Table;
public void ExportExcelFile(System.Data.DataTable excelData, string excelSheetName)
{
using (var package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add(excelSheetName);
int rowCount = 1;
foreach (DataRow rw in excelData.Rows)
{
//excelData.Columns.RemoveAt(0);
rowCount += 1;
for (int i = 1; i < excelData.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
ws.Row(1).Style.Font.Bold = true;
ws.Cells[1, i].Value = excelData.Columns[i - 1].ColumnName;
}
ws.Column(i).AutoFit();
//THIS IS THTE PROBLEM
string hyperlink = "<a href='~/Documents/DownloadFile.aspx?id=" + rw["DocPk"].ToString() + "'>" + rw["Drawing Id"].ToString() + "</a>";
ws.Cells[i, 7].Hyperlink = new Uri(hyperlink, UriKind.RelativeOrAbsolute);
ws.Cells[rowCount, i].Value = rw[i - 1].ToString();
}
}
MemoryStream Result = new MemoryStream();
package.SaveAs(Result);
Response.ClearContent();
Response.OutputStream.Write(Result.GetBuffer(), 0, Result.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
byte[] byteArray = Result.ToArray();
Response.AddHeader("Content-Disposition", "attachment; filename=" + excelSheetName + ".xlsx");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(byteArray);
Response.End();
}
}
Excel根本不支持相对URL,因为Excel文档没有相对于的URL 。您需要完全限定所有URL:
string hyperlink = String.Format("http://{0}/Documents/DownloadFile.aspx?id={1}",
Page.Request.Url.Host,
rw["DocPk"].ToString());
ws.Cells[rowCount, i].Hyperlink = new Uri(hyperlink, UriKind.Absolute);
ws.Cells[rowCount, i].Value = rw["Drawing Id"];
请注意完全限定的URL,其中包含http://和UriKind.Absolute
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句