C#插入图片后再插入文字后图片会被覆盖
[b][b]最近做实验室的项目遇到个难题,在WORD中插入图片,如果插入图片在WORD中的最后一行,不会有问题,但是如果插入到别的位置,后面再插入的话就会被覆盖掉,不知道怎么回事,下面贴上自己的代码,高分急求,有会的请留言或者加我的QQ:851959715,请主动和我说话。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
namespace Automatic_CreateWord
{
class Program
{
static void Main(string[] args)
{
object path; //文件路径变量
string strContent; //文本内容变量
MSWord.Application wordApp; //Word应用程序变量
MSWord.Document wordDoc; //Word文档变量
path = @"C:\Automatic_CreatedWord.doc"; //路径
wordApp = new MSWord.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);
strContent = "1 范围\n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent= " 1.1 标识\n 1.2 系统概述\n 1.3 文档的概述\n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Font.Bold = 0;
wordDoc.Paragraphs.Last.Range.Font.Size = 13;
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "2 引用文档\n";
wordDoc.Paragraphs.Last.Range.Font.Name = "楷体";
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorGreen;
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = " 引用的文档\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "3 术语应用\n";
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorRed;
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = " 术语的应用\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "2 程序系统组织结构\n 4.1 程序运行过程\n 程序的运行过程 \n 4.2 程序逻辑\n 插图片有问题\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent="5 组织导航软件详细设计(中断和主函数内部每一个函数都需要\"函数功能描述\")\n";
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlack;
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
wordDoc.Paragraphs.Last.Range.Text = strContent;
string filename = @"c:\TEST.jpg";
Object range = wordDoc.Paragraphs.Last.Range;
Object linkToFile = false; //默认
Object saveWithDocument = true; //默认
//使用InlineShapes.AddPicture方法插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object count = 30;
wordApp.Selection.MoveEnd(ref unit, ref count);
strContent = " 5.1 中断描述\n 中断的描述 \n 5.2 主程序 \n 主的程序\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "6 需求可追踪性\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
int lenght = wordDoc.Characters.Count-1;
object start = lenght;
object end = lenght;
//表格起始坐标
MSWord.Range tableLocation = wordDoc.Range(ref start, ref end);
//添加Word表格
MSWord.Table table = wordDoc.Tables.Add(tableLocation, 5, 5, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;
//使用两层循环填充表格的内容
table.Cell(1, 1).Range.Text = "序号";
table.Cell(1, 2).Range.Text = "概要设计章节";
table.Cell(1, 3).Range.Text = "概要设计项";
table.Cell(1, 4).Range.Text = "详细设计章节";
table.Cell(1, 5).Range.Text = "详细设计项";
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);
//关闭wordDoc文档对象
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 创建完毕!");
}
}
}
如上面所示,后面插入文字后,图片就会被覆盖。求高人解答[/b][/b] --------------------编程问答-------------------- 未看代码 且不懂
设置word版式可否呢? --------------------编程问答-------------------- 可以用标签,在指定的mark处添加你的图片。 --------------------编程问答-------------------- 未看代码 且不懂
设置word版式可否呢? --------------------编程问答-------------------- 怎么不在页面上写 , --------------------编程问答-------------------- liuwei2500,前辈能不能给出详细些的代码呢,如果能够实现,分就给你了,我的QQ:851959715 --------------------编程问答-------------------- 比较看不懂 --------------------编程问答-------------------- 不是插不了图片,而是插了图片之后,下面再插入文字就会被覆盖。 --------------------编程问答-------------------- 求高手解答,是100分哦 --------------------编程问答--------------------
--------------------编程问答-------------------- 其实这个比较简单,跟Office相关的,你可以先按需求录制宏,然后根据VBA代码,转.net就可以了,以前就是这么搞的 --------------------编程问答-------------------- 最后怎么解决的……我选择遇到同样的问题……
wordApp.Selection.MoveEnd(ref unit, ref count);
换成
object WdLine = Word.WdUnits.wdLine;//换一行;
wordApp.Selection.MoveDown(ref WdLine, ref count, ref omissing);//移动焦点
wordApp.Selection.TypeParagraph();//插入段落
补充:.NET技术 , C#