zoukankan      html  css  js  c++  java
  • DataTable 导出到TXT

       public static string  cExportTXT(DataView dv)
            {
                try
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    
    
                    saveFileDialog1.Filter = "txt(*.txt)|*.txt";
                    saveFileDialog1.FilterIndex = 0;
                    saveFileDialog1.RestoreDirectory = true;
                    saveFileDialog1.CreatePrompt = true;
                    saveFileDialog1.Title = "导出txt文件到 ";
    
                    DateTime now = DateTime.Now;
                  //  saveFileDialog1.FileName = now.Second.ToString().PadLeft(2, '0');
                    saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_AA";
    
                    //now.Year.ToString().PadLeft(2)+now.Month.ToString().PadLeft(2, '0 ') +now.Day.ToString().PadLeft(2, '0 ')+ "_ " +now.Hour.ToString().PadLeft(2, '0 ') +now.Minute.ToString().PadLeft(2, '0 ') +
                    saveFileDialog1.ShowDialog();
                    if (saveFileDialog1.FileName.Length==2)
                    {
                        return "";
                    }
                    Stream myStream;
                    myStream = saveFileDialog1.OpenFile();
              
                    //StreamWriter   sw=new   StreamWriter(myStream,System.Text.Encoding.GetEncoding( "gb2312 "));
                    //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("utf-8"));
                     
                    String str = "";
                    //写标题
                    for (int i = 0; i < dv.Table.Columns.Count; i++)
                    {
                        if (i > 0)
                        {
                            str += "	";
                        }
                        str += dv.Table.Columns[i].ColumnName;
                    }
                    sw.WriteLine(str);
                    //写内容
                    for (int rowNo = 0; rowNo < dv.Count; rowNo++)
                    {
                        String tempstr = "";
                        for (int columnNo = 0; columnNo < dv.Table.Columns.Count; columnNo++)
                        {
                            if (columnNo > 0)
                            {
                                tempstr += "	";
                            }
                            //tempstr+=dg.Rows[rowNo,columnNo].ToString();
                            tempstr += dv.Table.Rows[rowNo][columnNo].ToString();
                        }
                        sw.WriteLine(tempstr);
                    }
                    sw.Close();
                    myStream.Close();
    
                    MessageBox.Show("Export successful!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return ex.ToString();
                }
                return "";
            }
  • 相关阅读:
    资料网站
    HTML、CSS部分
    面试题三
    面试题二
    面试题一
    上学时的HTML+JS+CSS(小总结)
    01.策略模式-上篇
    【解决方案】HTTP could not register URL http://+:6001/
    【问题与思考】1+"1"=?
    WCF安全3-Transport与Message安全模式
  • 原文地址:https://www.cnblogs.com/hcyblogs/p/6645133.html
Copyright © 2011-2022 走看看