C#导出带图片和文字的WORD时,图片消失的问题

cxm0624 2014-07-15 11:08:59
单独导出图片或文字时都没有问题,但图片和文字都加上时,需要先显示图片,再显示文字,所以就只能显示出文字,图片就不见,这个是怎么回事?应该怎样解决?
附代码
protected string CreatWord(string id)
{
Sendinformation sendinfo = SendinformationLogic.Get(id);
string name = sendinfo.Caption.ToString();
object path;
string strContent;
string picName = "http://localhost/SendInfo/Files/20140710/党政内网修改3.05.files/image002.jpg";
MSWord.Application wordApp;
MSWord.Document wordDoc;

string dates = System.DateTime.Now.ToString("yyyyMMdd");
string phyPath = Server.MapPath("~/") + "Files\\报送信息导出\\" + dates + "\\";

if (!Directory.Exists(phyPath))
Directory.CreateDirectory(phyPath); //创建文件所在目录

path = phyPath + @"\" + name + ".doc";
wordApp = new MSWord.ApplicationClass();
if (File.Exists((string)path))
File.Delete((string)path);

Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//插入文档图片
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 553f;
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 211f;
MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;
wordApp.Selection.TypeParagraph();//插入段落
//插入文档内容
strContent = Server.HtmlDecode(sendinfo.Content);
wordDoc.Paragraphs.Last.Range.Text = strContent;
object format = MSWord.WdSaveFormat.wdFormatDocument;
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);

MSWord._Document doc = wordDoc as MSWord._Document;
doc.Close(ref Nothing, ref Nothing, ref Nothing);

MSWord._Application app = wordApp as MSWord._Application;
app.Quit(ref Nothing, ref Nothing, ref Nothing);
return path.ToString();
}
...全文
221 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

62,046

社区成员

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

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

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

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