zoukankan      html  css  js  c++  java
  • csharp: datagridview Convert csv file

     /// <summary>
            /// 保存文件
            /// 涂聚文
            /// 2014-08-29
            /// Geovin Du
            /// </summary>
            /// <param name="dGV"></param>
            /// <param name="filename"></param>
            public static void DataGridViewToCsV(DataGridView dGV, string filename)
            {
    
                string stOutput = "";
                // Export titles:  
                string sHeaders = "";
                for (int j = 0; j < dGV.Columns.Count; j++)
    
                    sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + ", ";
                stOutput += sHeaders + "
    ";
                // Export data.  
                for (int i = 0; i < dGV.RowCount - 1; i++)
                {
    
                    string stLine = "";
                    for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                        stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + ", ";
                    stOutput += stLine + "
    ";
    
                }
                UTF8Encoding utf8 = new UTF8Encoding();
                string file = "1.csv";
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                saveFileDialog1.CreatePrompt = true;
                saveFileDialog1.OverwritePrompt = true;
                saveFileDialog1.Title = "Save text Files";
                //saveFileDialog1.CheckFileExists = true;
                //saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.DefaultExt = "csv";
                saveFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 1;
                saveFileDialog1.RestoreDirectory = true;
                System.IO.Stream fileStream;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //FileStream fs = new FileStream(filename, FileMode.Create);
                    file = saveFileDialog1.FileName;
                    fileStream = saveFileDialog1.OpenFile();
                    MemoryStream userInput = new MemoryStream();
                    byte[] output = Encoding.Default.GetBytes(stOutput);
                    //fileStream.Read(output, 0, (int)output.Length);
                    fileStream.Write(output, 0, output.Length);
                    fileStream.Close();
    
                } 
    
    
            }
            /// <summary>
            /// 保存文件
            /// 涂聚文
            /// 2014-08-28
            /// Geovin Du
            /// </summary>
            /// <param name="dGV"></param>
            /// <param name="filename"></param>
            public static void DataGridViewToCsVDu(DataGridView dGV, string filename)
            {
    
                string stOutput = "";
    
                // Export titles:  
                string sHeaders = "";
                for (int j = 0; j < dGV.Columns.Count; j++)
    
                    sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + ", ";
                stOutput += sHeaders + "
    ";
                // Export data.  
                for (int i = 0; i < dGV.RowCount - 1; i++)
                {
    
                    string stLine = "";
    
                    for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
    
                        stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + ", ";
    
                    stOutput += stLine + "
    ";
    
                }
                UTF8Encoding utf8 = new UTF8Encoding();
                //Encoding utf16 = Encoding.GetEncoding(1200);//unicode encoding
                byte[] output = Encoding.Default.GetBytes(stOutput);
                FileStream fs = new FileStream(filename, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs, Encoding.Default);
                bw.Write(output, 0, output.Length); //write the encoded file
                bw.Flush();
                bw.Close();
                fs.Close();
                
    
            }
    
  • 相关阅读:
    Web大规模高并发请求和抢购的解决方案
    常用的排序算法
    Kafka中的消息是否会丢失和重复消费(转)
    excel操作之poi-ooxml
    spring-boot-configuration-processor 是干啥用的
    递归和尾递归的区别和原理(转)
    kafka接口文档和kafka教程
    quartz (从原理到应用)详解篇(转)
    Elastic-Job开发指南(转)
    SimpleDateFormat线程不安全及解决办法(转)
  • 原文地址:https://www.cnblogs.com/geovindu/p/3944488.html
Copyright © 2011-2022 走看看