根据数据表中的html代码生成word文件
object path; //文件路径变量
Application wordApp; //Word应用程序变量
Document wordDoc; //Word文档变量
string strWordFile = HttpContext.Current.Server.MapPath(WordFilePath);
if (!Directory.Exists(strWordFile))
Directory.CreateDirectory(strWordFile);
Thread.Sleep(1000);//延迟1秒
path = strWordFile + strTitle + ".doc"; //路径
wordApp = new ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//WdSaveFormat为Word文档的保存格式
int intIndex = strContent.IndexOf("<div");//根据<div>判断是否为txt文本
string strImagePath = ""; //图片地址
string picUrl = "";//图片绝对路径
foreach (Match match in Regex.Matches(strContent, @"(?<=src=['""])[^'""]*(?=['""])", RegexOptions.IgnoreCase))
{
strImagePath = HttpContext.Current.Server.MapPath(UploadFilePath);
strImagePath = strImagePath + match.Value.Replace("/","\\"); //strAPath.Substring(strPath.Length+1, strAPath.Length - strPath.Length-1);
strContent = strContent.Replace(match.Value, strImagePath);
}
picUrl = strImagePath;
object format = intIndex > 0 ? WdSaveFormat.wdFormatDOSTextLineBreaks : WdSaveFormat.wdFormatDocument;
//文件内容
wordDoc.Paragraphs.Last.Range.Text = intIndex > 0 ? "<body>" + strContent + "</body>" : strContent;
//将wordDoc文档对象的内容保存为DOC文档
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
如果有图片的话。下载到本地就看不到图片,,
如何将图片生成到word中呢。。
...全文
相关推荐
java将html文件生成word文件
使用docx4j把html转化成Word文档,可以生成doc或docx,需要的jar都在里面了
QT基于HTML生成WORD文件
QT基于HTML生成WORD文件,支持标题,表格,缩进,更多格式根据需求调整
C# HtmlToWord 将HTML文件转换成word文档
批量的把html 文件转换成word,不需要电脑上有word软件!并且支持本地图片,html链接的图片必须在本地,可以把图片转换到word里!
oracle数据表导出为word文档
做软件开发时,可以使用该工具将设计的数据库表格导出为word的表格形式。
在线编辑word,word转html,根据html标签生成word,word修改前后的内容对比
该功能支持word在线编辑,word转换为html,html转换为word,word在线编辑前后的内容对比,并支持将对比后的内容生成word,删除的内容标红,新增的内容标绿


