Excelシートからデータテーブルにデータを挿入したいのですが、これはうまくいきました。しかし、なぜ今次のエラーが発生するのかわかりません
「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 }