Я пытаюсь загрузить файл Excel. Файл Excel находится в формате XML. То есть, когда я щелкаю правой кнопкой мыши и редактирую, он показывает форматирование XML. Расширение файла .xlsx. Я получаю ошибку «Внешняя таблица не в ожидаемом формате».
Я могу успешно загрузить нормальные файлы Excel.
Моя строка подключения - Excel10ConString = "Provider = Microsoft.ACE.OLEDB.12.0; Источник данных = {0}; Расширенные свойства =" Excel 12.0; HDR = Нет; IMEX = 1; ""
private static DataTable GetExcelFileData(ExcelReaderRequest excelFileRequest)
{
if (excelFileRequest == null)
{
throw new ArgumentNullException("excelFileRequest");
}
string CommandText = string.Empty;
OleDbDataAdapter da = null;
string strMappedPath = DocumentManagementContext.ComponentSettings.MappedPath;
string sourcePath = string.Concat(strMappedPath, excelFileRequest.FileRelativePath);
string connectionString = excelFileRequest.FileExtension == ".xlsx" ? DocumentManagementContext.ComponentSettings.Excel10ConString : DocumentManagementContext.ComponentSettings.Excel07ConString;
string recordCount = excelFileRequest.MaxRecordCount == 0 ? string.Empty : string.Format("Top {0} ", excelFileRequest.MaxRecordCount + 1);
connectionString = string.Format(connectionString, sourcePath);
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
if (excelFileRequest.FileExtension == ".xlsx" || excelFileRequest.FileExtension == ".xls")
{
///// Getting the excel sheet name
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtSheetName.Rows[0]["TABLE_NAME"].ToString();
///// CommandText for excel files
CommandText = string.Format(@"SELECT {0}* FROM [{1}]", recordCount, SheetName);
}
else
{
throw new ServiceException("Invalid file");
}
using (OleDbCommand cmd = new OleDbCommand(CommandText, conn))
{
cmd.CommandType = CommandType.Text;
da = new OleDbDataAdapter(cmd);
using (DataTable dtTemp = new DataTable())
{
///// Satisfies rule: SetLocaleForDataTypes.
dtTemp.Locale = System.Globalization.CultureInfo.InvariantCulture;
da.Fill(dtTemp);
if (dtTemp != null)
{
using (DataTable dtexcelMain = CreateDataSetFromExcel(dtTemp))
{
dtexcelMain.Rows.RemoveAt(0);
return dtexcelMain;
}
}
}
}
}
return null;
}
catch (Exception Ex)
{
throw new ServiceException("Error in GetExcelFileData message : " + Ex.Message, Ex, ExceptionType.OtherHandledException, HttpStatusCode.InternalServerError, true, "PT1000");
}
finally
{
if (da != null)
{
da.Dispose();
}
}
}