【问题标题】:Error in opening ExcelConnection C# VS2005打开 ExcelConnection C# VS2005 时出错
【发布时间】:2011-11-09 13:38:03
【问题描述】:

我正在尝试将 .csv 文件导入我的数据库。我可以将 excel 工作表导入我的数据库,但是由于 .csv 和 .xls 的文件格式不同,我需要专门为 .csv 制作一个导入功能。

下面是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
    // Get the name of the Excel spreadsheet to upload. 
    string strFileName = Server.HtmlEncode(FileUpload1.FileName);

    // Get the extension of the Excel spreadsheet. 
    string strExtension = Path.GetExtension(strFileName);

    // Validate the file extension. 
    if (strExtension != ".xls" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
    {
        Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
        return;
    }

                // Generate the file name to save. 
        string strUploadFileName = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;

        // Save the Excel spreadsheet on server. 
        FileUpload1.SaveAs(strUploadFileName);

        // Create Connection to Excel Workbook
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Text;";
        using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)){
        OleDbCommand ExcelCommand = new OleDbCommand("SELECT [columns] FROM +userrolelist", ExcelConnection);

        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);

        ExcelConnection.Open();

    using (DbDataReader dr = ExcelCommand.ExecuteReader())
    {
        // SQL Server Connection String
        string sqlConnectionString = "Data Source=<IP>;Initial Catalog=<DB>;User ID=<userid>;Password=<password>";

        // Bulk Copy to SQL Server
        using (SqlBulkCopy bulkCopy =
                   new SqlBulkCopy(sqlConnectionString))
        {
            bulkCopy.DestinationTableName = "DEMUserRoles";
            bulkCopy.WriteToServer(dr);
            Response.Write("<script>alert('DEM User Data imported');</script>");

        }
    }
    }
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");

}

文件已成功保存,但错误提示文件路径无效,即使文件已成功保存为 .csv,因此我无法继续导入数据的过程进入我的数据库。

以下是我的错误截图:

总之,我遇到的错误是保存 csv 文件的文件路径无效,尽管 csv 文件已成功保存。需要有经验的帮助。谢谢你

【问题讨论】:

    标签: c# visual-studio excel visual-studio-2005


    【解决方案1】:

    连接字符串数据源应仅包含 CSV 文件的路径。它不应包含 CSV 文件名。

    文件名在 SQL 语句中指定为表。

    string dir = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\";
    string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
    
    string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
    
    OleDbCommand ExcelCommand = new OleDbCommand(
        "SELECT [columns] FROM " + mycsv, ExcelConnection);
    

    【讨论】:

    • 谢谢坎贝尔,它似乎对我有用。但是,在将管道分隔的文本文件转换为 .csv 文件时,我遇到了一些问题,并且某些列变量分成两部分并跳到下一列,弄乱了行,所以我必须解决为什么会这样。你介意看看我用于将管道分隔文件转换为 .csv 文件的代码吗?谢谢
    • stackoverflow.com/questions/8061307/… 这是我目前面临的错误,我不知道为什么转换中有错误。
    【解决方案2】:

    我强烈不建议您使用 OLE 访问 Excel 文档。有无数的故障和错误。 一般来说,当一列的不同单元格可以包含不同的数据类型时,使用 sql 访问数据 - 是无稽之谈。但即使没有这个,也有足够多的错误。

    在 Excel 中使用 COM 对象。否则,你会很难 - 相信我 :-)

    【讨论】:

    • +1 经历了这些,我回到Interop的速度比我想象的要快。
    • @MichałPowaga,是的,我也有类似的经历。 :-)
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2014-03-04
    • 2016-09-04
    相关资源
    最近更新 更多