Microsoft.Office.interop.excel DLL을 사용하여 프로그래밍 방식으로 열어서 Excel을 작성하는 방법?

StackOverflow https://stackoverflow.com/questions/1648125

  •  22-07-2019
  •  | 
  •  

문제

Microsoft.office.interop.excel DLL을 사용하여 데이터를 Excel 시트에 작성하고 싶습니다. 나는 코드를 가지고있다:

if (System.IO.File.Exists(strFileName))
{
    System.IO.File.SetAttributes(strFileName, FileAttributes.Normal);
    System.IO.File.Delete(strFileName);
}

// Open an instance of excel. Create a new workbook.
// A workbook by default has three sheets, so if you just want 
//a single one, delete sheet 2 and 3

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel._Workbook xlWB = (Excel._Workbook)xlApp.Workbooks.Add(Missing.Value);

Excel._Worksheet xlSheet = (Excel._Worksheet)xlWB.Sheets[1];
((Excel._Worksheet)xlWB.Sheets[2]).Delete();
((Excel._Worksheet)xlWB.Sheets[2]).Delete();

xlSheet.Name = strSheetName;

// Write a value into A1
xlSheet.Cells[2, 1] = "Tags";
xlSheet.Cells[2, 2] = "Leak Test";
xlSheet.Cells[2, 3] = "FIR";
xlSheet.Cells[2, 4] = "SOP";

xlWB.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, 
Missing.Value,Missing.Value, 
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, 
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlApp.Quit();

// Release the COM object, set the Excel variables to Null, and tell the 
//Garbage Collector to do its thing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

xlSheet = null;
xlWB = null;
xlApp = null;

이제 경로에서 기존 Excel 파일을 열고 일부 데이터를 우리가 명시한 Excel 시트에 넣고 프로그래밍 방식으로 특정 경로에 저장됩니다.

기존 Excel 파일을 열 수 있고 다른 이름으로 추가 및 저장할 수있는 경우 소스에 답장하십시오.

그 안부, girish

도움이 되었습니까?

해결책

여기를 참조하십시오 정보.

다른 팁

ExcelPackage는 더 이상 유지 관리되지 않습니다. 몇 가지 버그가있는 것 같습니다. 거기에서 의견을 읽었습니다 http://epplus.codeplex.com/

ExcelPackage를 기반으로하고 IT 라이센스 (GPL)를 상속하므로 요구 사항에 맞지 않을 수 있습니다.

2007 버전의 Excel을 사용하는 경우 사용하는 것이 좋습니다. ExcelPackage 대신 - OpenXML 표준의 구현이며 COM Interop보다 훨씬 빠르고 훨씬 덜 지저분하며 웹 서버에 Excel (Office)이 설치되지 않은 시스템에서 실행할 수 있습니다.

적극 권장하지만 Excel 2007 이상으로 제한됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top