CSV从DataGridView输出的代码

2015-04-04 21:16:37  访问(1825) 赞(0) 踩(0)


        /// <summary>
        /// 
        /// </summary>
        protected void CSVOutPut()
        {
 
            RefleshGridCtrlStatus();

            DataGridView gv = dataGridView_Main;

            int checkCount = DataGridViewCountCheckedRows();

            bool isNotAll = true;

            if (checkCount == 0)
            {
                if (MessageBox.Show("您尚未勾选要任何的记录,是否选中和导出当前列表的全部记录?", "请选择", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;

                isNotAll = false;
            }
            else
            {
                if (MessageBox.Show("您确定导出选中的记录?", "请选择", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
            }

            SaveFileDialog fd = new SaveFileDialog();
            fd.DefaultExt = ".csv";

            if (fd.ShowDialog() != DialogResult.OK)
                return;

            string fileName = fd.FileName;

            CommonUtil cu = CommonUtil.instance;

            DataTable dt = gv.DataSource as DataTable;
            DataRow dr = null;
            int iCount = dt.Rows.Count;

            object oValue = null;
            DateTime dateTimeValue = DateTime.MinValue;
            string dataColumnCheckBoxName = Column__CheckBox.Name;
            List<List<string>> theList = new List<List<string>>();
            List<string>  sonList = new List<string>();
            foreach (DataColumn dc in dt.Columns)
            {
                sonList.Add(dc.ColumnName);
            }
            theList.Add(sonList);

            for (int i = 0; i < iCount; ++i)
            {
                if (isNotAll)
                {
                    oValue = gv.Rows[i].Cells[dataColumnCheckBoxName].Value;

                    if (oValue == null)
                        continue;

                    if (oValue.ToString() != "1")
                        continue;
                }

                sonList = new List<string>();
                dr = dt.Rows[i];

                foreach (DataColumn dc in dt.Columns)
                {
                    oValue = dr[dc.ColumnName];

                    if (oValue == null || oValue == DBNull.Value)
                    {
                        sonList.Add("");
                        continue;
                    }

                    if (dc.DataType == typeof(DateTime))
                    {
                        dateTimeValue = Convert.ToDateTime(oValue);

                        if (dateTimeValue == dateTimeValue.Date)
                        {
                            sonList.Add(dateTimeValue.ToString("yyyy-MM-dd"));
                        }
                        else
                        {
                            sonList.Add(dateTimeValue.ToString("yyyy-MM-dd HH:mm:ss"));
                        }

                    }
                    else
                    {
                        sonList.Add(oValue.ToString());
                    }
                }

                theList.Add(sonList);

            }

            
            FileInfo theInfo = new FileInfo(fileName);


            cu.CSVExportTo(theInfo.FullName, theList);

            MessageBoxShowYesNoCancel(theInfo.FullName, theInfo.Directory.FullName);

        }

        /// <summary>
        /// 保存成功后的提示操作
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="dirName"></param>
        public void MessageBoxShowYesNoCancel(string fileName, string dirName)
        {
            DialogResult dg = MessageBox.Show(
                       "保存成功,是否复制文件或打开目录(点No)?", "选择操作", MessageBoxButtons.YesNoCancel);

            if (dg == DialogResult.Cancel)
                return;

            if (dg == DialogResult.No)
            {
                System.Diagnostics.Process.Start("explorer.exe", dirName);

                return;
            }

            if (dg == DialogResult.Yes)
            {

                System.Collections.Specialized.StringCollection stringCollectionValue
                    = new System.Collections.Specialized.StringCollection();

                stringCollectionValue.Add(fileName);

                Clipboard.SetFileDropList(stringCollectionValue);
            }
        }


标签:CSV从DataGridView输出的代码 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)