在html中显示的数据..我必须在excel中导出数据. [英] Displayed data in html .. i have to export data in excel..

查看:128
本文介绍了在html中显示的数据..我必须在excel中导出数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将从数据库中提取的数据导出到Excel工作表.我该怎么办..我在这里以html显示数据..我必须在excel中导出数据.



公共无效函数(字符串fname)
{
strhtmlcontent.Append(< div id =''id1''><表align =''Center''style =''background-color:Silver''BORDER-COLLAPSE:collapse cellSpacing = 0 rules = all border = 1 = 1宽度="100%">第1宽度= 8%< FirstName</th>第1宽度= 15%>产品名<第th宽度= 15%>客户名称/第th个宽度= 10%金额//第个宽度= 15%>活动日期);

SqlConnection scon =新的SqlConnection(ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString);

scon.Open();
如果(fname ==全部")
{
scmd =新的SqlCommand("SELECT usermaster.firstname,Product_Master.product_name,Product_Master.client_name,Product_Master.amount,Product_Master.activity_date来自usermaster内连接,product_master ON usermaster.product_id = Product_Master.product_id",scon);
}
其他
{

scmd = new SqlCommand("SELECT usermaster.firstname,Product_Master.product_name,Product_Master.client_name,Product_Master.amount,Product_Master.activity_date来自usermaster内联接product_master ON usermaster.product_id = Product_Master.product_id,其中firstname =""+ fname +"",scon);

}
dr = scmd.ExecuteReader();




字符串firstname =",product_name =",activity_date =";

字符串client_name =",amount =";

while(dr.Read())
{
firstname = dr ["firstname"].ToString();
product_name = dr ["product_name"].ToString();
client_name = dr ["client_name"].ToString();
数量= dr [数量"] .ToString();
activity_date = dr ["activity_date"].ToString();


strhtmlcontent.Append(< tr>< td align =" center>" +名字+& nbsp;</td>< td align =" center>" + product_name +& nbsp;/td>< td align =" center>" + client_name +& nbsp;</td>< td align =''center">"+金额+"/td>< td align =" center>" + activity_date + </td></tr>"));


}

dr.Close();
strhtmlcontent.Append(</table></div>");
HttpContext.Current.Response.Write(strhtmlcontent);
HttpContext.Current.Response.Flush();
//HttpContext.Current.Response.End();
}

I have to export fetched data from database to excel sheet. How can i do..Here i have displayed data in html .. i have to export data in excel.



public void Function(string fname)
{
strhtmlcontent.Append("<div id=''id1''><table align=''Center'' style=''background-color:Silver'' BORDER-COLLAPSE:collapse cellSpacing=0 rules=all border=1 width=''100%''><th width=8%>FirstName</th><th width=15%>Product Name</th><th width=15%>Client Name</th><th width=10%>Amount</th><th width=15%>Activity Date</th>");

SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

scon.Open();
if (fname == "All")
{
scmd = new SqlCommand("SELECT usermaster.firstname, Product_Master.product_name, Product_Master.client_name, Product_Master.amount, Product_Master.activity_date FROM usermaster INNER JOIN Product_Master ON usermaster.product_id = Product_Master.product_id", scon);
}
else
{

scmd = new SqlCommand("SELECT usermaster.firstname, Product_Master.product_name, Product_Master.client_name, Product_Master.amount, Product_Master.activity_date FROM usermaster INNER JOIN Product_Master ON usermaster.product_id = Product_Master.product_id where firstname=''"+fname+"''", scon);

}
dr = scmd.ExecuteReader();




string firstname = "", product_name = "", activity_date = "";

string client_name = "", amount = "";

while (dr.Read())
{
firstname = dr["firstname"].ToString();
product_name = dr["product_name"].ToString();
client_name = dr["client_name"].ToString();
amount = dr["amount"].ToString();
activity_date = dr["activity_date"].ToString();


strhtmlcontent.Append("<tr><td align=''center''>" + firstname + "&nbsp;</td><td align=''center''>" + product_name + "&nbsp;</td><td align=''center''>" + client_name + "&nbsp;</td><td align=''center''>" + amount + "&nbsp;</td><td align=''center''>" + activity_date + "&nbsp;</td></tr>");


}

dr.Close();
strhtmlcontent.Append("</table></div>");
HttpContext.Current.Response.Write(strhtmlcontent);
HttpContext.Current.Response.Flush();
//HttpContext.Current.Response.End();
}

推荐答案

excell的数据表
Datatable to excell
private void ExportDataTable(DataTable dt)
       {
           string attachment = "attachment; filename=a.xls";
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.AddHeader("content-disposition", attachment);
           HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
           string sTab = "";
           foreach (DataColumn dc in dt.Columns)
           {
               HttpContext.Current.Response.Write(sTab + dc.ColumnName);
               sTab = "\t";
           }
           HttpContext.Current.Response.Write("\n");

           int i;
           foreach (DataRow dr in dt.Rows)
           {
               sTab = "";
               for (i = 0; i < dt.Columns.Count; i++)
               {
                   HttpContext.Current.Response.Write(sTab + dr[i].ToString());
                   sTab = "\t";
               }
               HttpContext.Current.Response.Write("\n");
           }
           HttpContext.Current.Response.End();
       }


尝试一下....
受保护的void Button1_Click(对象发送者,EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition","attachment; filename = GridView1.xls");
Response.Charset =";
Response.ContentType ="application/vnd.xls";
StringWriter StringWriter =新的System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter =新的HtmlTextWriter(StringWriter);
GridView1.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}

公共重写void VerifyRenderingInServerForm(Control control)
{

}
Try this....
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=GridView1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridView1.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{

}


首先,我要求您使用更好的代码格式发布问题,以使其看起来更具可读性.

我可以在您的代码中看到数据检索和显示机制是在相同的地方尝试实现的.这不是一个好习惯.您应该这样思考:

1.最好通过数据访问类函数检索数据.
2.您将数据显示为html.
3.您将数据导出为ex​​cel.

看,这里您的数据与html没有关系.您可以通过单独的功能来掌握数据.您需要该数据,并且可以将其转换为任何格式.

希望这有道理.
1st of all I would request you to post your questions with better code formatting so that it looks more readable.

I can see in your code data retrieval and display mechanism is tried to implement in the same place. Which is not a good practice. You should think like the following:

1. You retrieve your data preferably through a data access class function.
2. You display data as html.
3. You export data as excel.

See, here your data is no way related to html. You have your data in your hand from a separate function. You call for that data and you can take it to any format.

Hope this make sense.


这篇关于在html中显示的数据..我必须在excel中导出数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆