gridview显示Excel内容(不支持合并单元格等复杂内容)

来源:互联网 发布:雪域藏宝胶囊淘宝店 编辑:程序博客网 时间:2024/06/10 00:01

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString;
        //office 2003
        ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/xml/zfzm.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"";
        //office 2007
        //ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0,Data Source" + Server.MapPath("~/xml/zfzm.xlsx") + ";Extended Properties=Excel 12.0;HDR=Yes;IMEX=1;";
        OleDbConnection DBConnection = new OleDbConnection(ConnectionString);
        DBConnection.Open();
        string SQLString = "SELECT * FROM [Sheet1$]";
        OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
        IDataReader DBReader = DBCommand.ExecuteReader();
        GridView1.DataSource = DBReader;
        GridView1.DataBind();
        DBReader.Close();
        DBConnection.Close();
    }

注:不支持合并单元格,设置单元格格式等复杂内容