我想從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 }
我已經試過這種但沒有有益 –
我不知道是什麼問題,但它再次被罰款的工作...我已經嘗試了一些其他的方式來從Excel導入數據到數據表 HTTP://www.codeproject .COM /用品/ 32370 /進口Excel的文件到DataSet中 試過此鏈接,然後後,我又試了一次我的代碼,那麼它的工作對我很好.... –