ASP.NET 中的 Excel 文件上传问题

发布于 2025-01-04 22:07:08 字数 1445 浏览 1 评论 0原文

我有一个应用程序,我在其中上传 Excel 文件并在 GridView 中显示数据。 我现在的问题是,当我上传文件时,文件就被上传了。如果数据包含整数和文本数据的混合,则文本数据不会显示在 Gridview 和我加载它的数据集中。

public DataTable GetExcelData(string _FileName)
{
    DataSet ds = new DataSet();

    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties=Excel 8.0;";

    OleDbConnection connection = new OleDbConnection(connectionString);
    connection.Open();

    string sheetname = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["table_name"].ToString();
    try
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetname + "]", connection);
        adapter.Fill(ds);
        connection.Close();
        return ds.Tables[0]; ;
    }
    catch (Exception ex)
    {
        try
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        catch (Exception) { }

        throw ex;
    }
}

这是我将数据加载到数据集的代码。

我将向您展示一个示例数据


Column1

  1. 1234
  2. 2345
  3. 4567
  4. T123
  5. 123Q
  6. 6789

如果我们上传包含上述数据的 Excel 工作表,它只会像这样显示。


Column1

  1. 1234
  2. 2345
  3. 4567
  4. 6789

您可以在上面的T123中看到,缺少123Q(这将是空白数据)。这是我上传 Excel 工作表时应用程序出现的问题。

有人遇到过这样的情况吗?我们有什么办法可以解决这个问题吗?

I have a Application on which i'm uploading a Excel File and displaying the data inside a GridView.
My problems is now that, When I'm uploading the File gets uploaded. If the Data contains a MIX of Intergers and Text data, Text data's are not shown inside the Gridview and the DataSet on which im loading it.

public DataTable GetExcelData(string _FileName)
{
    DataSet ds = new DataSet();

    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties=Excel 8.0;";

    OleDbConnection connection = new OleDbConnection(connectionString);
    connection.Open();

    string sheetname = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["table_name"].ToString();
    try
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetname + "]", connection);
        adapter.Fill(ds);
        connection.Close();
        return ds.Tables[0]; ;
    }
    catch (Exception ex)
    {
        try
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        catch (Exception) { }

        throw ex;
    }
}

This is code from which i'm loading the data onto the DataSet.

Ill show you an Example data


Column1

  1. 1234
  2. 2345
  3. 4567
  4. T123
  5. 123Q
  6. 6789

If we upload an Excel sheet with the Data Above it will only display it like this..


Column1

  1. 1234
  2. 2345
  3. 4567
  4. .
  5. .
  6. 6789

You can see in the Above T123,123Q is missing(it will be BLANK data). This is the Problem with my application when i'm Uploading an Excel sheet.

Has anyone come across a situation like this? Is there any way we can resolve this?

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

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

发布评论

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

评论(1

离线来电— 2025-01-11 22:07:08

尝试这个连接字符串...现在工作正常

这个连接字符串是通过使用其对 XLS 的工作来定义的

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"

For XLSX

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _FileName +";Extended Properties='Excel 12.0;HDR=YES;'"

Try this connection string... Its work fine now

This connection string is define by use its work for XLS

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"

For XLSX

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _FileName +";Extended Properties='Excel 12.0;HDR=YES;'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文