将DataSet数据源导出到txt文件(规定txt文件记录条数)

本文介绍了一种从数据集导出数据到多个TXT文件的方法,包括如何按记录数量拆分文件、处理文件路径及名称、确保文件内容的正确布局等关键技术细节。
部署运行你感兴趣的模型镜像

    /// <summary>
    /// Export.cs的摘要说明。
    /// 导出到txt文档。
    /// </summary>
    public class Export
    {
        protected static string path()
        {
            string str3 = Directory.GetCurrentDirectory();
            string[] sArr = str3.Split('b');
            return sArr[0] + "temptxt\\";
        }

        private const int TXTRecords = 10;//默认txt文档中记录条数。
        private const string TXTPOSTFIX = ".txt";
        private const int DATADISTANCE = 5;
        private const int TABDISTANCE = 8;

        public Export()
        {
            //删除目标文件夹下的所有txt文件
            DeleteFile(path());
        }

        public static void DeleteFile(string dirRoot)
        {
            try
            {
                string[] rootDirs = Directory.GetDirectories(dirRoot); //当前目录的子目录:
                string[] rootFiles = Directory.GetFiles(dirRoot);        //当前目录下的文件:

                foreach (string s2 in rootFiles)
                {
                    File.Delete(s2);                      //删除文件                   
                }
                foreach (string s1 in rootDirs)
                {
                    DeleteFile(s1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
 

 

        //获取数据源
        public DataSet GetData()
        {
            try
            {
                DBHelper db = new DBHelper();
                //现在到昨天所有数据库中的内容
                string strQuery = "select * from Store where SimulationTime between @YesterdayTime and @CurrentTime";
                SqlCommand cmd = db.GetSqlStringCommond(strQuery);

                string Millisecond = DateTime.Now.Millisecond.ToString();
                db.AddInParameter(cmd, "@YesterdayTime", DbType.DateTime, Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss") + "." + Millisecond));
                db.AddInParameter(cmd, "@CurrentTime", DbType.DateTime, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));

                return db.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        //将数据源导入到多个txt文件中,规定每个txt文件存放记录条数。
        public  void ExportToTxt(DataSet ds)
        {
            if (ds.Tables.Count != 0)
            {
                //统计dataset中当前表的行数
                int row = ds.Tables[0].Rows.Count;

                //统计dataset中当前表的列数
                int column = ds.Tables[0].Columns.Count;

                //用于统计当前表中每列记录中字符数最长的字符串的长度之和
                int totalLength = 0;

                int[] columnLength = new int[column];

                int txtCounts = 0;//定义txt文档的数量
                if (row % TXTRecords == 0)
                {
                    txtCounts = row / TXTRecords;
                }
                else
                {
                    txtCounts = row / TXTRecords + 1;
                }

                for (int x = 1; x < txtCounts+1; x++)
                {

                    string tempFileName = null;
                    Comon c = new Comon();
                    tempFileName = c.Ipcid + "_" + DateTime.Now.ToString("yyyyMMddhhmmss")+"_"+x;

                    FileInfo file = new FileInfo(path() + tempFileName + TXTPOSTFIX);
                    StreamWriter textFile = null;
                    try
                    {
                        textFile = file.CreateText();
                    }
                    catch
                    {
                        MessageBox.Show("系统找不到指定目录下的文件: " + path() + tempFileName + TXTPOSTFIX);
                        return;
                    }

                    for (int i = 0; i < column; i++)
                    {
                        columnLength[i] = ds.Tables[0].Columns[i].ColumnName.ToString().Length;
                    }
                    for (int i = 0; i < row; i++)
                    {
                        for (int j = 0; j < column; j++)
                        {
                            if (ds.Tables[0].Rows[i][j].ToString().Length > columnLength[j])
                            {
                                columnLength[j] = ds.Tables[0].Rows[i][j].ToString().Length;
                            }
                        }
                    }

                    for (int i = 0; i < column; i++)
                    {
                        textFile.Write(ds.Tables[0].Columns[i].ColumnName.ToString());//列名
                        for (int k = 0; k < columnLength[i] - ds.Tables[0].Columns[i].ColumnName.ToString().Length + DATADISTANCE; k++)
                        {
                            textFile.Write(' ');
                        }
                    }
                    textFile.WriteLine();

                    for (int i = 0; i < totalLength; i++)
                    {
                        textFile.Write('-');
                    }
                   
                    textFile.WriteLine();
                    textFile.Write("\t");

                    //把dataset中当前表的数据写入.txt文件中
                    int Count = 0;
                    if (x * TXTRecords > row)
                    {
                        Count = row;
                    }
                    else
                    {
                        Count = x * TXTRecords;
                    }

                    for (int i = (x-1)*TXTRecords; i < Count; i++)
                    {
                        for (int j = 0; j < column; j++)
                        {
                            textFile.Write(ds.Tables[0].Rows[i][j].ToString());
                            for (int k = 0; k < columnLength[j] - ds.Tables[0].Rows[i][j].ToString().Length + DATADISTANCE; k++)
                            {
                                textFile.Write(' ');
                            }
                        }
                        textFile.WriteLine();
                        textFile.Write("\t");
                    }
                    textFile.WriteLine();


                    for (int i = 0; i < totalLength; i++)
                    {
                        textFile.Write('-');
                    }


                    textFile.WriteLine();
                    textFile.WriteLine();
                    textFile.WriteLine();

                    //关闭当前的StreamWriter流
                    textFile.Close();
                }
            }

            else
            {
                System.Windows.Forms.MessageBox.Show("No Data");
            }
        }
    }

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值