Posted
Filed under .NET/C#

C#에서 ListView로 쌓아둔 내용(?)을 ... Excel로 저장하기 위한 소스 코드입니다.
Excel로 저장하기 위해서 처리한 Method만 올려 놓았습니다.
(실행 파일 및 스크린 샷은 첨부 하지 않았습니다.)

private void btnSaveExcel_Click(object sender, EventArgs e)
        {
            if (btnOpen.Visible == false)
            {
                MessageBox.Show("저장전 Port를 Close하세요!");
            }
            else
            {
                //엑셀 저장하기
                saveFileDialog1.CreatePrompt = true;
                saveFileDialog1.OverwritePrompt = true;
                
                saveFileDialog1.DefaultExt = "*.xls";
                saveFileDialog1.Filter = "Excel Files (*.xls)|*.xls";
                saveFileDialog1.InitialDirectory = "C:\\";

                DialogResult result = saveFileDialog1.ShowDialog();

                if (result == DialogResult.OK)
                {
                    try
                    {
                        object missingType = Type.Missing;
                        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                        Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Add(missingType);
                        Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.Add(missingType, missingType, missingType, missingType);
                        excelApp.Visible = false;

                        for (int i = 0; i < lstView.Items.Count; i++)
                        {
                            for (int j = 0; j < lstView.Columns.Count; j++)
                            {
                                if (i == 0)
                                {
                                    excelWorksheet.Cells[1, j + 1] = this.lstView.Columns[j].Text;
                                }
                                excelWorksheet.Cells[i + 2, j + 1] = this.lstView.Items[i].SubItems[j].Text;
                            }
                        }
                        excelBook.SaveAs(@saveFileDialog1.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, missingType, missingType, missingType, missingType, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missingType, missingType, missingType, missingType, missingType);
                        excelApp.Visible = true;
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    }
                    catch
                    {
                        MessageBox.Show("Excel 파일 저장중 에러가 발생했습니다.");
                    }
                }
            }
        }
2010/07/11 00:01 2010/07/11 00:01
Posted
Filed under .NET/C#

어제 Visual Studio 2010 Professional 을... 잘 받아서 장장.. 30여분간의 설치를 이겨내고 시작해 본 Visual Studio 2010 .......
그 중에서도 C#언어..... 새로운 기능....

public int A(int x, int y = 5, int z = 6)
{
    int Result = 0;
    Result = x + y + z;

    return Result;
}

위와 같이.. int형을 반환 시켜주는 Method A를 만들었다...
그리고, 파라메터들을 보자.......,,, 무언가 이상하다...... int y=5, int z =6 이라고 적혀 있다....;;;( C++ 에서는 이렇게... 디폴트 파라메터라고 해서..... 사용할 수 있다..)

위와 같은 코드를 Visual Studio 2008 에서 컴파일 해보았다....
사용자 삽입 이미지
기본 매개변수 지정자를 사용할 수 없습니다.. 라고 친절하게 VS2008 에서는.. 나타나 주신다.

하지만, Visual Studio2010(이하 VS2010)에서는.. 위에와 같은 코드를 컴파일 할수 있다..
A(6); //(x:6)+(y:5)+(z:6) 17의 값이 Return
A(6,1); //(x:6)+(y:1)+z(:6) 13 의 값이 Return
A(1,3,2); //(x:1)+(y:3)+z(:2) 6의 값이 Return

2010/05/01 22:37 2010/05/01 22:37