Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开ExcelC VS2005连接时出错_C#_Visual Studio_Excel_Visual Studio 2005 - Fatal编程技术网

C# 打开ExcelC VS2005连接时出错

C# 打开ExcelC VS2005连接时出错,c#,visual-studio,excel,visual-studio-2005,C#,Visual Studio,Excel,Visual Studio 2005,我正在尝试将.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 strFileN

我正在尝试将.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文件已成功保存。需要有经验的人的帮助。谢谢

我强烈建议您不要使用OLE访问Excel文档。有无数的小故障和bug。 通常,当一列的不同单元格可以包含不同的数据类型时,使用sql访问数据是毫无意义的。但即使没有这个,也有足够的bug


将COM对象用于Excel。否则,您将有一个硬信任我在地板上:-

连接字符串数据源应该只包含到您的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文件的代码吗?谢谢你,这是我目前面临的错误,我不知道转换为什么会有错误。+1经历了这一过程,我比我想象的更快地返回到Interop。@MichałPowaga,是的,我有类似的经历。:-