Dzwebs.Net

撰写电脑技术杂文十余年

net c#代码读取服务器端的Excel内容显示到客户端界面

Admin | 2013-5-9 9:10:36 | 被阅次数 | 6304

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  下面的这个代码功能是:把服务器端的Excel导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel工作薄里面的一个工作表的名称。

  public DataSet ExcelDataSource(string filepath, string sheetname)
  {
   string strConn;
   strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
        DataSet ds = new DataSet();
        oada.Fill(ds);
        conn.Close();
        return ds;
  }

  //获得Excel中的所有sheetname。
  public ArrayList ExcelSheetName(string filepath)
  {
        ArrayList al = new ArrayList();
        string strConn;
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataTable sheetNames = conn.GetOleDbSchemaTable
        (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
        conn.Close();
        foreach (DataRow dr in sheetNames.Rows)
        {
            al.Add(dr[2]);
        }
        return al;
    }


该杂文来自: 网站开发杂文

上一篇:C#代码直接生成质量更佳的JPG图片

下一篇:ASP.NET2.0程序性能优化

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计