asp.net实现导出DataTable数据到Word或者Excel的方法

/// <summary>< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> /// 导出DataTable数据到Word或者Excel /// </summary> /// <param>Page指令</param> /// <param>DataTable数据表</param> /// <param>导出Word或者Excel表格的名字</param> /// <param>导出Word或者Excel表格中内容的标题</param> /// <param>导出Word或者Excel的人</param> /// <param>导出类型(w:Word,e:Excel)</param> public bool DataTableToExcel(Page pPage, DataTable dt, string str_ExportTitle, string str_ExportContentTitle, string str_ExportMan, string str_ExportType) { bool bl_Result = false; string str_ExportTypeName = "word";//导出类型 string str_ExportFormat = ".doc";//导出类型的格式 if (str_ExportType.Equals("e")) { str_ExportTypeName = "excel"; str_ExportFormat = ".xls"; } HttpResponse response = pPage.Response; if (dt.Rows.Count > 0) { response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); response.ContentType = "application/ms-" + str_ExportTypeName; response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str_ExportTitle, System.Text.Encoding.UTF8).ToString() //该段需加,否则会出现中文乱码 + str_ExportFormat); //获取DataTable的总列数 int i_ColumnCount = dt.Columns.Count; //定义变量存储DataTable内容 System.Text.StringBuilder builder = new System.Text.StringBuilder(); builder.Append("<html><head>\n"); builder.Append("<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n"); builder.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n"); builder.Append("</head>\n"); builder.Append("<table>"); if (!string.IsNullOrEmpty(str_ExportContentTitle)) { builder.Append(string.Concat(new object[] { "<tr><td colspan=", (i_ColumnCount + 1), ">", str_ExportContentTitle, "</td></tr>" })); } builder.Append("<tr><td colspan=" + (i_ColumnCount + 1) + " valign='middle'>"); builder.Append("导出人:【" + str_ExportMan + "】,导出时间:【" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "】</td></tr>"); builder.Append("<tr>\n"); builder.Append("<td>序号</td>\n"); for (int i = 0; i < i_ColumnCount; i++) { if (dt.Columns[i].Caption.ToString().ToLower() != "id") { builder.Append("<td><b>" + dt.Columns[i].Caption.ToString() + "</b></td>\n"); } } #region 此处没有在导出的数据列的最前面加一列(序号列) //此处没有在导出的数据列的最前面加一列(序号列) //foreach (DataRow row in dt.Rows) //{ // builder.Append("<tr>"); // for (int j = 0; j < i_ColumnCount; j++) // { // if (dt.Columns[j].Caption.ToString().ToLower() != "id") // { // builder.Append("<td>" + row[j].ToString() + "</td>"); // } // } // builder.Append("</tr>\n"); //} #endregion #region 在导出的数据列的最前面加了一序号列(注意:非DataTable数据的序号) //在导出的数据列的最前面加了一序号列(注意:非DataTable数据的序号) for (int m = 0; m < dt.Rows.Count; m++) { builder.Append("<tr>"); for (int j = 0; j < i_ColumnCount; j++) { if (dt.Columns[j].Caption.ToString().ToLower() != "id") { if (j == 0) { builder.Append("<td>" + (m + 1) + "</td>"); } if (j > 0) { builder.Append("<td>" + dt.Rows[m][j - 1].ToString() + "</td>"); } if (j == dt.Columns.Count - 1) { builder.Append("<td>" + dt.Rows[m][j].ToString() + "</td>"); } } } builder.Append("</tr>\n"); } #endregion builder.Append("<tr><td colspan=" + (i_ColumnCount + 1) + " valign='middle'>"); builder.Append("合计:共【<font color='red'><b>" + dt.Rows.Count + "</b></font>】条记录</td></tr>"); builder.Append("<tr>\n"); builder.Append("</table>"); response.Write(builder.ToString()); response.End(); bl_Result = true; } return bl_Result; }

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyjxj.html