【C#】datagridview分段保存为几个CSV文件

来源:互联网 发布:移动4g 网络 编辑:程序博客网 时间:2024/06/05 02:09

在实际应用中,在导出文件时,会遇到导出的文件过大,导致导出速度慢,导出数据不全的问题,影响软件正常使用。本文介绍一种将数据按照设定的数值分成几部分进行保存,保证导出数据的完整性。

直接帖程序;

        /// <summary>        /// 将datagridview的数据分成几个CSV文件进行保存。        /// </summary>        /// <param name="dataGridView">要导出的DataGridView;要导出的CSV文件的个数,日志的总行数</param>        /// <returns></returns>        public bool FenDuanDataGridViewToCSV(DataGridView dataGridView,int geshu,int rowscount)        {            SaveFileDialog saveFileDialog = new SaveFileDialog();            saveFileDialog.Filter = "CSV files (*.csv)|*.csv";            saveFileDialog.FilterIndex = 0;            saveFileDialog.RestoreDirectory = true;            saveFileDialog.CreatePrompt = true;            saveFileDialog.FileName = null;            saveFileDialog.Title = "保存";            if (saveFileDialog.ShowDialog() == DialogResult.OK)            {                string savepath = saveFileDialog.FileName.ToString();//获取文件路径                string filepath = savepath.Substring(0, savepath.LastIndexOf("\\"));//获取文件路径,不带文件名                string filenameext = savepath.Substring(savepath.LastIndexOf("\\") + 1);//获取文件名(带后缀),不带路径                string filename = filenameext.Substring(0, filenameext.LastIndexOf("."));//获取文件名,不带后缀                string nowpath = "";//定义分段的保存路径                for (int i = 0; i < geshu;i++ )                {                    if (i < 1)//判断是否为0,若为0,则直接按照指定的路径保存,不为0时,按照自定义的路径保存                    {                        nowpath=savepath;                    }                    else                    {                         System.IO.Directory.CreateDirectory(filepath);//先创建文件夹路径                          nowpath = filepath +"\\"+filename+ Convert.ToString(i) + ".csv";//创建分段的保存路径                         System.IO.File.Create(nowpath).Close();//不为0时,要先创建此路径,.close()同时解决此文件被另一线程占用的问题                    }                    StreamWriter sw = new StreamWriter(nowpath, false, System.Text.Encoding.GetEncoding(-0));//false为覆盖。true为接着写。                        string strLine = "";                        try                        {                            Thread thdSub = new Thread(new ThreadStart(showmsss));                            thdSub.Start();                            for (int c = 0; c < dataGridView.ColumnCount; c++)                            {                                if (c > 0)                                    strLine += ",";                                strLine += dataGridView.Columns[c].HeaderText;                            }                            strLine.Remove(strLine.Length - 1);                            sw.WriteLine(strLine);                            strLine = "";                            for (int j = (i * x); j < (i + 1) * x; j++)                            {                               //判断是否也保存完整,若已保存完整,则跳出循环,=====                                //if(dataGridView.Rows[j].Cells[1].Value.ToString()=="NULL")//此行第一列是否没有数据                                //{                                //    break;                                //}                                if (i == geshu - 1){if (j == rowscount){ break; } }                                strLine = "";                                int colCount = dataGridView.Columns.Count;                                for (int k = 0; k < colCount; k++)                                {                                    if (k > 0 && k < colCount)                                        strLine += ",";                                    if (dataGridView.Rows[j].Cells[k].Value == null)                                        strLine += "";                                    else                                    {                                        string cell = dataGridView.Rows[j].Cells[k].Value.ToString().Trim();                                        cell = cell.Replace("\"", "\"\"");                                        cell = "\"" + cell + "\"";                                        strLine += cell;                                    }                                }                                sw.WriteLine(strLine);                            }                            sw.Close();                            MessageBox.Show("部分数据被导出到:" + nowpath, "导出完毕", MessageBoxButtons.OK, MessageBoxIcon.Information);                            thdSub.Abort();                        }                        catch (Exception ex)                        {                            MessageBox.Show(ex.Message, "导出错误", MessageBoxButtons.OK, MessageBoxIcon.Information);                            return false;                        }                        nowpath = "";//将保存路径清空,方便下次赋值。                    }            }            return true;        }

使用此方法,即只弹出一次保存文件对话框,分段保存的路径在保存文件对话框选定的基础上加1,几个输入获取如下:

int rowcount = dataGridView_ATP.Rows.Count-1;//获取查询到的日志的列数

 int geshu;//设置分段导出的CSV文件的个数,初始化为0;
geshu = (rowcount / x) + 1;//获得要导出成为的CSV文件的个数;

int x自己设置它的值。



参考的资料:http://www.cnblogs.com/tianguook/p/3277258.html

https://zhidao.baidu.com/question/306817051847789284.html

解决问题“文件xxx正由另一进程使用,因此该进程无法访问此文件”

创建一个文件路径的方法:https://zhidao.baidu.com/question/92028854.html

streamwriter的使用方法。

1 0