具体描述:
数据源:DataTable。
要求:
把DataTable的内容导出到EXCEL,
服务器不安装OFFCICE EXCEL以及其他任何相关组件,
生成列中会有某列是超链接到某地址,列和超链接地址是确定的。求解决方案。
数据源:DataTable。
要求:
把DataTable的内容导出到EXCEL,
服务器不安装OFFCICE EXCEL以及其他任何相关组件,
生成列中会有某列是超链接到某地址,列和超链接地址是确定的。求解决方案。
解决方案 »
- Treeview与页面局部刷新
- MVC 页面呈现求解
- 如何将字符串变为datetime
- 怎么分离一个ASP.net的网站,,,并把改栏目做成一个独立的个体?
- 送分了.简单算法 1+2-3+4-5+6.......100
- DeleteCommand为什么执行了两次??
- 一个关于dropdownlist的简单问题,却让我郁闷了几天,急,在线等
- 怎么在后台获取当前页面焦点的位置从而得到控件的ID?(100分!)
- 高手,问个问题:如何从数据库中取出二进制的图片放到创建好的文件夹中
- 1
- asp.net引用excel失败,在dcomcnfg添加了aspnet用户还无法访问,急!
- “System.UnauthorizedAccessException: 拒绝访问”如何解决?
里面有一个模板excel文件,里面只有各字段。方案:
导出前copy该模板成另一个文件,通过oledb方式连接excel将datatable的数据用sql insert到excel中。
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();
}
}
}