1,项目展示,将datagrid中的数据导出
2,点击导出数据
3,代码展示 导出数据按钮事件如下
//导出
private void tsbtnUpload_Click(object sender, EventArgs e)
{
List<GoldPlate> list = dgvGoldList.DataSource as List<GoldPlate>;
string fileName = "金板预警统计";
FormUtility.DataToExcel<GoldPlate>(list, dgvGoldList.Columns, fileName + ".xlsx", fileName, fileName, "导出金板预警明细数据");
}
4,导出数据的具体方法如下
public static void DataToExcel<T>(List<T> list, DataGridViewColumnCollection cols, string fileName, string sheetName, string msg, string title)
{
if (list != null && list.Count > 0)
{
string filePath = "";
FolderBrowserDialog fbdChoose = new FolderBrowserDialog();
if (fbdChoose.ShowDialog() == DialogResult.OK)
{
filePath = fbdChoose.SelectedPath;
}
if (string.IsNullOrEmpty(filePath))
{
MessageBox.Show("请选择导出的位置!");
return;
}
if (filePath.LastIndexOf('/') != filePath.Length - 1)
filePath += "/";
Dictionary<string, string> colsName = new Dictionary<string, string>();
foreach (DataGridViewColumn dc in cols)
{
colsName.Add(dc.Name, dc.HeaderText);
}
int count = ExcelHelper.ListToExcel<T>(list, filePath + fileName, sheetName, colsName);
if (count > 0)
MessageBox.Show(title, msg + "数据导出成功!");
else
{
MessageBox.Show(msg + "数据导出失败!");
}
}
else
{
MessageBox.Show("没有数据可导出!");
return;
}
}
以上内容就是数据导出功能,下次更新DevExpress数据导出功能下图就是dev数据导出界面比较有动感。