zoukankan      html  css  js  c++  java
  • C# 一些代码小结--datGirdView 保存到csv文件

     if (dataGridView1.Rows.Count == 0)
     {
         MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
    else
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "CSV files (*.csv)|*.csv";
        saveFileDialog.FilterIndex = 0;
        saveFileDialog.RestoreDirectory = true;
        saveFileDialog.CreatePrompt = true;
        saveFileDialog.FileName = null;
        saveFileDialog.Title = "Save path of the file to be exported";
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            Stream myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string strLine = "";
            try
            {
                //Write in the headers of the columns.
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i > 0)
                        strLine += ",";
                    strLine += dataGridView1.Columns[i].HeaderText;
                }
                strLine.Remove(strLine.Length - 1);
                sw.WriteLine(strLine);
                strLine = "";
                //Write in the content of the columns.
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    strLine = "";
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {
                        if (k > 0)
                            strLine += ",";
                        if (dataGridView1.Rows[j].Cells[k].Value == null)
                            strLine += "";
                        else
                        {
                            string m = dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            strLine += m.Replace(",", ",");
                        }
                    }
                    strLine.Remove(strLine.Length - 1);
                    sw.WriteLine(strLine);
                    //Update the Progess Bar.
                    // toolStripProgressBar1.Value = 100 * (j + 1) / dataGridView1.Rows.Count;
                }
                sw.Close();
                myStream.Close();
                MessageBox.Show("Data has been exported to:" + saveFileDialog.FileName.ToString(), "Exporting Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // toolStripProgressBar1.Value = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exporting Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
    
  • 相关阅读:
    【转】Android中自动连接到指定SSID的Wi-Fi
    【转】多文件目录下makefile文件递归执行编译所有c文件
    Android NDK R9d 安装
    【转】第一个MiniGUI程序:模仿QQ界面
    FFmpeg缩放swscale详解 <转>
    【转】基于Qt, TUIO和TSLIB的嵌入式Linux下的多点触摸设计
    【转】TI-Davinci开发系列之六CCS5.2调试Linux内核
    【转】ffserver用法小结
    【转】Android HAL实例解析
    【转】DM8168图像处理Link
  • 原文地址:https://www.cnblogs.com/memorypro/p/10589003.html
Copyright © 2011-2022 走看看