DevExpress- GridControl数据导出到EXCEL

将DevExpress-GridControl数据导出到EXCEL,目前用过两种,一为利用GridControl函数操作,一为用流(IO)操作.区别在于前者可以将你在GridControl所见格式全部导入EXCEL,比如有时候我们在Bands里面拖拽出来的Title样式,后者最大优点在于速度很快,亲测10W条数据导出只需2秒左右;

   

No.1:用GridControl函数导出数据到EXCEL,建议设置Options下OptionsPrint—AutoWidth=False,使导出后单元格宽度根据内容长度自动调整.

SaveFileDialog fileDialog = new SaveFileDialog();

fileDialog.Filter = "Excel文件¦*.xls;";

fileDialog.FileName = this.Text + DateTime.Now.ToString("yyMMddHHmm");

if (fileDialog.ShowDialog() == DialogResult.OK)

{

  gridControl1.ExportToXls(fileDialog.FileName);

  XtraMessageBox.Show("操作成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

}

   

   

No.2:用流将数据导入EXCEL

#region 用流将数据导入到Excel中

                public static void DataGridToExcel(DataTable dt, GridView dgv, string orderDateTime, string title)

                {

                        SaveFileDialog saveFileDialog = new SaveFileDialog();

                        saveFileDialog.Filter = "Execl (*.xls)¦*.xls¦ Execl (*.xlsx)¦*.xlsx";

                        saveFileDialog.FilterIndex = 0;

                        saveFileDialog.RestoreDirectory = true;

                        saveFileDialog.CreatePrompt = true;

                        saveFileDialog.Title = "导出Excel文件到";

    DateTime now = SystemManage.GetTimeNow();

  saveFileDialog.FileName = title + orderDateTime;

    DialogResult dr = saveFileDialog.ShowDialog();

                        if (dr == DialogResult.Cancel)

                                return;

                        Stream myStream;

    myStream = saveFileDialog.OpenFile();

                        string fileName = saveFileDialog.FileName;

                        if (fileName == "")

                        {

XtraMessageBox.Show("请输入文件名!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                                return;

                        }

                        StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));

                        string str = "";

                        try

                        {

                                //写标题

                                string stc = title + orderDateTime;

sw.WriteLine(stc);

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

                                {

                                        if (i == 0)

                                        {

                                                str += "序号";

                                        }

                                        else

                                        {

                                                if (dgv.Columns[i - 1].Visible == true)

                                                {

                                                        str += "\t";

                                                        str += dgv.Columns[i - 1].Caption;

                                                }

}

                                }

sw.WriteLine(str);

                                int count = 0;

                                //写内容

                                for (int j = 0; j < dt.Rows.Count; j++)

                                {

                                        count++;

                                        string tempStr = "";

                                        for (int k = 0; k < dt.Columns.Count + 1; k++)

                                        {

                                                if (k == 0)

                                                {

                                                        tempStr = count.ToString();

                                                }

                                                else

                                                {

                                                        if (dgv.Columns[k - 1].Visible == true)

                                                        {

                                                                tempStr += "\t";

                                                                tempStr += dt.Rows[j][k - 1].ToString().Trim();

}

                                                }

}

                                        sw.WriteLine(tempStr);

}

                                sw.Close();

                                sw.Dispose();

                                myStream.Close();

                                myStream.Dispose();

XtraMessageBox.Show("操作成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                        }

                        catch (Exception e)

                        {

XtraMessageBox.Show("操作失败!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                        }

                        finally

                        {

                                sw.Close();

                                myStream.Close();

}

                }

               

#endregion

   

OK,调用就简单了:

ClassName.DataGridToExcel(dt, gridView1, SystemManage.GetTimeNow().ToString("yyMMddHHmmss"), this.Text + "报表");

   

源文档 <http://www.liaoyong.name/?p=202>

posted @ 2015-05-21 10:08  AllenRobin  阅读(1515)  评论(0编辑  收藏  举报