개발(IT)/DevExpress(WinForm)

[GridView] GridView 파일저장 - 엑셀(xlsx)

isony 2023. 10. 18. 20:51
반응형

1. GridView를 파일저장하기

- 여러 파일 저장 기능을 지원합니다.

- 기본지원 파일(xls, xlsx, rtf, pdf, html)

saveDialog.Filter = "Excel (.xls)|*.xls|Excel (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html";

 

- 버튼을 클릭을 했을때 처리 루틴을 만들어 보았습니다.

- 파일 저장후 열어 보는 루틴을 선택해서 볼수 있도록 수정해 보았습니다.

 

<아래 소스파일을 참조>

private void btnSave_Click(object sender, EventArgs e)
{
    if (dgViewMaster.RowCount <= 0) { return; }
    if (dgViewMaster.SelectedRowsCount == 0) { return; }

    // Master GridView 선택한 Row의 데이터를 가져온다.
    DataRow row = dgViewMaster.GetFocusedDataRow();

    string path = "전송자료_" + row["order_no"].ToString() + "_" + DateTime.Now.ToString("yyyyMMdd"); ;

    using (SaveFileDialog saveFile = new SaveFileDialog())
    {
        saveFile.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html";
        saveFile.FileName = path;

        if (saveFile.ShowDialog() != DialogResult.Cancel)
        {
            string filePath = saveFile.FileName;
            string fileExt = new System.IO.FileInfo(filePath).Extension;

            switch (fileExt)
            {
                case ".xls":
                    //Customize export options
                    XlsExportOptionsEx xlsOptions = new XlsExportOptionsEx();
                    xlsOptions.AllowGrouping = DefaultBoolean.False;
                    xlsOptions.ShowTotalSummaries = DefaultBoolean.False;
                    xlsOptions.SheetName = "Exported Data";

                    gridControl.ExportToXls(filePath, xlsOptions);
                    break;

                case ".xlsx":
                    //Customize export options
                    XlsxExportOptionsEx xlsxOptions = new XlsxExportOptionsEx();
                    xlsxOptions.AllowGrouping = DefaultBoolean.False;
                    xlsxOptions.ShowTotalSummaries = DefaultBoolean.False;
                    xlsxOptions.SheetName = "Exported Data";

                    gridControl.ExportToXlsx(filePath, xlsxOptions);
                    break;

                case ".rtf":
                    gridControl.ExportToRtf(exportFilePath);
                    break;
                    
                case ".pdf":
                    gridControl.ExportToPdf(exportFilePath);
                    break;
                    
                case ".html":
                    gridControl.ExportToHtml(exportFilePath);
                    break;
                    
                case ".mht":
                    gridControl.ExportToMht(exportFilePath);
                    break;
                    
                default:
                    break;
            }

            if (System.IO.File.Exists(filePath))
            {
                try
                {
                    //Try to open the file and let windows decide how to open it.
                    if (MessageBox.Show("저장된 파일을 열어보시겠습니까?", "데이터 생성", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Process.Start(filePath);
                    }
                }
                catch
                {
                    String msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + filePath;
                    MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                String msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + filePath;
                MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}

<참조 링크 사이트>

https://docs.devexpress.com/WindowsForms/17733/controls-and-libraries/data-grid/export-and-printing/export-to-xls-and-xlsx-formats

 

Export to XLS, XLSX, CSV Formats | WinForms Controls | DevExpress Documentation

The Grid Control supports two modes (engines) to export data from Grid Views and Banded Grid Views in XLS, XLSX, and CSV formats: Data-Aware Export The new export engine features improved performance and memory usage. Choose this mode if you need to proces

docs.devexpress.com

 

반응형