내용 보기

작성자

관리자 (IP : 172.17.0.1)

날짜

2020-07-10 04:55

제목

[C#] 엑셀 파일 출력하기 (엑셀 열지 않고)


using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;     

 

            Excel.Application xlApp;
            Excel.Workbooks xlWorkBooks;
            Excel.Workbook xlWorkBook;
            Excel.Sheets xlSheet;
            Excel.Worksheet xlWorkSheet;

 

            xlApp = new Excel.Application();
            xlWorkBooks= xlApp.Workbooks;

 

            String filePath = @"C:\excle_Test.xlsx";//열파일 지정


            xlWorkBook= xlApp.Workbooks.Open(filePath, 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);

 

            xlSheet = xlWorkBook.Worksheets;
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;

 

            xlWorkSheet.Cells[1, 1] = "셀에 값입력";
           

            xlRange.PrintOut(1, 1, 1, false, Type.Missing, false, false, Type.Missing);//엑셀 열지않고 출력;


            //xlApp.Visible = true; //엑셀 화면에 띄우기

 

 

            xlWorkBook.Close(false, null, null);
            releaseObject(xlWorkSheet);
            releaseObject(xlSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkBooks);
            releaseObject(xlApp);

//xlWorkBook.SaveAs(@"C:\excle_TestComplete.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);//다른이름으로 저장시

//xlWorkSheet.get_Range("A1", "I11").Borders.LineStyle = BorderStyle.FixedSingle;//지정영역 전셀 테두리 입히기 윈폼일때만 가능?


 

            //GC.Collect();
            //GC.WaitForPendingFinalizers();
            //GC.WaitForFullGCComplete();


        public static void releaseObject(object obj)//엑셀 메모리 해제
        {
            try
            {?
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                throw ex;
            }
        }

 

--------------------------------------------------------------------------------------------------------------------------

 

엑셀 메모리 생성 방식

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(Type.Missing);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


출처1

http://blog.naver.com/PostView.nhn?blogId=wzblue&logNo=10107209303

출처2