PDA

View Full Version : سوال: چاپ اطلاعات



mersade100
شنبه 18 دی 1389, 15:50 عصر
با سلام ؛
مي خواستم اطلاعاتي كه در پايگاه ذخيره شده را در محيط سي شارپ بر روي يك فايل WORD به ازاي نام آن فيلد نمايش يابد
باتشكر

ad.davachi
شنبه 18 دی 1389, 15:56 عصر
سلام. بیشتر توضیح دهید.

نیما حتمی
یک شنبه 19 دی 1389, 09:12 صبح
با سلام بنده هم همچین مشکلی رو دارم
کسی هست کمک کنه
توضیحات:
فرض کنید یه گرید دریم دارای 10 یا هرچند ستون(تعدادش مهم نیست) می خوام این گرید به هموم شکل در word ،Export کنم.

ad.davachi
یک شنبه 19 دی 1389, 14:10 عصر
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.

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)
{

}

The flow for the code behind page is as follows.

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)
{

}}

منبع:http://www.aspnettutorials.com/tutorials/file/gv-word-aspnet2-csharp.aspx

ali.rezaei7
یک شنبه 19 دی 1389, 15:33 عصر
درود. آقای ad.davachi شما کد ASP.NET گذاشتی ها!!!

در این مثال فرض بر این است:


پایگاه داده: SQL Server Express
نام پایگاه داده: db1
نام جدول: t1
فیلدها: id,bin

کد ذخیره هر گونه فایل داخل دیتابیس:


SqlConnection con =
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();
بجای FileName مسیر فایلی رو که باید ذخیره بشه رو قرار بده.

کد بازیابی فایل و نمایش آن:

SqlConnection con = new
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();
در مثال بالا رکوردی که فیلد id آن برابر با یک است رو انتخاب میکنه. و فایل موجود در فیلد bin رو روی هارد ذخیره می کنه.
بجای NewFileName مسیری رو برای ذخیره فایل قرار بده.