public void MakeThePaper(string paperPath, string paperName, string[] qesNbu, string[] queType, int[] queCon, ArrayList[] path)
{
//定义
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
//Word文档的名称
object strFileName = paperPath + paperName + ".doc";
//如果存在这个文档就先删除
if (System.IO.File.Exists((string)strFileName))
{
System.IO.File.Delete((string)strFileName);
}
object nothing = System.Reflection.Missing.Value;
doc = app.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing); //打开一个word
//一个大题一个大题的写
for (int i = 0; i < qesNbu.Length; i++)
{
if (qesNbu[i] == null)
{
break;
}
//先写题目
string mark = qesNbu[i] + queType[i];
doc.Paragraphs.Last.Range.Text = doc.Paragraphs.Last.Range.Text + mark;
//添加一个空格
string blank = "";
doc.Paragraphs.Last.Range.Text = doc.Paragraphs.Last.Range.Text + blank;
//写这个大题下面的每一个小题
for (int j = 0; j <= queCon[i]; j++)
{
//从第二个小题考试写入图片
if (j != 0)
{
//先写入空格
string blankAno = "";
doc.Paragraphs.Last.Range.Text = doc.Paragraphs.Last.Range.Text + blankAno;
string fileName = path[i][j - 1].ToString(); //要插入的图片的路径
Object oMissed = doc.Paragraphs[doc.Paragraphs.Count - 1].Range; //插入的位置, 替换掉上一个空格
Object oLinkToFile = false; //缺省
Object oSaveWithDocument = true;//缺省
try
{
doc.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
}
catch (Exception)
{
break;
}
}
//然后写入题号,只要queCon[i]不是最后一个就写入
if (j != queCon[i])
{
doc.Paragraphs.Last.Range.Text = doc.Paragraphs.Last.Range.Text + " ";
mark = " " + (j + 1).ToString() + "、" + "()";
doc.Paragraphs.Last.Range.Text = doc.Paragraphs.Last.Range.Text + mark;
}
}
}
app.Visible = true;
//将wordDOC文档对象保存为DOC文档
doc.SaveAs(ref strFileName, 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, ref nothing);
//关闭doc文档
doc.Close(ref nothing, ref nothing, ref nothing);
//关闭WordApp组件对象
app.Quit(ref nothing, ref nothing, ref nothing);
}
项目中一个用于导出word的方法
最新推荐文章于 2025-08-23 13:39:57 发布
本文介绍了一种利用C#编程语言自动生成包含图片的Word试卷的方法。通过Microsoft.Office.Interop.Word命名空间,该方法可以创建新的Word文档,根据传入的题目数量和类型动态生成文档内容,并在指定位置插入相应的图片。
4614

被折叠的 条评论
为什么被折叠?



