devexpress 数据导入(gridcontrol 导出 csv)

// 1.gridcontrol 导出 csv:

              DataTable dtbNew = new DataTable();

            dtbNew.Columns.Add("Checkbox", Convert.ToDecimal(1).GetType());

            dtbNew.Columns.Add("Seq");

            dtbNew.Columns.Add("Test");

            dtbNew.Columns.Add("Test2");

            dtbNew.Columns.Add("Test3");

            dtbNew.Columns.Add("Test4");

            dtbNew.Columns.Add("Test5");

            dtbNew.Columns.Add("Test6");

            dtbNew.Columns.Add("Test7");

            dtbNew.Columns.Add("Test8");

            dtbNew.Columns.Add("Test9");

            dtbNew.Columns.Add("Test10");



            for (int intCount = 0; intCount < 5000; intCount++)

            {

                DataRow drwRow = dtbNew.NewRow();

                drwRow[0] = Convert.ToDecimal(1);

                drwRow[1] = intCount;

                drwRow[2] = "Test " + intCount;

                drwRow[3] = "this is a sample long text aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

                drwRow[4] = "this is a sample long text bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

                drwRow[5] = "this is a sample long text ccccccccccccccccccccccccccccccccccccccccccc";

                drwRow[6] = "this is a sample long text ddddddddddddddddddddddddddddddddddddddddddddd";

                drwRow[7] = "this is a sample long text eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";

                drwRow[8] = "this is a sample long text fffffffffffffffffffffffffff";

                drwRow[9] = "this is a sample long text gggggggggggggggggggggggggggggggggggggggggggggggggg";

                drwRow[10] = "this is a sample long text hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";

                drwRow[11] = "this is a sample long text iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";

                dtbNew.Rows.Add(drwRow);

            }



            customGridControl1.SetDataSourceWithCheckbox(dtbNew);



            SaveFileDialog sfdSaveAs = new SaveFileDialog();

            sfdSaveAs.AddExtension = true;

            sfdSaveAs.Filter = "CSV (Comma delimiter) (*.csv)|*.csv";

            sfdSaveAs.Title = "Save As CSV File";

            sfdSaveAs.FileName = "";

            sfdSaveAs.InitialDirectory = "C:\\";

            if (sfdSaveAs.ShowDialog() == System.Windows.Forms.DialogResult.OK)

            {

                string strFilename = sfdSaveAs.FileName;



                //Option1

                //PrintableComponentLink l = new PrintableComponentLink(new PrintingSystem());

                //l.Component = customGridControl1;

                //l.CreateDocument();

                //l.PrintingSystem.ExportOptions.Csv.Separator = ",";

                //l.PrintingSystem.ExportToCsv(strFilename);



                //Option 2

                DevExpress.XtraGrid.Export.BaseExportLink l_gvLink;

                DevExpress.XtraExport.ExportTxtProvider l_provider;

                l_provider = new DevExpress.XtraExport.ExportTxtProvider(strFilename);

                l_provider.Separator = ",";

                l_provider.AlignColumnWidth = false;

                l_gvLink = customGridControl1.Grid.CreateExportLink(l_provider);

                l_gvLink.ExportAll = true;

                l_gvLink.ExportTo(true);

            }

        }

 

 

//2. 网上找的代码 有待测试。

void Export( DataTable table, string fileName)

{

    DevExpress.XtraExport.ExportXlsProvider provider = new DevExpress.XtraExport.ExportXlsProvider(fileName);

    // First row : column names

    for( int i=0; i<table.Columns.Count; i++ )

    {

        provider.SetCellString( i, 0, table.Columns[i].ColumnName); // Invalid Cell Dimension EXCEPTION

    }



    // rows : data

    for( int i=0; i<table.Rows.Count; i++ )

    {

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

            {

            provider.SetCellData( j, i+1, table.Rows[i][j] );

            }

    }

}

你可能感兴趣的:(DevExpress)