繁体   English   中英

使用asp.net和C#将Excel数据导入数据库

[英]importing excel data to database using asp.net and c #

尝试将数据从excel导入数据库时​​出现以下错误。

Microsoft Office Access数据库引擎找不到对象'C:\\ Users \\ DAKTARI \\ Desktop \\ smarttable.xls'

这是我正在使用的代码。

 public partial class Smarttable : System.Web.UI.Page
{
    OleDbConnection Econ;
    SqlConnection con;

    string constr, Query, sqlconn;
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    private void ExcelConn(string FilePath)
    {

        constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DAKTARI\Desktop\smarttable.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES;""");
        Econ = new OleDbConnection(constr);

    }
    private void connection()
    {
        sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString;
        con = new SqlConnection(sqlconn);

    }


    private void InsertExcelRecords(string FilePath)
    {
        ExcelConn(FilePath);

        Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");
        OleDbCommand Ecom = new OleDbCommand(Query, Econ);
        Econ.Open();

        DataSet ds = new DataSet();
        OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
        Econ.Close();
        oda.Fill(ds);
        DataTable Exceldt = ds.Tables[0];
        connection();
        //creating object of SqlBulkCopy    
        SqlBulkCopy objbulk = new SqlBulkCopy(con);
        //assigning Destination table name    
        objbulk.DestinationTableName = "smarttable";
        //Mapping Table column    
        objbulk.ColumnMappings.Add("InvoiceNumber", "InvoiceNumber");
        objbulk.ColumnMappings.Add("AmountPaid", "AmountPaid");
        objbulk.ColumnMappings.Add("Remarks", "Remarks");
        //inserting Datatable Records to DataBase    
        con.Open();
        objbulk.WriteToServer(Exceldt);
        con.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
        InsertExcelRecords(CurrentFilePath);
    }
}

您的Excel文件格式使用XLS,这意味着适用于Office 2003或更早版本,但是您使用的是用于Office 2007或更高版本的ACE OLEDB提供程序:

constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DAKTARI\Desktop\smarttable.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES;"");

正确的用法是使用像这样的Jet 4.0提供程序:

constr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;'", FilePath);

另外还有第二个问题,即使用错误的查询字符串读取工作表中的数据:

Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");

应该在下面将其更改为适当的形式:

Query = "SELECT [InvoiceNumber],[AmountPaid],[Remarks] FROM [Orders$]";

更改此行

Query = string.Format("Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [C:\\Users\\DAKTARI\\Desktop\\smarttable.xls]", "Orders$");

Query = "Select [InvoiceNumber],[AmountPaid],[Remarks] FROM [Orders$]";

FROM后面必须加上工作表名称,该名称被视为表格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM