2011-05-23 7 views
2

私のasp.net Webアプリケーションでは、私はExcelとの間でデータをインポートおよびエクスポートする必要があります。どうすればいい?ここASP.NETでExcelの機能にインポート/エクスポートする方法は?

+0

からデータをインポートするためのコードであるあなたは、Excel 2003、2007、または2010へエクスポートするために探していますか? OleDbジェットのバージョンはExcelの各バージョンによって異なります。 – bitxwise

+0

投稿45件の質問がありますが、投票はまだ行われていません。 –

答えて

0

は、Excel

StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    string attachment = "attachment; filename=excel" + ".xls"; 
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", attachment); 
    rptMain.DataBind(); 
    rptMain.RenderControl(htw); 
    Response.Write(sw.ToString()); 
    Response.Flush(); 
    Response.End(); 

でデータをエクスポートするためのコードであり、ここでエクセル

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
     DbDataAdapter adapter = factory.CreateDataAdapter(); 
     DbCommand selectCommand = factory.CreateCommand(); 
     selectCommand.CommandText = "SELECT ColumnNames FROM [Sheet1$]"; 
     DbConnection connection = factory.CreateConnection(); 
     connection.ConnectionString = connectionString; 
     selectCommand.Connection = connection; 
     adapter.SelectCommand = selectCommand; 
     DataTable dtbl = new DataTable(); 
     adapter.Fill(dtbl); 

     if (dtbl.Rows.Count > 0) 
     { 
     ............. 
     ............. 
     } 
関連する問題