具体描述:
数据源:DataTable。
要求:
把DataTable的内容导出到EXCEL,
服务器不安装OFFCICE EXCEL以及其他任何相关组件,
生成列中会有某列是超链接到某地址,列和超链接地址是确定的。求解决方案。

解决方案 »

  1.   

    服务器上有一个目录,有写入权限.
    里面有一个模板excel文件,里面只有各字段。方案:
    导出前copy该模板成另一个文件,通过oledb方式连接excel将datatable的数据用sql insert到excel中。
      

  2.   

    http://dotnet.aspx.cc/article/700bd3fa-a17f-41dc-b258-0dc572625700/read.aspx
      

  3.   

    http://dotnet.aspx.cc/article/bf0a54f9-c7c7-4200-bd9a-802ac1f5de50/read.aspx
      

  4.   

    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();
            }
        }
    }