我现在做毕业设计,生成试卷,我所有的单道试题都是单独的word文档,我现在想把单独的试题集中到一个文档中,意思是根据算法,找到选用的试题,当然,这是很多道试题,用于出卷,然后根据路径找到每一道试题(单独的word文档),将内容写入到已经设置好格式的word(页边距、分栏这些)中,生成一份试卷,每一道试卷基本都包含图片和公式,所以肯定不能用追加String的方法,那样公式会丢失或者乱码,不知道你们理解了么?

解决方案 »

  1.   

    直接用流导入到word文档里去!
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    using Microsoft.Office;
    using Microsoft.Office.Interop.Word; public class Word
       {
           object strFileName;
           Object Nothing;
           Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
           Document myWordDoc;
           string strContent = "";
           /// <summary>
           /// 将字符串生产word 文件
           /// </summary>
           /// <param name="str">生产word文件的字符串</param>
           /// <param name="name">生产word文件的名称</param>
           /// <param name="path">生产word文件路径</param>
           /// <returns>生产结构是否成功</returns>
           public string createWord(string str, string name, string path)
           {
               //strFileName = System.Windows.Forms.Application.StartupPath + "\\" + name;
               strFileName = path + "\\" + name;           if (System.IO.File.Exists((string)strFileName))
                   System.IO.File.Delete((string)strFileName);
               Object Nothing = System.Reflection.Missing.Value;
               myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);           #region 将数据库中读取得数据写入到word文件中           strContent = str;
               myWordDoc.Paragraphs.Last.Range.Text = strContent;           #endregion           //将WordDoc文档对象的内容保存为DOC文档 
               myWordDoc.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);
               //关闭WordDoc文档对象 
               myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
               //关闭WordApp组件对象 
               myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);           str = strFileName + "\r\n" + "创建成功";
               return str;       }
           /// <summary>
           /// 读取word文件 生产字符串
           /// </summary>
           /// <param name="path">获取word文件路径</param>
           /// <returns>返回读取内容</returns>
           public string openWord(string path)
           {
               string returnstr = "";
               object filepath = path;
               //filepath = "D:\\Debugtest.doc"; 
               object oMissing = System.Reflection.Missing.Value;
               myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
               //myWordDoc.Content.Font.Size = font.Size;
               //myWordDoc.Content.Font.Name = font.Name;
               myWordDoc.Save();
               returnstr = myWordDoc.Content.Text;
               myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
               myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
               return returnstr;
           }   }
    }
      

  3.   

    2楼,不是你这样的,我word里好多都是大公式和图片,根本不能用text。这样公式会丢失或乱码
      

  4.   

    4楼的,找到个这样的,能实现合并,但是合并后的word内容里有没用的东西,我也不知道怎么出来的。你试试吧。http://kb.cnblogs.com/page/49309/