2
我有我使用的Excel文件導入服務,並實現如下所示:Excel導入到DataSet中打破當文件打開時
[Attributes.ImportFileExtension(Extension=".xls")]
[Attributes.ImportFileExtension(Extension=".xlt")]
[Attributes.ImportFileExtension(Extension=".xlsx")]
public class ExcelImportService:FileImportServiceBase,IFileImportService
{
public DataSet Import(System.IO.Stream fileStream)
{
IExcelDataReader excelReader = null;
switch (Extension)
{
case ".xls":
case ".xlt":
excelReader = ExcelReaderFactory.CreateBinaryReader(fileStream);
break;
case ".xlsx":
excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
break;
}
excelReader.IsFirstRowAsColumnNames = true;
DataSet dataSet = excelReader.AsDataSet();
return dataSet;
}
}
此外,我有以下的代碼調用此通過正在創建的工廠提供服務。用於調用該服務的代碼如下所示:
try
{
var extension = Path.GetExtension(dialog.FileName);
var importService = FileImportServiceFactory.Create(extension);
var stream = dialog.OpenFile();
var data = importService.Import(stream);
stream.Close();
stream.Dispose();
var result = new FileImportedMessage { Data = data };
result.Dispatch();
}
catch (Exception e)
{
MessageDispatcher.Dispatch(new ExceptionMessage(e));
}
進口的作品找到除非在被其導入的文件被打開。
我有兩個問題:
1)什麼是處理這種情況的最佳實踐/策略?
2)我該如何處理這個問題?