根据数据表中的html代码生成word文件

kevin87923 2012-12-10 05:02:43

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中呢。。
...全文
272 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
winner2050 2012-12-12
  • 打赏
  • 举报
回复
你连接百度的肯定不行。 大部分的网站都作了反盗链。
爱在夕阳下 2012-12-12
  • 打赏
  • 举报
回复
1 新建一个页面 什么都不要写 就插入一张图片 2 将页面保存为单个网页文件, 3 修改它的后缀为doc 4 如果word打开是一个图片 再用txt打开word 你会发现有一段很长的编码 那就是图片的base64 5 你在用我给你的那个方法生成另一个图片的base64 把word中的替换,保存 6 打开word你会发现图片变了
kevin87923 2012-12-11
  • 打赏
  • 举报
回复
ie不行。 火狐可以。 。协议?
webdiyer 2012-12-11
  • 打赏
  • 举报
回复
引用 4 楼 kevin87923 的回复:
是这样写吗。。 可word中显示不出来。。 是个X
你说的图片本身就不存在,浏览器显示404
kevin87923 2012-12-11
  • 打赏
  • 举报
回复
<img width=214 height=717
src="http://t1.baidu.com/it/u=1354549538,1627489155&fm=25&gp=0.jpg" align=left hspace=12 v:shapes="_x0000_s1036"/>
是这样写吗。。 可word中显示不出来。。 是个X
webdiyer 2012-12-11
  • 打赏
  • 举报
回复
你将图片路径转成了针对服务器文件系统的绝对路径,这样将word下载后引用的图片肯定不存在了,你可以将针对文件系统的图片路径改为url绝对路径试试,当然这样的坏处是查看word文档时必须连网才能看到图片。
kevin87923 2012-12-11
  • 打赏
  • 举报
回复
src="data:image/jpg;base64,转换后的字符串" 这样写吗。。 没有用啊。
爱在夕阳下 2012-12-10
  • 打赏
  • 举报
回复
图片保存到word 只能是将图片先转换成base64编码的文本 在保存 这是一个将图片转换成base64编码文本的一个方法。。 private void ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new Bitmap(Imagefilename); this.pictureBox1.Image = bmp; FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); String strbaser64 = Convert.ToBase64String(arr); sw.Write(strbaser64); sw.Close(); fs.Close(); MessageBox.Show("转换成功!"); } catch (Exception ex) { MessageBox.Show("ImgToBase64String 转换失败/nException:" + ex.Message); } }

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧