0

私は、すべての Excel データを SQL Server テーブルに入れる必要がある asp.net に取り組んでいます。このために、ファイル アップロードを使用して Excel ファイルをアップロードし、ボタンをクリックすると、Excel のすべてのデータを SQL Server 2008 に挿入します。ボタンをクリックすると、次のコードを使用しました。

protected void ImportNow_Click(object sender, EventArgs e)
{
    private static string _connStr = System.Configuration.ConfigurationManager.AppSettings["Database1"].ToString();
        try
        {
            if ((fileuploadExcel.FileName != ""))
            {
                string extension = Path.GetExtension(fileuploadExcel.PostedFile.FileName);
                string excelConnectionString;
                SqlConnection conn = new SqlConnection(_connStr);
                string tableName = "School";
                string path = fileuploadExcel.PostedFile.FileName;

                //Create connection string to Excel work book
                if (extension == ".xls")
                {
                    excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
                                                          ";Extended Properties=Excel 8.0;Persist Security Info=False";
                }
                else
                {
                    excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path +
                                                         ";Extended Properties=Excel 12.0;Persist Security Info=False";
                }

                //Create Connection to Excel work book
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                //Create OleDbCommand to fetch data from Excel            
                conn.Open();
                SqlCommand comm = new SqlCommand("truncate table " + tableName, conn);
                SqlCommand identityChange = conn.CreateCommand();
                identityChange.CommandText = "SET IDENTITY_INSERT " + tableName + " ON";
                OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection);
                excelConnection.Open();
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();
                identityChange.ExecuteNonQuery();
                SqlBulkCopy sqlBulk = new SqlBulkCopy(_connStr);
                //Give your Destination table name
                sqlBulk.DestinationTableName = tableName;
                sqlBulk.WriteToServer(dReader);
                excelConnection.Close();
                conn.Close();
                lblMessage.ForeColor = Color.Green;
                lblMessage.Text = "Import into table <b>" + tableName + "</b> successful!<br />";
            }
            else
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please first upload (Select) excel file.";
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

Excelファイルをアップロードして読み取ると、エラーが表示されます

Microsoft Office Access データベース エンジンは、オブジェクト 'Sheet1$' を見つけることができませんでした。オブジェクトが存在すること、およびその名前とパス名のつづりが正しいことを確認してください。

を持つファイルがあってもSheet1. 私を助けてください。ありがとう

4

1 に答える 1

0

アップロードしたファイルのパスがデータソースに与える際に有効かどうかを確認してください。コード以来

string path = fileuploadExcel.PostedFile.FileName; 

一部のブラウザーでは、ファイル パスの取得に失敗します。こちらこちらを参照してください。として保存されるファイルのパスを指定してみてください。

string path = @"c:\documents\files\"+ fileuploadExcel.PostedFile.FileName;
于 2014-10-15T06:18:05.977 に答える