DevExpress将GridView导出到Excel

Nej*_*the 9 c# excel devexpress

我真的需要这方面的帮助..我在互联网上找不到任何例子我使用DevExpress GridView我需要将它发送到excel并且我遇到问题循环到每个单元格和列,因为DevExpress包含不同的方法然后的DataGridView

那是我想写的代码..我真的感谢你的帮助

    public class Form1 : System.Windows.Forms.Form
    {
    Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

    string FirstName = "First Name";
    string FatherName = "Father Name";
    string LastName = "Last Name";
    }
    public Form1()
    {
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        ExcelApp.Columns.ColumnWidth = 20;
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
    }
    private void simpleButton1_Click(object sender, System.EventArgs e)
    {
        try
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\Emp.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\"";

            con.Open();
            DataTable dtSchema;
            dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", con);
            OleDbDataAdapter da = new OleDbDataAdapter(Command);
            DataSet ds = new DataSet ();
            da.Fill(ds);
            dataGrid1.DataSource = ds.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    private void ExportBtn_Click(object sender, System.EventArgs e)
    {
        for (int i = 1; i < gridView3.Columns.Count + 1; i++)
        {
            //ExcelApp.Cells[1, i] = gridView3.Columns[i].HeaderStyleName;

        }
        for (int i = 0; i< gridView3.RowCount - 1; i++)
        {
            for (int j = 0; j < gridView3.Columns.Count; j++)
            {
                ExcelApp.Cells[i + 2, j + 1] = gridView3.Columns[j].ToString();
            }
        }
        ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\pc\\Emp.xlsx");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是在导出按钮中单击事件..没有Row()这样的东西

Nir*_*ngh 21

要了解XtraGrid的各种导出方法,请浏览导出方法和设置

使用GridControl.ExportToXls(String)方法

示例代码段:

private void mnuExportTable_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    using (SaveFileDialog saveDialog = new SaveFileDialog())
    {
        saveDialog.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html";
        if (saveDialog.ShowDialog() != DialogResult.Cancel)
        {
            string exportFilePath = saveDialog.FileName;
            string fileExtenstion = new FileInfo(exportFilePath).Extension;

            switch (fileExtenstion)
            {
                case ".xls":
                    gridControl.ExportToXls(exportFilePath);
                    break;
                case ".xlsx":
                    gridControl.ExportToXlsx(exportFilePath);
                    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 (File.Exists(exportFilePath))
            {
               try
               {
                   //Try to open the file and let windows decide how to open it.
                   System.Diagnostics.Process.Start(exportFilePath);
                }
                catch
                {
                    String msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
                    MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
             }
             else
             {
                 String msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
                 MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

参考:将
多个XtraGrid控件导出到单个Excel文件