2012-02-20 56 views
-1

我必須將每個數據導出爲ex​​cel表格或ant格式,例如pdf,word等。我該怎麼做(將數據寫入任何格式)...我已經在html中顯示數據..我必須以word或任何格式導出。將每個數據導出爲ex​​cel表格或任何格式

公共無效的功能(字符串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(); 

}

+0

您可以隨時使用水晶報表和導出結果集爲PDF ...查看該http://www.codeproject.com/Articles/14597/Convert-a-Crystal-Report-to- PDF或這裏這裏http://vb.net-informations.com/crystal-report/vb.net_crystal_report_export_pdf.htm – 2012-02-20 08:14:40

+0

我知道使用水晶我可以做到這一點,但要求是以上.. – harsh 2012-02-20 08:16:46

回答

1

使用ADO.NET

連接字符串應該是這樣的: -

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; 

例如:

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 

using (DbConnection connection = factory.CreateConnection()) 
{ 
    connection.ConnectionString = connectionString; 

    using (DbCommand command = connection.CreateCommand()) 
    { 
     command.CommandText = "INSERT INTO [Cities$] (ID, City, State) VALUES(4,\"Tampa\",\"Florida\")"; 

     connection.Open(); 

     command.ExecuteNonQuery(); 
    } 
} 

參考Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory

+0

當我運行我的網頁時,它獲取數據準確就像在數據庫中...是的,我想從我的網頁上輸出相同的數據(不是HTML)到任何格式,就像它在瀏覽器上顯示的一樣。 – harsh 2012-02-20 09:45:41

相關問題