111,096
社区成员




string _html = "";
if (Request.Form["hidhtml"] != null && Request.Form["hidhtml"].ToString().Trim() != string.Empty)
{
//取得网页的html代码
_html = Request.Form["hidhtml"].ToString();
_html = new Regex("title(.*?)\"(.*?)\"").Replace(_html, "");
string path = Server.MapPath("\\upload") + GetSaveFilePath(); //创建文件夹算法
//因為Web 是多執行緒環境,避免甲產生的文件被乙下載去,所以檔名都用唯一
string fileNameWithOutExtention = path.Substring(path.LastIndexOf("\\") + 1);
string strPath = path + ".doc";
//生成word
Document _doc = new Document();
DocumentBuilder _builder = new DocumentBuilder(_doc);
_builder.InsertHtml(_html);
_doc.Save(strPath, Aspose.Words.SaveFormat.Doc);
//檔案下載
//把檔案讀進串流
FileStream fs = new FileStream(strPath, FileMode.Open);
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
fs.Close();
//Response給用戶端下載
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/msword";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileNameWithOutExtention + ".doc");//強制下載
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(file);
Response.End();
}