【Vegas原创】稍复杂点的导出word方法(利用word书签进行定位)

别忘了引用word.exe

using System.Reflection;
     DBAccess db = new DBAccess();
        DataSet ds 
= new DataSet();
        
string sql = "";
        
object oMissing = System.Reflection.Missing.Value;
        
object oEndOfDoc = "endofdoc";
        
object oEndOfDoc2 = "endofdoc2";
            
//Start Word and create a new document.
        Word._Application oWord;
        Word._Document oDoc;


btnclick():

    //导出word      
      protected void Button1_Click(object sender, System.EventArgs e)
        {
            EndProcess();
            oWord 
= new Word.Application();
            oWord.Visible 
= true;
            
string whichDoc = "1";
            
try
            {

                Part1(whichDoc);

                part2();

                
//
            }
            
catch
            {
                Response.Write(
"对不起,录入资料有误,请联系管理员,谢谢!");
                Response.End();
            }
            
object fn = (object)Server.MapPath("temp.doc");
            oDoc.SaveAs(
ref fn, 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 );
       
            oDoc.Close(
ref oMissing,ref oMissing,ref oMissing);
            oWord 
= null;
            outFile(
"temp.doc", Server.MapPath("temp.doc"));
        }

结束word进程:

   void EndProcess()
        {
            
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
            {
                
if (thisproc.ProcessName.Equals("WINWORD"))
                {
                    thisproc.Kill();
                }
            }
        }

第一部分填充:
"d:\\dot\\" + whichDoc + ".dot"; 
        路径指你事先编辑好的word模板。

object oBookMark_l1 = "l1";  
        l1代表你在制作word模板的时候所添加的书签名称。

oDoc.Bookmarks.get_Item(ref oBookMark_l1).Range.Text = "\u00fe";  
        "\u00fe"是对勾的写法。 在word模板中,将此标签的字体设置为Wingdings,当输入"\u00fe"时,方框就会变为加对勾的方框^_^
        

    void Part1(string whichDoc)
        {
            
object oTemplate = "d:\\dot\\" + whichDoc + ".dot";
            oDoc 
= oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                
//****
                 string  sql = "select " ;
                 DataSet    ds
=db.GetDataSet(sql);
               
                    
if (ds.Tables[0].Rows.Count == 0)
                    {
                        Response.Write(
"<sricpt>alert('对不起,无资料!');</script>");
                        Response.End();
                    }
                    
else
                    {
                        
object oBookMark_dept = "dept";
                        
object oBookMark_name = "name";
                        
object oBookMark_emp = "emp";
                        
object oBookMark_t1 = "t1";
                        
object oBookMark_l1 = "l1";

                        oDoc.Bookmarks.get_Item(
ref oBookMark_dept).Range.Text = TextBox3.Text;
                        oDoc.Bookmarks.get_Item(
ref oBookMark_emp).Range.Text = TextBox1.Text;
                        oDoc.Bookmarks.get_Item(
ref oBookMark_name).Range.Text = TextBox2.Text;
                        oDoc.Bookmarks.get_Item(
ref oBookMark_t1).Range.Text = ds.Tables[0].Rows[0][3].ToString();
                        
                        
if (ds.Tables[0].Rows[0][7].ToString() == "1")
                        {
                            oDoc.Bookmarks.get_Item(
ref oBookMark_l1).Range.Text = "\u00fe";
                        }
                      
                    }
                    ds.Dispose();
        }


part2() 类同,不再贴出。

outfile()

       void outFile(string fileName,string filePath)
        {
            
string MIME = "application/vnd.ms-word";
            Response.Clear();
            Response.AppendHeader(
"content-disposition""attachment; filename=" + fileName );
            
//设置输出文件类型
            Response.ContentType = MIME;
            
//把文件流发送到客户端
            Response.WriteFile(filePath);
            Response.Flush();
            Response.Close();
            Response.End();
        }

 

如报错,查看:

http://www.cnblogs.com/vegaslee/archive/2008/01/04/1025555.html 

 

posted @ 2008-04-07 14:15  李济宏(Amadeus)  阅读(255)  评论(0编辑  收藏  举报