在DataTable C#中插入超链接

我想添加超链接将dataTable导出到excel中

但是excel不能显示超链接属性,只能显示string。

数据表input字段是否有任何限制?

下面是我的格式化DATATABLE的代码

int dt_current_row = 0; string[] Days = testDays.ToArray(); for (int i = 0; i < tableOriginal.Rows.Count; i++) { string wo_number = handle(tableOriginal.Rows[i]["WO_NUMBER"].ToString()); string date = handle(tableOriginal.Rows[i]["CREATE_DATE"].ToString()); if (i == 0) { // Insert New DataRow dr = dt.NewRow(); dt.Rows.Add(dr); dr["WO_NUMBER"] = wo_number; for (int x = 1; x < dt.Columns.Count; x++) { int z = Convert.ToInt32(Math.Floor(0.5 * (x - 1))); //search Date if (tableOriginal.Rows[i]["CREATE_DATE"].ToString() == Days[z]) { //if equal , insert num and ID object xx = tableOriginal.Rows[i]["NUM"]; int num = Convert.ToInt32(xx); dr["PHOTO_" + z.ToString()] = num; object yy = tableOriginal.Rows[i]["PHOTO_ID"]; string photo = Convert.ToString(yy); dr["ID_" + z.ToString()] = HttpContext.Current.Server.HtmlEncode(httpLink + photo); break; } } } 

….和在Excel中导出单元格到DataTable的单元格部分文档xls我试图设置公式,但它不起作用

 foreach (DataRow r in result.Rows) { currentCol = 1; foreach (DataColumn c in result.Columns) { string temp = c.ColumnName.ToString().Substring(0,2); if (temp.Equals("ID")) { httpLinkForPhoto = r[currentCol - 1].ToString(); if (!httpLinkForPhoto.Equals(null)) { string formula = "=HYPERLINK(" + httpLinkForPhoto + "," + httpLinkForPhoto + ")"; excelDoc2.SetFormula(1, 1, currentRow, currentCol, currentRow, currentCol, formula); } } else { excelDoc2.setCell(1, 1, currentRow, currentCol, r[currentCol - 1].ToString()); } currentCol++; } currentRow++; } 

.net数据表只能包含.nettypes 。 所以不可能在dataTable中添加一个“超链接” 。 但是,当然你可以添加一个超链接到Excel单元格 。 看到这个问题MSDN有关添加超链接到Excel的详细信息。 根据你的代码,这可能看起来像

 // common syntax to add a Hyperlink to Excel object Add( [In] object Anchor, [In] string Address, [In, Optional] object SubAddress, [In, Optional] object ScreenTip, [In, Optional] object TextToDisplay ); // your code for (int rowIndex=0; rowIndex<result.Rows.Count; rowIndex++) { for (int columnIndex=0; columnIndex<result.Columns.Count; columnIndex++) { string yourValue = result.Rows[rowIndex].Item[columnIndex].ToString(); if (columnIndex!=YOUR_HYPERLINK_COLUMN_INDEX) excelDoc2.setCell(1, 1, rowIndex, columnIndex, yourValue); else { Excel.Range range = (Range) YOUR_SHEET.Cells[rowIndex, columnIndex]; CURRENT_WORKSHEET.Hyperlinks.Add( range, yourValue, Type.Missing,"YOUR_SCREEN_TIP", "YOUR_TEXT_TO_DISPLAY"); } } } 

Excel中有这个function。 语法是

 =HYPERLINK(url;friendlyname) 

只要确保单元格中的文字看起来像这样,url和friendlyname当然是占位符,你应该replace它们来显示你的URL和文本。