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

 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);

        }


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值