using System.IO;
//中间函数忽略不计
string strContent = somethings; //假使现在somethings是包含数据读取的字符串
string strPath = Request.PhysicalApplicationPath + "Templates//"; //模板存放路径
string strTempNm = "a.txt"; //模板名称
string strFileNm= DateTime.Now.ToString("yyyyMMddhhmmss")+".xls"; //文件名称
StreamReader sr = new StreamReader(strPath+strTempNm,System.Text.Encoding.UTF8); //读取模板
string str = sr.ReadToEnd();
str = str.Replace("[@abc]",strContent); //[@abc]是模板中设置的一个变量
try
{
//获取或设置输出流的字符集
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//将一个http头添加到输出流
Response.AddHeader("Content-Disposition",string.Format("attachment:filename={0}",strFileNm));
//将一个二进制字符串写入http输出流
Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(str));
//向客户端发送当前所有缓冲的输出
Response.Flush();
}
catch(Exception ex)
{
Console.Write(ex.Message);
}
注意:若要采用这种不通过生成临时文件而直接产生报表的方法,其前台页面不能存在类似于<link href="../../css/HQIndex.css" rel="stylesheet" type="text/css">或者<script src="../../VisifireControls/Visifire.js"></script>等内容。