.net导出excel 加超级链接 如题,在导出excel的时候,如何给某一列添加一个超链接?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以先编辑个带超链接的excel模版,然后copy这个模版,把datatable的值遍历写入这个excel 参考using System;using System.Text;using System.IO;namespace Test{ public partial class XmlExcel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //存放模板文件内容 string fileContent = string.Empty; //模板文件位置 string modleFileName = Server.MapPath(".") + "\\ExcelModleFile.xls"; //生成文件位置 string renderFileName = Server.MapPath(".") + "\\ExcelFile.xls"; //读出并保存模板文件内容 StreamReader sr = new StreamReader(modleFileName, System.Text.Encoding.GetEncoding("gb2312")); fileContent = sr.ReadToEnd(); sr.Close(); //循环生成数据行 StringBuilder sbRowsText = new StringBuilder(1024); sbRowsText.Append("<Row ss:AutoFitHeight=\"0\">"); sbRowsText.Append("<Cell ss:StyleID=\"s24\" ss:HRef=\""); //设置超链接地址 sbRowsText.Append("http://www.126.com/"); sbRowsText.Append("\"><Data ss:Type=\"String\">View</Data></Cell>"); sbRowsText.Append("<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">"); //设置内容 sbRowsText.Append("Content"); sbRowsText.Append("</Data></Cell>"); sbRowsText.Append("</Row>"); //保存完整Excel内容的字符串 StringBuilder sbRender = new StringBuilder(); //获得模板内容 sbRender.Append(fileContent); //设置Excel数据行 sbRender.Replace(@"[RowCount]", "3"); //设置Excel标题 sbRender.Replace(@"[Header]", "Title"); //添加数据行 sbRender.Replace(@"[DataRows]", sbRowsText.ToString()); lblXml.Text = sbRender.ToString(); //将内容写入文件 StreamWriter sw = new StreamWriter(renderFileName); sw.Write(sbRender.ToString()); sw.Close(); //将文件输出到客户端 Response.Charset = "GB2312"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(renderFileName)); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(renderFileName); Response.End(); } }} 我使用的是 AppLibrary.WriteExcel组件做的导出excel,可以试试你说的这种方法。 请大家看下我的代码哪来出错了? 如何实现DATAGRIDVIEW的多维表头的问题。.net控件达人进来看一看吧 范型的问题 freetextbox 中文版带源码的版本用那个版本最好? 如果按钮只是一个背景图,如何设置快捷键,谢谢, winform 打包的时候资源文件也一起打包了么?为什么找不到 数据库中历史数据怎么处理好?比如去年数据保留,所有系统中的统计、查询等全部以新年为准? 请问如何获取 winform中 Properties 窗口的属性值? 如何去掉字符后面的空格? 急求指点. C#页面抓取信息 改变dataGridView中comboBox的值
using System.Text;
using System.IO;namespace Test
{
public partial class XmlExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//存放模板文件内容
string fileContent = string.Empty;
//模板文件位置
string modleFileName = Server.MapPath(".") + "\\ExcelModleFile.xls";
//生成文件位置
string renderFileName = Server.MapPath(".") + "\\ExcelFile.xls"; //读出并保存模板文件内容
StreamReader sr = new StreamReader(modleFileName, System.Text.Encoding.GetEncoding("gb2312"));
fileContent = sr.ReadToEnd();
sr.Close(); //循环生成数据行
StringBuilder sbRowsText = new StringBuilder(1024);
sbRowsText.Append("<Row ss:AutoFitHeight=\"0\">");
sbRowsText.Append("<Cell ss:StyleID=\"s24\" ss:HRef=\"");
//设置超链接地址
sbRowsText.Append("http://www.126.com/");
sbRowsText.Append("\"><Data ss:Type=\"String\">View</Data></Cell>");
sbRowsText.Append("<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">");
//设置内容
sbRowsText.Append("Content");
sbRowsText.Append("</Data></Cell>");
sbRowsText.Append("</Row>"); //保存完整Excel内容的字符串
StringBuilder sbRender = new StringBuilder();
//获得模板内容
sbRender.Append(fileContent);
//设置Excel数据行
sbRender.Replace(@"[RowCount]", "3");
//设置Excel标题
sbRender.Replace(@"[Header]", "Title");
//添加数据行
sbRender.Replace(@"[DataRows]", sbRowsText.ToString()); lblXml.Text = sbRender.ToString(); //将内容写入文件
StreamWriter sw = new StreamWriter(renderFileName);
sw.Write(sbRender.ToString());
sw.Close(); //将文件输出到客户端
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(renderFileName));
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(renderFileName);
Response.End();
}
}
}