C#: Excel 통합 문서를 어떻게 열고 닫을 수 있습니까?
-
20-09-2019 - |
문제
단순히 Excel 통합 문서를 열고 닫는 방법을 아는 사람이 있습니까?
파일의 데이터를 읽을 필요가 없으며 열면 닫아야합니다. (*)
Microsoft.Office.interop.excel 어셈블리를 참조해야한다고 생각합니다.
*이유 : 이미 제 3 자 라이브러리 (aspose)로 피벗 테이블 정보를 구성했습니다. 이제 생성 된 피벗 테이블을 읽어야합니다.
불행히도, Aspose 라이브러리는 런타임에 피벗 테이블을 생성 할 수 없습니다. 누군가가 필요합니다 Excel로 파일을 엽니 다 따라서 Excel이 피벗 테이블 값을 생성 할 수 있습니다.
해결책
Microsoft.office.interop.excel을 참조한 후 마침내 정리하십시오.
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;
try
{
_Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
WB = _Excel.Workbooks.Open("FILENAME",
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);
//do something
}
catch (Exception ex)
{
WB.Close(false, Type.Missing, Type.Missing);
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);
}
다른 팁
빠른 Google은 코드 프로젝트에서 다음을 제공합니다.
System.Diagnostics.Process를 사용하여 Excel을 시작하고 모니터링하고 닫으십시오. 이 사이트는 좋은 소개를 제공합니다. http://www.thescarms.com/dotnet/process.aspx, 보이지 않는 창으로 달리기 및 입력을 보내는 것을 포함하여.
제휴하지 않습니다 StackOverflow