DevExpress.XtraExport.ExportXlsProvider实现Excel带入数据

View Code
 1     private void btnImportExcel_Click(object sender, EventArgs e)
 2         {
 3             if (this.gridView1.RowCount <= 0)
 4             {
 5                 MessageBox.Show("没有数据导入!", "导入信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
 6             }
 7             SaveFileDialog saveExcel = new SaveFileDialog();
 8             saveExcel.Filter = "Excel文件(.xlsx)|*.xls";
 9             saveExcel.FilterIndex = 1;
10             saveExcel.RestoreDirectory = true;
11             string newFileName = string.Empty;
12             if (saveExcel.ShowDialog() == DialogResult.OK)
13             {
14                 string localFilePath = saveExcel.FileName.ToString();
15                 string localfileNameExt = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
16                 newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
17                 newFileName = DateTime.Now.ToString("yyyy-MM-dd") + "_" + newFileName;
18                 ExportToExcel(localfileNameExt + "\\" + newFileName);
19             }
20         }

 

View Code
 1   public void ExportToExcel(string fileName)
 2         {
 3             //this.gridControl1.ExportToXlsx(fileName);   
 4             try
 5             {
 6                 if (!string.IsNullOrEmpty(fileName))
 7                 {
 8                     ExportTo(gridView1, new DevExpress.XtraExport.ExportXlsProvider(fileName));
 9                 }
10             }
11             catch (Exception ex)
12             {
13                 MessageBox.Show(ex.Message.ToString());
14             }
15         }
View Code
 1   public void ExportTo(DevExpress.XtraGrid.Views.Base.BaseView bv, DevExpress.XtraExport.IExportProvider provider)
 2         {
 3             Cursor currentCursor = Cursor.Current;
 4             Cursor.Current = Cursors.WaitCursor;
 5 
 6             DevExpress.XtraGrid.Export.BaseExportLink link = bv.CreateExportLink(provider);
 7             link.ExportTo(true);
 8 
 9             Cursor.Current = currentCursor;
10         }
View Code
 1    private void btnSearch_Click(object sender, EventArgs e)
 2         {
 3             string Where = "";
 4             List<VAV_ACT_SOURCE> SourceList = EntityManager.GetListOfVavActSource(true, false, Where);
 5             if (SourceList != null && SourceList.Count > 0)
 6             {
 7                 gridControl1.DataSource = SourceList;
 8             }
 9             else
10             {
11                 VAV_ACT_SOURCE sourcInfo = new VAV_ACT_SOURCE();
12                 sourcInfo.CBEHAVIOR = "开机";
13                 sourcInfo.CBROADCAST_TIME = DateTime.Now;
14                 sourcInfo.CCASN = "20120503";
15                 sourcInfo.CCH_ID = 2012050301;
16                 sourcInfo.CEND_TIME = DateTime.Now;
17                 sourcInfo.CFILENAME = @"c:\Documents and Settings";
18                 sourcInfo.CMEMO = "其他说明信息";
19                 sourcInfo.CMSO_ID = 601;
20                 sourcInfo.COPERAERT = "收视";
21                 sourcInfo.CPRE_CCH_ID = 201;
22                 sourcInfo.CSCID = "2012050301";
23                 sourcInfo.CSOURCE = "源数据";
24                 sourcInfo.CSTAMP = DateTime.Now;
25                 sourcInfo.CSTART_TIME = DateTime.Now;
26                 sourcInfo.CTYPE = 0;
27                 SourceList.Add(sourcInfo);
28 
29                 gridControl1.DataSource = SourceList;
30             }
31         }

 

posted @ 2012-05-03 18:46  石 磊  阅读(3726)  评论(0编辑  收藏  举报