项目中一个用于导出word的方法

来源:互联网 发布:制图用什么软件 编辑:程序博客网 时间:2024/06/06 09:02
 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);        }


 

原创粉丝点击