using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using Microsoft.Office.Interop.Word;
 



namespace ExamSecure 

    
///  
    
/// ItemToDoc 的摘要说明。 
    
///  

    public class ItemToDoc : System.Windows.Forms.Form 
    

        
object strFileName; 
        Object Nothing; 
        ApplicationClass myWordApp
=new ApplicationClass(); 
        Document myWordDoc; 
        
string strContent=""



        
private System.ComponentModel.Container components = null



        
public ItemToDoc() 
        

            
// 
            
// Windows 窗体设计器支持所必需的 
            
// 
            InitializeComponent(); 



            
// 
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
            
// 
        }
 
        [STAThread] 
        
static void Main()  
        

            System.Windows.Forms.Application.Run(
new ItemToDoc()); 
        }
 
        
///  
        
/// 清理所有正在使用的资源。 
        
///  

        protected override void Dispose( bool disposing ) 
        

            
if( disposing ) 
            

                
if(components != null
                

                    components.Dispose(); 
                }
 
            }
 
            
base.Dispose( disposing ); 
        }
 



        
Windows Form Designer generated code 

        
private void ItemToDoc_Load(object sender, System.EventArgs e) 
        

            WriteFile(); 
        }
 
        
private void WriteFile() 
        

   
            strFileName
=System.Windows.Forms.Application.StartupPath+"\\试题库【"+GetRandomString()+"】.doc"
            Object Nothing
=System.Reflection.Missing.Value; 
            myWordDoc
=myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing); 
    
            
将数据库中读取得数据写入到word文件中  
    
            
//将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); 
        }
 



        
///  
        
/// 获取一个随即字符串 
        
///  
        
///  

        private string GetRandomString() 
        

            DateTime iNow
=DateTime.Now; 
            
string strDate=iNow.ToString("yyyyMMddHHmmffff"); 
    
            Random ran
=new Random(); 
            
int iRan=Convert.ToInt32(10000*ran.NextDouble()); 
            
string strRan=iRan.ToString(); 
            
//位数不足则补0    
            int iRanlen=strRan.Length; 
            
for(int i=0;i<4-iRanlen;i++
            

                strRan
="0"+strRan; 
            }
 
            
return strDate+strRan; 
        }
 

        
///  
        
/// 将字符串写入到Word文件中 
        
///  
        
/// 要写入的字符串 

        private void WriteFile(string str) 
        

            myWordDoc.Paragraphs.Add.Range.Text
=str; 
        }
 
    }
 
}