2012-08-30 66 views
1

我想从excel表格数据插入到数据表中,这是工作的罚款。但我不知道为什么我现在得到以下错误如何插入从excel表数据到数据表在asp.net?

“Microsoft Jet数据库引擎找不到对象'Sheet1'。请确保该对象存在,并且正确拼写其名称和路径名称。

我的代码检索数据是

if (fupExcel.HasFile) 
     { 
      // Get the name of the Excel spreadsheet to upload. 
      string strFileName = Server.HtmlEncode(fupExcel.FileName); 
      strAct = "New Schedule is uploaded, File name:" + strFileName; 

      // Get the extension of the Excel spreadsheet. 
      string strExtension = Path.GetExtension(strFileName); 

      // Validate the file extension. 
      if (strExtension != ".xls" && strExtension != ".xlsx") 
      { 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "ntexl", "alert('Please select a Excel spreadsheet to import!');", true); 
       return; 
      } 

      // Generate the file name to save. 
      //string uploadFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension; 
      uploadFileName = CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time").ToString("yyyyMMddHHmmss") + strExtension; 

      // Save the Excel spreadsheet on server. 
      fupExcel.UploadFile(uploadPath, uploadFileName); 

      ClsScheduleFileNew objCLSSchedule = new ClsScheduleFileNew(); 
      ClsScheduleFileNewProp objUserFile = new ClsScheduleFileNewProp(); 

      objUserFile.FileName = strFileName; 
      objUserFile.FilePath = uploadFileName; 
      objUserFile.CreatedDate = CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time"); //DateTime.Now; 
      ScheduleFileId = objCLSSchedule.InsertFileInfo(objUserFile); 
      hdfScheduleFileId.Value = ScheduleFileId.ToString(); 
      if (ScheduleFileId != 0) 
      { 
       //ViewState["nw_Upload"] = ScheduleFileId; 
      } 
      else 
      { 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "err", "alert('Some error occured while exporting the file.');", true); 
       return; 
      } 
      // Generate the connection string for Excel file. 
      strExcelConn = uploadPath; 

      // There is no column name In a Excel spreadsheet. 
      // So we specify "HDR=YES" in the connection string to use 
      // the values in the first row as column names. 
      if (strExtension == ".xls") 
      { 
       // Excel 97-2003 
       strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelConn + uploadFileName + ";Extended Properties='Excel 8.0;HDR=YES;'"; 
      } 
      else 
      { 
       // Excel 2007 
       strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelConn + uploadFileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'"; 
      } 
      hdfStrExcelConn.Value = strExcelConn; 
      try 
      { 
       ImportData(uploadFileName, uploadPath, strExcelConn, ScheduleFileId); 
      } 
      catch (Exception) 
      { 
       //Delete file over here and database entry over here. 
       string path = uploadPath + uploadFileName; 
       CCMMUtility.DeleteFilefromLocation(path); 
       objCLSSchedule.DeleteScheduleFile(ScheduleFileId); 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "alert('Some column values are not matched.');", true); 
       return; 
      } 
     } 

Improt函数的代码是

DataTable dt = new DataTable(); 
     DataRow row; 
     DataTable dtExcel = CreateDataTable(); 

     using (OleDbConnection conn = new OleDbConnection(strConn)) 
     { 
      OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1]", conn); 
      da.Fill(dt);// here i am getting that error 

      // Create new datatable having non empty rows 
      DataTable filteredDataTable = (dt.Rows.Cast<DataRow>().Where(t => t.ItemArray.Any(field => !(field is DBNull)))).CopyToDataTable<DataRow>(); 

      // Fill the DataTable with data from the Excel spreadsheet. 
      if (filteredDataTable.Rows.Count > 0) 
      { 
// my code hete to fill datatable } 

回答

2

尝试使用:

"select * from [Sheet1$]" 

的$是必需的,我认为。

+0

我已经试过这种但没有有益 –

+0

我不知道是什么问题,但它再次被罚款的工作...我已经尝试了一些其他的方式来从Excel导入数据到数据表 HTTP://www.codeproject .COM /用品/ 32370 /进口Excel的文件到DataSet中 试过此链接,然后后,我又试了一次我的代码,那么它的工作对我很好.... –

0

写作,我已经尝试了解决方案。

商店excel表为逗号分隔文件,并将其保存为.txt

然后使用以下逻辑来同一导入到一个数据表。

using (StreamReader sr = new StreamReader("C://File.txt")) 
     { 
      String line; 
      int index = -1; 
      while ((line = sr.ReadLine()) != null) 
      { 
       index++; 
       iIndexofComma = line.IndexOf(@","); 
       iLength = line.Length; 
       dt.Rows.Add(); 
       dt.Rows[index]["col1"] = line.Substring(0, iIndexofComma); 
       dt.Rows[index]["col2"] = line.Substring(iIndexofComma + 1, iLength - iIndexofComma - 1); 
       dtMapping.AcceptChanges(); 
      } 
     } 

希望有所帮助。