导出信息到Excel,如何将DevExpress的Gridcontrol导出到Excel 源码。
需要添加引用:System.Windows.Forms
如何将DevExpress的Gridcontrol导出到Excel 源码。
#region 导出已支付信息
1. private void btnDCYZF_Click(object sender, EventArgs e)
{
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.Title = "导出Excel";
fileDialog.Filter = "Excel文件(*.xls)|*.xls";
DialogResult dialogResult = fileDialog.ShowDialog(this);
if (dialogResult == DialogResult.OK)
{
DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
gridControl3.ExportToXls(fileDialog.FileName);
DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
2、 public override void ImportExcel()
{
if (XtraMessageBox.Show("确定要将本页面数据导入到Excel内?") == DialogResult.OK)
{
try
{
if (this.DataSourceTable == null || this.DataSourceTable.Rows.Count == 0)
{
XtraMessageBox.Show("没有数据要导出!");
return;
}
this.SaveFileDialog.Filter = "Excel文件(*.xls)|*.xls";
if (this.SaveFileDialog.ShowDialog(this) == DialogResult.OK)
{
BaseFormFactory.ProcessFactory.Show(this, "数据导出开始");
ExportTo(new DevExpress.XtraExport.ExportXlsProvider(this.SaveFileDialog.FileName));
}
}
catch (Exception vErr)
{
XtraMessageBox.Show("导出数据失败!错误源:" + vErr.Message);
}
finally
{
BaseFormFactory.ProcessFactory.Close();
XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void ExportTo(DevExpress.XtraExport.IExportProvider provider)
{
DevExpress.XtraGrid.Export.BaseExportLink link = this.gridData.gridView2.CreateExportLink(provider);
(link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
link.ExportTo(true);
provider.Dispose();
}
导出信息到Excel详解:
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- //打开的文件选择对话框上的标题
- saveFileDialog.Title = "请选择文件";
- //设置文件类型
- saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
- //设置默认文件类型显示顺序
- saveFileDialog.FilterIndex = 1;
- //保存对话框是否记忆上次打开的目录
- saveFileDialog.RestoreDirectory = true;
- //设置是否允许多选
- saveFileDialog.Multiselect = false;
- //按下确定选择的按钮
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //获取文件路径,不带文件名
- //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
- //获取文件名,带后缀名,不带路径
- string fileNameWithSuffix = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
- //去除文件后缀名
- string fileNameWithoutSuffix = fileNameWithSuffix.Substring(0, fileNameWithSuffix.LastIndexOf("."));
- //在文件名前加上时间
- string fileNameWithTime = DateTime.Now.ToString("yyyy-MM-dd ") + fileNameExt;
- //在文件名里加字符
- string newFileName = localFilePath.Insert(1, "Tets");
- }
fileDialog.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";