ado.net存取word及excel

來源:互聯網
上載者:User

ado.net存取word及excel

 blob表

  3 id int 4 0

  0 name char 50 1

  0 blob image 16 1

  0 type char 60 1

  saveFile.aspx.cs

  PRivate void Button1_Click(object sender, System.EventArgs e)

  {

  Stream imgdatastream = File1.PostedFile.InputStream;

  int imgdatalen = File1.PostedFile.ContentLength;

  string imgtype = File1.PostedFile.ContentType;

  string name = this.getFileNameByURL(this.File1.PostedFile.FileName);

  byte[] imgdata = new byte[imgdatalen];

  int n = imgdatastream.Read(imgdata,0,imgdatalen);

  string connstr = "workstation id=OVERMIND;packet size=4096;user id=sa;passWord=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";

  SqlConnection connection = new SqlConnection(connstr);

  SqlCommand command = new SqlCommand("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )", connection );

  SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );

  paramTitle.Value = name;

  command.Parameters.Add(paramTitle);

  SqlParameter paramData = new SqlParameter( "@blob", SqlDbType.Image );

  paramData.Value = imgdata;

  command.Parameters.Add( paramData );

  SqlParameter paramType = new SqlParameter( "@type", SqlDbType.VarChar,50 );

  paramType.Value = imgtype;

  command.Parameters.Add( paramType );

  wztj.debug.TestSQL.TraceErrorSql("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )",command.Parameters);

  connection.Open();

  int numRowsAffected = command.ExecuteNonQuery();

  connection.Close();

  }

  listFile.aspx//這個東西主要用來列表,把已經有的東西列出來

  <asp:HyperLinkColumn DataNavigateUrlField="id" HeaderText="產品名稱" DataNavigateUrlFormatString="./getFile.aspx?ID={0}" DataTextField="name" DataTextFormatString="{0}" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="160px">

  listFile.aspx.cs

  string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";

  SqlConnection connection = new SqlConnection(connstr);

  SqlCommand command = new SqlCommand("select * from blob", connection );

  connection.Open();

  SqlDataAdapter adaptor = new SqlDataAdapter(command);

  DataSet ds = new DataSet();

  adaptor.Fill(ds,"blob");

  connection.Close();

  this.DataGrid1.DataSource=ds.Tables["blob"].DefaultView;

  this.DataGrid1.DataBind();

  getFile.aspx.cs//這個檔案比較重要負責把村道資料庫裡面的檔案,按照格式,按照名稱,給傳輸出來

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

  {

  string imgid =this.Request.QueryString.Get("ID");

  //Request.QueryString["imgid"];

  string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";

  string sql="SELECT name,blob, type FROM blob WHERE id = " + imgid;

  SqlConnection connection = new SqlConnection(connstr);

  SqlCommand command = new SqlCommand(sql, connection);

  connection.Open();

  SqlDataReader dr = command.ExecuteReader();

  if(dr.Read())

  {

  Response.Clear();

  Response.Buffer= true;

  Response.Charset="GB2312";

  Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//設定輸出資料流為簡體中文

  //Response.ContentType = "application/ms-word";//設定輸出檔案類型為word檔案。

  Response.ContentType = dr["type"].ToString();

  Response.BinaryWrite( (byte[]) dr["blob"] );

  string FileName = dr["name"].ToString()。Trim();

  FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );

  Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);

  }

  connection.Close();

  }

  這裡要說的有兩點,第一,就是把檔案的名稱getFile.aspx變成我們想要的名稱。

  Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);

  第二,就是把指定的名稱變成我們想要的值,是標準的中文,而不是中文的亂碼。

  FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.