在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原地址:忘了
/// <summary> /// DataTable 转换为 Html /// </summary> /// <param name="dt"></param> /// <returns></returns> public string GetHtmlString(DataTable dt) { StringBuilder sb = new StringBuilder(); sb.Append("<html><head>"); sb.Append("<title>Excel转换为Table</title>"); sb.Append("<meta http-equiv='content-type' content='text/html; charset=GB2312'> "); sb.Append("<style type=text/css>"); sb.Append("td{font-size: 9pt;border:solid 1 #000000;}"); sb.Append("table{padding:3 0 3 0;border:solid 1 #000000;margin:0 0 0 0;BORDER-COLLAPSE: collapse;}"); sb.Append("</style>"); sb.Append("</head>"); sb.Append("<body>"); sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'>"); sb.Append("<tr valign='middle'>"); sb.Append("<td><b></b></td>"); foreach (DataColumn column in dt.Columns) { sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>"); } sb.Append("</tr>"); int iColsCount = dt.Columns.Count; int rowsCount = dt.Rows.Count - 1; for (int j = 0; j <= rowsCount; j++) { sb.Append("<tr>"); sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>"); for (int k = 0; k <= iColsCount - 1; k++) { sb.Append("<td>"); object obj = dt.Rows[j][k]; if (obj == DBNull.Value) { obj = " ";//如果是NULL则在HTML里面使用一个空格替换之 } if (obj.ToString() == "") { obj = " "; } string strCellContent = obj.ToString().Trim(); sb.Append("<span>" + strCellContent + "</span>"); sb.Append("</td>"); } sb.Append("</tr>"); } sb.Append("</table>"); //点击单元格 输出 行和列 sb.Append("<script src='https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js'></script>"); sb.Append("<script type='text/javascript'>"); sb.Append("$('table tbody').on('click', 'td', function (e) {"); sb.Append("var row = $(this).parent().prevAll().length-1 ;"); sb.Append("var column = $(this).prevAll().length-1 ;"); sb.Append("var str = 'dt.Rows[' + row + '][' + column + '].ToString()';"); sb.Append("console.log(str);alert(str);"); sb.Append("});"); sb.Append("</script>"); sb.Append("</body></html>"); return sb.ToString(); }
|
请发表评论