导入和导出Excel工作表时出错 [英] Error while importing and exporting Excel sheet

查看:124
本文介绍了导入和导出Excel工作表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在显示Excel工作表并导入该工作表时出现错误如何解决此问题如果我更改导出工作表名称工作表导入否则出现错误



While Expoting Excel Sheet And Importing That Sheet Got Error How To Fix This If I Change Exports Sheet Name Sheet Is Importing Otherwise It Got Error

System.Data.OleDb.OleDbException (0x80004005): External table is not in the expected format. at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at iqmsampleworks_project.Areas.admin.Controllers.adminController.import(HttpPostedFileBase file, HttpPostedFileBase file1) in f:\Final\IQM SAMPLE WORK - Copy\Areas\admin\Controllers\adminController.cs:line 84







public ActionResult import(HttpPostedFileBase file, HttpPostedFileBase file1)
{
    try
    {
   


        if (file1 != null)
        {
            file1.SaveAs(HttpContext.Server.MapPath("~/File/") + file1.FileName);

        }
        
       
        if (file != null)
        {
            string path = string.Concat(Server.MapPath("~/File3/" + file.FileName));
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
             
            }
            
            file.SaveAs(path);
            //ViewData["Test"] = path;
            // Connection String to Excel Workbook
            string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
            OleDbConnection connection = new OleDbConnection();
            connection.ConnectionString = excelConnectionString;
            OleDbCommand command = new OleDbCommand("select * from [file.FileName]", connection);
            connection.Open();
            DataTable dt = new DataTable();

            dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (dt == null)
            {
                return null;
            }

            String[] excelSheets = new String[dt.Rows.Count];
            int t = 0;
           
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[t] = row["TABLE_NAME"].ToString();
                t++;
            }
            
            DataSet ds = new DataSet();
            string query = string.Format("Select * from [{0}]", excelSheets[0]);
            using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connection))
            {
                dataAdapter.Fill(ds);
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                ConfigurationTable model = new ConfigurationTable();
                string b = ds.Tables[0].Rows[i]["ID"].ToString();
                int c = Convert.ToInt16(b);
                int cnt = db.ConfigurationTables.Where(a => a.ID == c).Count();
                if (cnt == 1)
                {

                    ConfigurationTable model1 = db.ConfigurationTables.SingleOrDefault(e => e.ID == c);
                    model1.ID = Convert.ToInt16(ds.Tables[0].Rows[i]["ID"].ToString());
                    model1.DomainName = ds.Tables[0].Rows[i]["DomainName"].ToString();
                    model1.Type = ds.Tables[0].Rows[i]["Type"].ToString();
                    model1.Category = ds.Tables[0].Rows[i]["Category"].ToString();
                    model1.SampleName = ds.Tables[0].Rows[i]["SampleName"].ToString();
                    model1.DevelopedBy = ds.Tables[0].Rows[i]["DevelopedBy"].ToString();
                    model1.url = ds.Tables[0].Rows[i]["url"].ToString();
                    model1.UserType = ds.Tables[0].Rows[i]["UserType"].ToString();
                    model1.UserType1 = ds.Tables[0].Rows[i]["UserType1"].ToString();
                    model1.UserType2 = ds.Tables[0].Rows[i]["UserType2"].ToString();
                    model1.UserType3 = ds.Tables[0].Rows[i]["UserType3"].ToString();
                    model1.UserType4 = ds.Tables[0].Rows[i]["UserType4"].ToString();
                    model1.UserType5 = ds.Tables[0].Rows[i]["UserType5"].ToString();
                    model1.UserType6 = ds.Tables[0].Rows[i]["UserType6"].ToString();
                    model1.U

推荐答案

错误意味着工作表不符合您的代码所期望的格式(例如,单元格的数据类型错误)。这就是为什么它适用于一张而不是另一张。
The error means that the sheet is not in the format that your code expects ( for example, a cell has the wrong data type ). That's why it works for one sheet and not another.


这篇关于导入和导出Excel工作表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆