------

[ AD ] Port Monitor ( Try to use a Best WebSite Monitoring Tool )

------

데이타그리드뷰에서 엑셀 저장 c샵 프로그래밍

 



 

 



 

 

 

Before you write code, you must add a reference to the Microsoft Excel object library.

코드를 작성하기전, 엠에스 엑셀 오브젝트 라이브러리를 참조 추가해야 한다.

Right click on your project and select Add Reference menu.

프로젝트에서 오른쪽 클릭하고 참조추가 메뉴를 선택한다. 

After that go to COM tab and select and add Microsoft Excel 12.0 object library.

컴 탭으로 가서 엠에스 엑셀 오브젝트 라이브러리를 추가한다.

 

 

 

 

private void button1_Click_1(object sender, EventArgs e)

        {

            // creating Excel Application

            Microsoft.Office.Interop.Excel._Application app  = new Microsoft.Office.Interop.Excel.Application();

            // creating new WorkBook within Excel application

            Microsoft.Office.Interop.Excel._Workbook workbook =  app.Workbooks.Add(Type.Missing);

           

            // creating new Excelsheet in workbook

             Microsoft.Office.Interop.Excel._Worksheet worksheet = null;                   

           

           // see the excel sheet behind the program

            app.Visible = true;

          

           // get the reference of first sheet. By default its name is Sheet1.

           // store its reference to worksheet

            worksheet = workbook.Sheets["Sheet1"];

            worksheet = workbook.ActiveSheet;

            // changing the name of active sheet

            worksheet.Name = "Exported from gridview";

           

            // storing header part in Excel

            for(int i=1;i<dataGridView1.Columns.Count+1;i++)

            {

    worksheet.Cells[1, i] = dataGridView1.Columns[i-1].HeaderText;

            }

            // storing Each row and column value to excel sheet

            for (int i=0; i < dataGridView1.Rows.Count-1 ; i++)

            {

                for(int j=0;j<dataGridView1.Columns.Count;j++)

                {

                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                }

            }

            // save the application

            workbook.SaveAs("c:\\output.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing);

           

            // Exit from the application

          app.Quit();
        }

'온라인게임' 카테고리의 다른 글

Boost MemoryPool  (0) 2011.04.05
cocos2d-x 2d 그래픽 엔진 / 모바일 / 아이폰 / c++ win32  (0) 2011.04.01
갤럭시탭 8.9  (0) 2011.03.22
Cross-thread operation not valid  (0) 2011.03.22
원격 데스크톱 서비스 관리자  (0) 2011.03.22

+ Recent posts