博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
项目中一个用于导出word的方法
阅读量:4048 次
发布时间:2019-05-25

本文共 3186 字,大约阅读时间需要 10 分钟。

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

 

转载地址:http://qhdci.baihongyu.com/

你可能感兴趣的文章
2010的第一次思想触动
查看>>
文学大师做菜艺术20个"须知"
查看>>
SVN + 批处理 + Dropbox + TeamViewer实现远方协同工作
查看>>
vc学习之关于缩放到托盘区
查看>>
vc学习之键盘事件OnKeyDown
查看>>
近期工作
查看>>
春寒料峭,原来春天这样走近
查看>>
电脑坏了--关于联想笔记本声卡驱动
查看>>
C#中枚举类型在switch语句中值对照问题
查看>>
GridView中根据特殊标记设置不可编辑
查看>>
PowerDesinger中生成数据库时将域的内容转化为实际的字段
查看>>
通过模板将GridView导出为Excel
查看>>
迭代开发过程及一些原则
查看>>
Windows IIS服务器CA认证安装
查看>>
Asp.net 中Excel通过模板导出中发布问题
查看>>
64位机器配置CA认证一些小问题
查看>>
SVN自动完全备份
查看>>
开发中的Warning原来也很有用
查看>>
Silverlight Toolkit例子代码中缺少System.Windows.Controls.Samples.Common.dll的解决办法
查看>>
项目经理培训后的一点感概
查看>>