我想读取excel文件并将数据插入数据集。我可以读取一些文件并且它可以正常工作但在某些情况下它无法正常工作。我也附加了错误和文件的屏幕截图。 当我们取消阻止该文件并打开它并保存它然后代码工作。 以下是我的代码:
try
{
startTime = DateTime.Now;
System.IO.FileStream fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite);
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
fs.Close();
fs.Dispose();
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}
答案 0 :(得分:0)
试试这个:
System.IO.FileStream fs;
try
{
startTime = DateTime.Now;
fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite);
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}
finally
{
if (fs != null){
try{
fs.Close();
fs.Dispose();
}
catch(Exception ex){
//Error handling for being unable to close file
}
}
}
听起来好像您要通过之前的执行运行将文件保持打开状态并且没有正确关闭它,这应该有希望解决您的问题。如果没有,请检查是否还有其他内容正在使用该文件
或者您可以使用using
功能,以便关闭文件使用,请参阅here
[编辑] using
函数的示例(您可能还需要在IExcelDataReader上使用using
,但我还没有检查过API):
try
{
using(System.IO.FileStream fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite))
{
startTime = DateTime.Now;
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}
[EDIT2]
问题是由NTFS系统设置一个表示它不安全的标志引起的,您需要使用以下代码并在访问该文件之前调用它以取消阻止它:
public class FileUnblocker {
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFile(string name );
public bool Unblock(string fileName) {
return DeleteFile(fileName+ ":Zone.Identifier");
}
}
所以你得到的代码是:
try
{
if (new FileUnblocker().Unblock(FullfilePath))
{
using(System.IO.FileStream fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite))
{
startTime = DateTime.Now;
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
}
else{
//Issue with unblocking, deal with it here
}
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}