DevExpress-GridControl数据导出到EXCEL技术指导QQ:595789813

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

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

1 SaveFileDialog fileDialog = new SaveFileDialog();
2             fileDialog.Filter ="Excel文件¦*.xls;";
3             fileDialog.FileName =this.Text + DateTime.Now.ToString("yyMMddHHmm");
4             if(fileDialog.ShowDialog() == DialogResult.OK)
5             {
6                 gridControl1.ExportToXls(fileDialog.FileName);
7                 XtraMessageBox.Show("操作成功!","操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
8             }

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

01 #region 用流将数据导入到Excel中
02         publicstatic voidDataGridToExcel(DataTable dt, GridView dgv, stringorderDateTime, stringtitle)
03         {
04             SaveFileDialog saveFileDialog =new SaveFileDialog();
05             saveFileDialog.Filter ="Execl (*.xls)¦*.xls¦ Execl (*.xlsx)¦*.xlsx";
06             saveFileDialog.FilterIndex = 0;
07             saveFileDialog.RestoreDirectory =true;
08             saveFileDialog.CreatePrompt =true;
09             saveFileDialog.Title ="导出Excel文件到";
10   
11             DateTime now = SystemManage.GetTimeNow();
12             saveFileDialog.FileName = title + orderDateTime;
13   
14             DialogResult dr = saveFileDialog.ShowDialog();
15             if(dr == DialogResult.Cancel)
16                 return;
17             Stream myStream;
18   
19             myStream = saveFileDialog.OpenFile();
20             stringfileName = saveFileDialog.FileName;
21             if(fileName == "")
22             {
23   
24                 XtraMessageBox.Show("请输入文件名!","操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
25                 return;
26             }
27             StreamWriter sw =new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
28             stringstr = "";
29             try
30             {
31                 //写标题
32                 stringstc = title + orderDateTime;
33   
34                 sw.WriteLine(stc);
35                 for(int i = 0; i <= dgv.Columns.Count; i++)
36                 {
37                     if(i == 0)
38                     {
39                         str +="序号";
40                     }
41                     else
42                     {
43                         if(dgv.Columns[i - 1].Visible == true)
44                         {
45                             str +="\t";
46                             str += dgv.Columns[i - 1].Caption;
47                         }
48   
49                     }
50                 }
51   
52                 sw.WriteLine(str);
53                 intcount = 0;
54                 //写内容
55                 for(int j = 0; j < dt.Rows.Count; j++)
56                 {
57                     count++;
58                     stringtempStr = "";
59                     for(int k = 0; k < dt.Columns.Count + 1; k++)
60                     {
61                         if(k == 0)
62                         {
63                             tempStr = count.ToString();
64                         }
65                         else
66                         {
67                             if(dgv.Columns[k - 1].Visible == true)
68                             {
69                                 tempStr +="\t";
70                                 tempStr += dt.Rows[j][k - 1].ToString().Trim();
71   
72                             }
73                         }
74   
75                     }
76                     sw.WriteLine(tempStr);
77   
78                 }
79                 sw.Close();
80                 sw.Dispose();
81                 myStream.Close();
82                 myStream.Dispose();
83   
84                 XtraMessageBox.Show("操作成功!","操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
85             }
86             catch(Exception e)
87             {
88   
89                 XtraMessageBox.Show("操作失败!","操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
90             }
91             finally
92             {
93                 sw.Close();
94                 myStream.Close();
95   
96             }
97         }
98         #endregion

OK,调用就简单了:

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

声明: 本文采用 BY-NC-SA 协议进行授权 | Carey's Blog

标签:C#,DevExpress,DevExpress控件 分类:.NET编程,Winform开发

你可能感兴趣的:(DevExpress-GridControl数据导出到EXCEL技术指导QQ:595789813)