将数据从DataGridView中导出成CSV格式文件, 完整代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Statistic.BaseClass
{
class DataExport
{
public static void ExportToCSV(DataGridView dgv, string fileName)
{
if (dgv.Rows.Count < 1)
{
MessageBox.Show("没有记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
sfDialog.FilterIndex = 0;
sfDialog.FileName = fileName;
if (sfDialog.ShowDialog() == DialogResult.OK)
{