现在的想法是:数据后台提交时,一边把所有的数据提交到库中,一边通过摸板生成一个html文件,并且将这个HTML文件的文件名也提交到库中的FileURL字段中,以后直接读取这个文件。另外,在数据提交时怎么生成WORD的.DOC文件?请教高手?

解决方案 »

  1.   

    string[] HtmlFormat = new string[7];//定义和HTML页数目一致的数组
    System.Text.StringBuilder htmltext = new StringBuilder();
       try
        {

        using(System.IO.StreamReader sr = new StreamReader("..\\Templates\\ArticleTemp.html",System.Text.Encoding.ASCII))
           {
    string line;
    while((line=sr.ReadLine()) != null)
    {
      htmltext.Append(line);
             }
    sr.Close();
    }
    }
    catch
    {
        Response.Write("<Script>alert('读取摸板文件错误!')</Script>");
    }// -------------------给标记数组赋值-------------------------
    HtmlFormat[0]="'"+this.TextBox1.Text.ToString().Trim()+"'";
    HtmlFormat[1]="dfgsdg";
    HtmlFormat[2]="dsfg";
    HtmlFormat[3]="asdas";
    HtmlFormat[4]="'"+this.TextBox2.Text.ToString().Trim()+"'";
    HtmlFormat[5]="asd";
    HtmlFormat[6]="asdasd";// ---------------------替换HTML标记为要加入的内容-----------------
    for(int i=0;i<7;i++)

    {
      htmltext.Replace("$HtmlFormat["+i+"]",HtmlFormat[i]);
    }

    //-------------------生成HTML文件------------------try
    {
      using(StreamWriter sw = new StreamWriter("..\\TaxNews\\NewsFile\\001.html",false,System.Text.Encoding.GetEncoding("GB2312")))
      {
           sw.WriteLine(htmltext);
           sw.Flush();
           sw.Close();
           }
        }
    catch
      {
         Response.Write("<Script>alert('文件生成失败!')</Script>");
      }
     }
    }用上面的代码,老是提示异常错误(读取摸板文件错误!/文件生成失败!)。请问是什么原因,谢谢
      

  2.   

    c#的io 不支持 ..\\ 这样的相对路径,路径有问题
      

  3.   

    以下代码测试通过:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;namespace study
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } protected void Button1_Click(object sender,System.EventArgs e)
    {
    string[] Formate=new String[3];
    StringBuilder sb=new StringBuilder();
    try
    {
    StreamReader sr=new StreamReader(Server.MapPath("Format.html"));
    string line;
    while((line=sr.ReadLine())!=null)
    {
    sb.Append(line);
    }
    sr.Close(); }
    catch
    {
    Response.Write("Read Template Eorr!");
    }
    Formate[0]="a";
    Formate[1]="b";
    Formate[2]="c"; for(int i=0;i<=2;i++)
    {
    sb.Replace("$html["+i+"]",Formate[i]);
    } try
    {
    StreamWriter sw=new StreamWriter(Server.MapPath("1.Html"),false,System.Text.Encoding.GetEncoding("GB2312"));
    sw.WriteLine(sb);
    sw.Flush();
    sw.Close();
    }
    catch
    {
    Response.Write("The file could't write");
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  4.   

    楼上的代码可以实现,可是,原来摸班中的中文在生成后的HTML文件里都成了乱码或问号?是怎么回事啊?
      

  5.   

    web.config中的配置:    <!--  全球化          此节设置应用程序的全球化设置。    -->
        <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312"/>