데이터베이스 데이터에서 Excel 파일을 생성 할 수있는 Windows Form 응용 프로그램을 만들고 있습니다. 데이터베이스에 파일을 채우고 난 후 파일을 열고 매크로를 실행하여 파일을 저장 한 다음 닫습니다 (또는 내가 닫을 것으로 생각합니다). 이 파일은 내 HDD에 저장되지만 모든 파일이 생성되면 새 파일 세트를 생성 할 수 있어야하므로 새 세트를 생성하기 전에 내 HDD에서 모든 이전 파일을 삭제해야합니다. 응용 프로그램을 한 번 실행 한 후 새 파일 집합을 생성하려고하면 파일 중 하나 (첫 번째 배치에서 생성 된 마지막 파일)가 다른 프로세스에서 사용되었다는 오류가 발생합니다. 여기Excel 파일을 만들고 편집 한 후 릴리스
내 코드입니다 : 응용 프로그램이 파일을 해제하지 않기 때문에 내가 잘못하고있는 무슨
public void RemoveRows_Macro(string fileName)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(fileName);
//xlApp.Visible = true;
xlApp.DisplayAlerts = true;
//Run the macro
xlApp.Run("RemoveRows", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Save();
xlWorkBook.Close(false);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
Console.WriteLine("Error in releasing object :" + ex);
obj = null;
}
finally
{
GC.Collect();
}
}
?
미안하지만 잘못 입력했습니다! 나는 지금 그것을 시도하고있다. – Lahib
편집 내 대답 – TalentTuner
그것은 효과가있다. 도움에 감사드립니다. 감사합니다. – Lahib