使用 OleDbDataAdapter 的 Excel 到网格视图问题

发布于 2024-10-31 07:46:24 字数 849 浏览 5 评论 0原文

我正在使用 Visual Studio 2010 创建 Windows 窗体应用程序。

我使用 OleDbDataAdapter 方法将数据从 excel 文件填充到 DataGridView。

这是我的代码

dataGridView1.DataSource = null;
dataGridView1.Update();

var connectionString =
     string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";", fileName);


var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);

var ds = new DataSet();
DataTable t = new DataTable();

adapter.Fill(t);

dataGridView1.DataSource = t; 

现在的问题是,如果某些单元格合并在 Excel 文件中,输出会有点不同。 这是为了更好地理解的图像。 在此处输入图像描述

那么如何解决这个问题呢?

我想如果我能识别合并单元格,那么我就可以解决这个问题。但我目前没有明确的想法。

有没有更好的方法在网格视图中表示 Excel 数据,就像在 Excel 文件中一样?

任何答案都会有所帮助。请分享任何建议。

谢谢

约翰

I am creating a Windows Forms Application using Visual Studio 2010.

I populate data to a DataGridView from an excel file using OleDbDataAdapter method.

Here is my code

dataGridView1.DataSource = null;
dataGridView1.Update();

var connectionString =
     string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";", fileName);


var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);

var ds = new DataSet();
DataTable t = new DataTable();

adapter.Fill(t);

dataGridView1.DataSource = t; 

Now Problem is if some cells are merged in the excel file output get bit different.
Here is the image for better understanding.
enter image description here

So how can I fix this problem ?

I think if I can identify the merge cells then I can fix this. But I don't have a clear idea at the moment.

Is there a better way to represent excel data in grid view as it is in the excel file ?

Any answer would be help. Please share any suggestions.

Thanks

Yohan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-11-07 07:46:25

我的解决方案:

protected void Page_Load(object sender, EventArgs e)
{
    string path = @"C:\samples\firstexcel.xlsx";
    GetExcelSheetNames(path);
}

private void GetExcelSheetNames(string excelFile)
{
    OleDbConnection objConn = null;
    System.Data.DataTable dt = null;
    try
    {
        DataSet ds = new DataSet();
        // Connection String. 
        String connString = @"Data Source=" + excelFile +            ";
        Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
        // Create connection. 
        objConn = new OleDbConnection(connString);
        // Opens connection with the database. 
        objConn.Open();
        // Get the data table containing the schema guid, and also sheet names. 
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        if (dt == null)
        {
            return;
        }

        String[] excelSheets = new String[dt.Rows.Count];
        int i = 0;
        // Add the sheet name to the string array. 
        // And respective data will be put into dataset table 

        foreach (DataRow row in dt.Rows)
        {
            excelSheets[i] = row["TABLE_name"].ToString();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            oleda.Fill(ds, "TABLE");
            i++;
        }

        // Bind the data to the GridView 
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
        Session["Table"] = ds.Tables[0];
    }

    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    finally
    {
        // Clean up. 
        if (objConn != null)
        {
            objConn.Close();
            objConn.Dispose();
        }

        if (dt != null)
        {
            dt.Dispose();
        }
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
    GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{;}

My solution:

protected void Page_Load(object sender, EventArgs e)
{
    string path = @"C:\samples\firstexcel.xlsx";
    GetExcelSheetNames(path);
}

private void GetExcelSheetNames(string excelFile)
{
    OleDbConnection objConn = null;
    System.Data.DataTable dt = null;
    try
    {
        DataSet ds = new DataSet();
        // Connection String. 
        String connString = @"Data Source=" + excelFile +            ";
        Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
        // Create connection. 
        objConn = new OleDbConnection(connString);
        // Opens connection with the database. 
        objConn.Open();
        // Get the data table containing the schema guid, and also sheet names. 
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        if (dt == null)
        {
            return;
        }

        String[] excelSheets = new String[dt.Rows.Count];
        int i = 0;
        // Add the sheet name to the string array. 
        // And respective data will be put into dataset table 

        foreach (DataRow row in dt.Rows)
        {
            excelSheets[i] = row["TABLE_name"].ToString();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            oleda.Fill(ds, "TABLE");
            i++;
        }

        // Bind the data to the GridView 
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
        Session["Table"] = ds.Tables[0];
    }

    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    finally
    {
        // Clean up. 
        if (objConn != null)
        {
            objConn.Close();
            objConn.Dispose();
        }

        if (dt != null)
        {
            dt.Dispose();
        }
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
    GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文