【问题标题】:How to do Import-Export to Excel functionality in ASP.NET?如何在 ASP.NET 中执行导入导出到 Excel 功能?
【发布时间】:2011-05-23 06:45:08
【问题描述】:

在我的 asp.net Web 应用程序中,我需要在 Excel 中导入和导出数据。我该怎么做?

【问题讨论】:

  • 您要导出到 Excel 2003、2007 还是 2010? OleDb jet 版本因 Excel 的每个版本而异。
  • 您已经发布了 45 个问题,并且您还没有投过任何赞成票。

标签: asp.net export-to-excel import-from-excel


【解决方案1】:

这是在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();

这是从 Excel 导入数据的代码

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

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2019-03-12
    • 2020-02-19
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多