导出的最简单方法(text/html/word)

    /// <summary>
    /// 把日记导出到一个HTML文件里
    /// </summary>
    /// <param name="bookList"></param>
    private void WriteText(OA.Daily.Entities.TList<OA.Daily.Entities.OASelfNoteBook> bookList)
    {
        string name = DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(10000).ToString() + ".html";//生成一个文件名,时间+随机数。
        string drive = Server.MapPath(UploadRoot) + @"日记/";
        if (!System.IO.Directory.Exists(drive))//如果没有这个文件夹自动创建。
        {
            Directory.CreateDirectory(drive);
        }
        string driveName = drive + name;
        FileStream fs = new FileStream(driveName, FileMode.CreateNew, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
        sw.WriteLine("<html><head><title>日记</title></head><body>");
        foreach (OA.Daily.Entities.OASelfNoteBook bookObj in bookList)
        {
            sw.WriteLine("===============================================");
            sw.WriteLine("</br>");
            sw.WriteLine("时间:" + bookObj.WriteDate.Value.ToString("yyyy-mm-dd") + "      标题:" + bookObj.NoteTitle);
            sw.WriteLine("</br>");
            sw.WriteLine(bookObj.Content);
            sw.WriteLine("</br>");
        }
        sw.WriteLine("</body></html>");
        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + name);
        Response.ContentType = "text/html";// 指定返回下载的文件格式。
        Response.WriteFile(driveName); // 把文件流发送到客户端。
        response.Flush();
        System.IO.File.Delete(driveName);//删除在服务器上放置的临时文件。
        Response.End();
    } 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值