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