none
dataGridview에 출력된 내용을 excel 파일로 저장 RRS feed

  • 질문

  • dataGridView에 있는 내용을 excel파일로 저장하고 싶은데요..

    dataGridView가 총 4개 있는데

    이 4개를 하나의 excel sheet로 저장하고 싶습니다.

    private void ExportExcel(bool captions)
            {
                this.saveFileDialog1.FileName = "TempName";
                this.saveFileDialog1.DefaultExt = "xls";
                this.saveFileDialog1.Filter = "Excel files (*.xls)|*.xls";
                this.saveFileDialog1.InitialDirectory = "{Desktop}";
                DialogResult result = saveFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    int num = 0;
                    object missingType = Type.Missing;
                    Excel.Application objApp;
                    Excel._Workbook objBook;
                    Excel.Workbooks objBooks;
                    Excel.Sheets objSheets;
                    Excel._Worksheet objSheet;
                    Excel.Range range;
                    string[] headers = new string[dataGridView1.ColumnCount];
                    string[] columns = new string[dataGridView1.ColumnCount];
                    for (int c = 0; c < dataGridView1.ColumnCount; c++)
                    {
                        headers[c] = dataGridView1.Rows[0].Cells[c].OwningColumn.HeaderText.ToString();
                        num = c + 65;
                        columns[c] = Convert.ToString((char)num);
                    }
                    try
                    {
                        objApp = new Excel.Application();
                        objBooks = objApp.Workbooks;
                        objBook = objBooks.Add(Missing.Value);
                        objSheets = objBook.Worksheets;
                        objSheet = (Excel._Worksheet)objSheets.get_Item(1);
                        if (captions)
                        {
                            for (int c = 0; c < dataGridView1.ColumnCount; c++)
                            {
                                range = objSheet.get_Range(columns[c] + "1", Missing.Value);
                                range.set_Value(Missing.Value, headers[c]);
                            }
                        }
                        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                        {
                            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                            {
                                range = objSheet.get_Range(columns[j] + Convert.ToString(i + 2),
                                Missing.Value);
                                range.set_Value(Missing.Value,
                                dataGridView1.Rows[i].Cells[j].Value.ToString());
                            }
                        }
                        objApp.Visible = false;
                        objApp.UserControl = false;
                        objBook.SaveAs(saveFileDialog1.FileName,
                        Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
                        missingType, missingType, missingType, missingType,
                        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                        missingType, missingType, missingType, missingType, missingType);
                        objBook.Close(false, missingType, missingType);
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Save Success!!!");
                    }
                    catch (Exception theException)
                    {
                        String errorMessage;
                        errorMessage = "Error: ";
                        errorMessage = String.Concat(errorMessage, theException.Message);
                        errorMessage = String.Concat(errorMessage, " Line: ");
                        errorMessage = String.Concat(errorMessage, theException.Source);
                        MessageBox.Show(errorMessage, "Error");
                    }
                }
            }

    현재 찾아본 코드인데.. Gridview 하나밖에 못 옮기는거 같고..

    또 이걸 버튼누를때 호출하려면 어떻게 코드를 작성해야 하나요?

    2013년 3월 12일 화요일 오전 1:10

답변