PDA

View Full Version : سوال: گزارش گیری از GridView با فرمت xls یا اکسل



پیام حیاتی
چهارشنبه 14 بهمن 1394, 21:25 عصر
سلام
دو نمونه کد برای انجام این کار در نت پیدا کردم اما متاسفانه کل محتوای سایت رو در فایل اکسل درج میکنه :


Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls");
Response.ContentType = "application/excel";
StringWriter sWriter = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
gridReportView.RenderControl(hTextWriter);
Response.Write(sWriter.ToString());
Response.End();

و

//Response.Clear();
//Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
//Response.ContentType = "application/vnd.ms-excel";
//Response.ContentEncoding = System.Text.Encoding.Unicode;
//Response.BinaryWrite(System.Text.Encoding.Unicode. GetPreamble());
//this.EnableViewState = false;
//StringWriter sr = new StringWriter();
//HtmlTextWriter hr = new HtmlTextWriter(sr);
//gridReportView.RenderControl(hr);
//Response.Write(sr.ToString());
//sr.Dispose();
//hr.Dispose();
//Response.End();

چه راهکاری پیشنهاد می کنید؟

malloc
پنج شنبه 15 بهمن 1394, 09:05 صبح
سلام .... متوجه نشدم یعنی چی کل رو درج میکنه ؟؟؟؟ خوب شما همونقدری که میخوای رو بصورت متن بهش بده .

پیام حیاتی
پنج شنبه 15 بهمن 1394, 14:00 عصر
یعنی اینکه این صفحه من هست :
138817

خروجی که میگیرم ، زمان باز شدن این خطا رو دارم :

138818

وقتی هم ok می کنم باز میشه :

138819
138820

به چه شکل به صورت متن بهش بدم؟!

پیام حیاتی
جمعه 16 بهمن 1394, 09:43 صبح
بروزرسانی....

malloc
جمعه 16 بهمن 1394, 12:16 عصر
داداش من الان این کد رو زدم و جواب گرفتم .... شاید یکم بنظر بیاد که کند هست اما بنظر من مشکلی نداره ..... فقط استایل ها و تعداد هدر ها رو باید دقت کنی مثل گرید ویو خودت باشه چون گرید ویو من تعداد هدر هاش 5 تا بود


Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls");
Response.ContentType = "application/excel";
string HeadStyle = @"background-color:#aaa;color:#fff;font;font-family:tahoma;font-size:15px";
string OddRowStyle = @"background-color:#eee;font-family:tahoma;font-size:13px;border-left:1px solid";
string EvenRowStyle = @"background-color:#e1e1e1;font-family:tahoma;font-size:13px;border-left:1px solid";

string _Result = @"<table style=""direction:rtl;float:right"">";

_Result += @"<tr>";

_Result += @"<td style=""" + HeadStyle + @""">شناسه جواب</td>";
_Result += @"<td style=""" + HeadStyle + @""">شناسه سوال</td>";
_Result += @"<td style=""" + HeadStyle + @""">متن جواب</td>";
_Result += @"<td style=""" + HeadStyle + @""">مرتب سازی</td>";
_Result += @"<td style=""" + HeadStyle + @""">ضریب</td>";

_Result += @"</tr>";

int _index = 0;
string _RowStyle = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (_index % 2 == 0)
_RowStyle = EvenRowStyle;
else
_RowStyle = OddRowStyle;


_Result += @"<tr>";

_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[0].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[1].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[2].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[3].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[4].Text + @"</td>";

_Result += @"</tr>";

_index++;
}

_Result += @"</table>";


Response.Write(_Result);
Response.End();

پیام حیاتی
جمعه 16 بهمن 1394, 14:12 عصر
داداش من الان این کد رو زدم و جواب گرفتم .... شاید یکم بنظر بیاد که کند هست اما بنظر من مشکلی نداره ..... فقط استایل ها و تعداد هدر ها رو باید دقت کنی مثل گرید ویو خودت باشه چون گرید ویو من تعداد هدر هاش 5 تا بود


Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls");
Response.ContentType = "application/excel";
string HeadStyle = @"background-color:#aaa;color:#fff;font;font-family:tahoma;font-size:15px";
string OddRowStyle = @"background-color:#eee;font-family:tahoma;font-size:13px;border-left:1px solid";
string EvenRowStyle = @"background-color:#e1e1e1;font-family:tahoma;font-size:13px;border-left:1px solid";

string _Result = @"<table style=""direction:rtl;float:right"">";

_Result += @"<tr>";

_Result += @"<td style=""" + HeadStyle + @""">شناسه جواب</td>";
_Result += @"<td style=""" + HeadStyle + @""">شناسه سوال</td>";
_Result += @"<td style=""" + HeadStyle + @""">متن جواب</td>";
_Result += @"<td style=""" + HeadStyle + @""">مرتب سازی</td>";
_Result += @"<td style=""" + HeadStyle + @""">ضریب</td>";

_Result += @"</tr>";

int _index = 0;
string _RowStyle = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (_index % 2 == 0)
_RowStyle = EvenRowStyle;
else
_RowStyle = OddRowStyle;


_Result += @"<tr>";

_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[0].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[1].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[2].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[3].Text + @"</td>";
_Result += @"<td style=""" + _RowStyle + @""">" + GridView1.Rows[i].Cells[4].Text + @"</td>";

_Result += @"</tr>";

_index++;
}

_Result += @"</table>";


Response.Write(_Result);
Response.End();

ممنون سالار ، درست شد.

سیروس مقصودی
چهارشنبه 03 شهریور 1395, 15:47 عصر
با سلام
با اين تابع فقط تعداد رديفي كه در مونيتور نمايش داده ميشود به اكسل انتقال داده ميشود . چگونه ميتوان كل اطلاعات را به اكسل انتقال داد .

با تشكر

amir_T_2008
چهارشنبه 03 شهریور 1395, 16:12 عصر
با سلام
با اين تابع فقط تعداد رديفي كه در مونيتور نمايش داده ميشود به اكسل انتقال داده ميشود . چگونه ميتوان كل اطلاعات را به اكسل انتقال داد .

با تشكر

http://barnamenevis.org/showthread.php?528735-%D9%85%D8%B4%DA%A9%D9%84-%D8%AF%D8%B1-%D8%AE%D8%B1%D9%88%D8%AC%DB%8C-%D8%A8%D8%A7-%D9%81%D8%B1%D9%85%D8%AA-%D8%A7%DA%A9%D8%B3%D9%84-%D8%8C-%D8%B9%D8%A8%D8%A7%D8%B1%D8%A7%D8%AA-%D9%86%D8%A7%D9%85%D9%81%D9%87%D9%88%D9%85-%D8%A8%D9%87-%D8%AC%D8%A7%DB%8C-%D8%B9%D8%A8%D8%A7%D8%B1%D8%AA-%D9%87%D8%A7%DB%8C-%D9%81%D8%A7%D8%B1%D8%B3%DB%8C&p=2336765&viewfull=1#post2336765