با سلام ؛
مي خواستم اطلاعاتي كه در پايگاه ذخيره شده را در محيط سي شارپ بر روي يك فايل WORD به ازاي نام آن فيلد نمايش يابد
باتشكر
با سلام ؛
مي خواستم اطلاعاتي كه در پايگاه ذخيره شده را در محيط سي شارپ بر روي يك فايل WORD به ازاي نام آن فيلد نمايش يابد
باتشكر
سلام. بیشتر توضیح دهید.
با سلام بنده هم همچین مشکلی رو دارم
کسی هست کمک کنه
توضیحات:
فرض کنید یه گرید دریم دارای 10 یا هرچند ستون(تعدادش مهم نیست) می خوام این گرید به هموم شکل در word ،Export کنم.
We use the Button1_Click event to do the work. We then call "Response.AddHeader" to export a file which is named FileName.doc. We then use "Response.ContentType" to denotes the type of the file being exported.
The flow for the code behind page is as follows.کد HTML:protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=FileName.doc"); Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/vnd.word"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();} public override void VerifyRenderingInServerForm(Control control) { }
منبع:http://www.aspnettutorials.com/tutor...t2-csharp.aspxکد HTML:using System; using System.Data; using System.Configuration; 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.Text; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { string ConnectionString = "Data Source=(local);Initial Catalog=pubs;User Id=sa;Password=sa123"; SqlConnection cn1; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SqlConnection cn = new SqlConnection(ConnectionString); cn.Open(); cn1 = new SqlConnection(ConnectionString); cn1.Open(); SqlCommand cmd = new SqlCommand("select * from [authors]", cn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = dr; GridView1.DataBind(); dr.Close(); cmd.Dispose(); cn.Dispose(); cn1.Dispose(); cn = cn1 = null;}} protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=FileName.doc"); Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/vnd.word"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();} public override void VerifyRenderingInServerForm(Control control) { }}
درود. آقای ad.davachi شما کد ASP.NET گذاشتی ها!!!
در این مثال فرض بر این است:
- پایگاه داده: SQL Server Express
- نام پایگاه داده: db1
- نام جدول: t1
- فیلدها: id,bin
کد ذخیره هر گونه فایل داخل دیتابیس:
SqlConnection con =بجای FileName مسیر فایلی رو که باید ذخیره بشه رو قرار بده.
new SqlConnection("Initial Catalog=db1;integrated security=true;server=.\\SQLEXPRESS");
SqlCommand cmd =
new SqlCommand("INSERT INTO t1 (bin) VALUES(@binfile)",con);
FileStream fs =
new FileStream(@"FileName", FileMode.Open, FileAccess.Read);
byte[] bf = new byte[fs.Length];
fs.Read(bf, 0, (int)fs.Length);
cmd.Parameters.AddWithValue("binfile", bf);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
fs.Close();
کد بازیابی فایل و نمایش آن:
SqlConnection con = newدر مثال بالا رکوردی که فیلد id آن برابر با یک است رو انتخاب میکنه. و فایل موجود در فیلد bin رو روی هارد ذخیره می کنه.
SqlConnection("Initial Catalog=db1;integrated security=true;server=.\\SQLExpress");
SqlCommand cmd = new
SqlCommand("SELECT bin FROM t1 WHERE id=1", con);
FileStream fs = new
FileStream(@"NewFileName", FileMode.Create, FileAccess.Write);
con.Open();
byte[] bf = (byte[])cmd.ExecuteScalar();
fs.Write(bf, 0, bf.Length);
System.Diagnostics.Process.Start(@"NewFileName");
con.Close();
fs.Close();
بجای NewFileName مسیری رو برای ذخیره فایل قرار بده.