Oledb Exception - 在上传excel文件时定义了许多字段

时间:2011-08-04 08:51:33

标签: c# file-upload export-to-excel

这是我上传excel的编码......

if (RevenueDumpFileUpload.HasFile)
        {
            string strFilePathOnServer = ConfigurationManager.AppSettings["RevenueDumpFileLocation"];
            String sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strFilePathOnServer) + RevenueDumpFileUpload.FileName + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
            string strPostedFileName = RevenueDumpFileUpload.PostedFile.FileName;
            if (strPostedFileName != string.Empty && RevenueDumpFileUpload.PostedFile.ContentLength != 0)
            {
                //Save-Upload File to server. 
                RevenueDumpFileUpload.PostedFile.SaveAs(Server.MapPath(strFilePathOnServer) + RevenueDumpFileUpload.FileName);
                RevenueDumpFileUpload.FileContent.Dispose();
            }
            OleDbConnection Exlcon = new OleDbConnection(sConnectionString);
            try
            {
                //Exlcon.Open();
            }
            catch
            {
                return;
            }
            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Owner$]", Exlcon);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
            objAdapter1.SelectCommand = objCmdSelect;
            objDataset1.Clear();
            objAdapter1.Fill(objDataset1, "XLData");
            DataRow rowDel = objDataset1.Tables["XLData"].Rows[0];
            objDataset1.Tables["XLData"].Rows.Remove(rowDel);
            objDataset1.Tables["XLData"].Columns[0].ColumnName = "Industry";
            objDataset1.Tables["XLData"].Columns[1].ColumnName = "Company Name";
            objDataset1.Tables["XLData"].Columns[2].ColumnName = "Website";
            objDataset1.Tables["XLData"].Columns[3].ColumnName = "Address";
            objDataset1.Tables["XLData"].Columns[4].ColumnName = "State";
            objDataset1.Tables["XLData"].Columns[5].ColumnName = "Company PhoneNumber";
            objDataset1.Tables["XLData"].Columns[6].ColumnName = "Contact Person";
            objDataset1.Tables["XLData"].Columns[7].ColumnName = "Title Description";
            objDataset1.Tables["XLData"].Columns[8].ColumnName = "Company Size";
            objDataset1.Tables["XLData"].Columns[9].ColumnName = "Mail ID";
            objDataset1.Tables["XLData"].Columns[10].ColumnName = "Guess MailID";
            objDataset1.Tables["XLData"].Columns[11].ColumnName = "Phone No";
            objDataset1.Tables["XLData"].Columns[12].ColumnName = "Linked in id";
            objDataset1.Tables["XLData"].Columns[13].ColumnName = "Comment";
            methodtosave();
        }

我正在获取错误填充方法......“太多的字段未定义”。 只有“14”栏.....

3 个答案:

答案 0 :(得分:1)

请看下面的代码,尝试在下面的select字符串中给出所有的locums,并检查连接字符串..

string sql = "SELECT F1, F2, F3, F4, F5 FROM [sheet1$] WHERE F1 IS NOT NULL";

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + PrmPathExcelFile + @";Extended Properties=""Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text""");

OleDbCommand cmd = new OleDbCommand(sql, connection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);

DataSet ds = new DataSet();
ds.Tables.Add("xlsImport", "Excel");
da.Fill(ds, "xlsImport");

// Remove the first row (header row)
DataRow rowDel = ds.Tables["xlsImport"].Rows[0];
ds.Tables["xlsImport"].Rows.Remove(rowDel);

ds.Tables["xlsImport"].Columns[0].ColumnName = "LocationID";
ds.Tables["xlsImport"].Columns[1].ColumnName = "PartID";
ds.Tables["xlsImport"].Columns[2].ColumnName = "Qty";
ds.Tables["xlsImport"].Columns[3].ColumnName = "UserNotes";
ds.Tables["xlsImport"].Columns[4].ColumnName = "UserID";

connection.Close(); 

  var data = ds.Tables["xlsImport"].AsEnumerable();
    var query = data.Where(x => x.Field<string>("LocationID") == "COOKCOUNTY").Select(x =>
                new Contact
                {
                    LocationID= x.Field<string>("LocationID"),
                    PartID = x.Field<string>("PartID"),
                    Quantity = x.Field<string>("Qty"),
                    Notes = x.Field<string>("UserNotes"),
                    UserID = x.Field<string>("UserID")
                });

OleDB & mixed Excel datatypes : missing data

答案 1 :(得分:0)

错误是说字段是未定义的,所以我想你的数据表对于数据中的至少一些行来说比14列宽。 Excel可能很有趣,因为空的但初始化的单元格被认为是数据单元格。

查看objDataset1.Tables["XLData"].Columns.Count并查看它返回的内容。您可能需要在列名称赋值的末尾添加一个函数,该函数循环其余列并指定任意名称(列{x})。

您也可以将select语句更改为仅检索前14行,而不是选择*。

答案 2 :(得分:0)

我认为您的数据集存在问题。 objDataset1.Clear();只是清除数据,而不是结构。尝试使用objDataset1 = new DataSet();

修改

尝试使用连接字符串:

@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
            + Server.MapPath(strFilePathOnServer) 
            + RevenueDumpFileUpload.FileName 
            + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";