写模板后读入数据库

来源:互联网 发布:javascript的for循环 编辑:程序博客网 时间:2024/05/05 15:46

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Word;
using System.Data.SqlClient;

public partial class ksyw_addword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        wordtosql("","");
    }
    private void AddWord()
    {

        ////SqlDataReader dr = DbHelperSQL.ExecuteReader("select ");

        //ApplicationClass WordApp = new ApplicationClass();
        //object missing = System.Reflection.Missing.Value;
        //string path = Server.MapPath(".");
        //path = path.Substring(0, path.Length - 10);
        //string dotpath = path + "//moban//来信来访登记表.dot";
        //string docpath = path + "//upload//ks//" + ajid + "//来信来访登记表.doc";
        ////docpath = "E://vs2005//ksyw//ajcl//c.doc";
        //object tempName = @dotpath;
        //object docName = @docpath;
        ////      object isVisible = true;
        ////      object readOnly = false;
        ////      Word.Document WordDoc = WordApp.Documents.Open(ref docName, ref missing, ref readOnly,
        ////ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ////ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,ref missing);

        ////      WordDoc.Activate();

        ////      //WordApp.Selection.TypeText("This is the text");
        ////     // WordApp.Selection.TypeParagraph();
        ////      //WordDoc.Save();

        //WordApp.Application.Quit(ref missing, ref missing, ref missing);
        //Document MyDoc = wordApp.Documents.Add(ref tempName, ref missing, ref missing, ref missing);
        //wordApp.Visible = true;
        //MyDoc.Activate();
        //for (int i = 1; i <= MyDoc.Bookmarks.Count; i++)
        //{
        //    object j = i;
        //    Word.Range wordRng = MyDoc.Bookmarks.get_Item(ref   j).Range;
        //    string name = MyDoc.Bookmarks.get_Item(ref j).Name;
        //    wordRng.Text = MyDoc.Bookmarks.get_Item(ref j).Name;
        //}

        ////wordApp.Selection.Font.Size = 30; // 字体大小
        ////wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; // 居中
        ////wordApp.Selection.Font.Bold = (int)WdConstants.wdToggle; // 黑体
        //// wordApp.Selection.TypeText("hello");
        ////保存
        //MyDoc.SaveAs(ref docName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        //MyDoc.Close(ref missing, ref missing, ref missing);
        //wordApp.Quit(ref missing, ref missing, ref missing);
        //MyDoc = null;
        //wordApp = null;
    }
    private void wordtosql(string filepath,string ajid)
    {
        StringBuilder str = new StringBuilder();
        string path = Server.MapPath(".");
        path = path.Substring(0, path.Length - 5);
        string dotpath = path + "//moban//来信来访登记表.dot";
        ajid="2009_0026";
        string docpath = path + "//upload//ks//" + ajid + "//aj1cs.doc";
           filepath=docpath;
              if (filepath != "")
             {
                    FileStream fs=File.OpenRead(filepath);

                    System.IO.BinaryReader br = null;
                    br = new BinaryReader((Stream)fs);
                    byte[] b = new byte[fs.Length];
                    int intLength = (int)fs.Length;
                    b = br.ReadBytes(intLength);
                    fs.Close();
                     str.Append("insert into ks_file(filename,ksaj_id,doccontent,filetype,doctype) values(@filename,@ksaj_id,@doccontent,@filetype,@doctype)");
                    SqlParameter[] p ={
                    new SqlParameter("@filename", SqlDbType.VarChar,50),
                    new SqlParameter("@ksaj_id", SqlDbType.VarChar,50),
                    new SqlParameter("@doccontent", SqlDbType.Image,intLength),
                    new SqlParameter("@filetype", SqlDbType.VarChar,50),
                    new SqlParameter("@doctype", SqlDbType.VarChar,50)
                    };
                    p[0].Value = "as.doc";
                    p[1].Value = "2009_0028";
                    p[2].Value =b;
                    p[3].Value = "证据材料";
                    p[4].Value = "doc";

                    int n= DbHelperSQL.ExecuteSql(str.ToString(), p);
                    if (n > 0)
                    {
                    Response.Write("dnal");
                    }
                    else
                    {
                    Response.Write("shibai");
                    }
              
       }
    }
}