txt文件帮助类

Posted on 2014-03-17 15:34  哥德巴赫猜  阅读(263)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Windows.Forms;

//add 2013-08-22 weiyj 修改XML文件格式
namespace yjs_cl_seq
{
    public class TXTHelper
    {
        public void createTxt(string DocTitle, string path, DataTable sampTable, DataTable zfTable, string CurrentUserName,string netUser,string netPwd, string netIP )
        {
            path = path + @"\" + CurrentUserName;
            try
            {
                //访问的是网络路径
                if (!string.IsNullOrEmpty(netUser) && !string.IsNullOrEmpty(netPwd) && !string.IsNullOrEmpty(netIP))
                {
                    if (ConnectHelper.impersonateValidUser(netUser, netIP, netPwd) == false)
                    {
                        MessageBox.Show("网络路径不存在");
                        return;
                    }
                }
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("访问路径错误" + ex.ToString());
                return;
            }

            FileStream fs = new FileStream(path + @"\" + DocTitle + "_" + DateTime.Now.ToString("yyyy-MM-dd hhmmssfff") + ".txt", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            DateTime datenow = new DateTime();
            datenow = DateTime.Now;

            StringBuilder str = new StringBuilder();
            str.Append("[Header]");
            str.AppendLine();
            str.AppendFormat("Batch File Name    {0}", "DB\\Sample_LC_EN\\1\\Admin");
            str.AppendLine();
            str.AppendFormat("Output Date    {0}", datenow.ToString("yyyy-MM-dd"));
            str.AppendLine();
            str.AppendFormat("Output Time    {0}", datenow.ToString("hh:mm:ss"));
            str.AppendLine();
            str.AppendLine();
            str.Append("[File Information]");
            str.AppendLine();
            str.Append("Type    Batch File");
            str.AppendLine();
            str.AppendFormat("Generated    {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            str.AppendLine();
            str.AppendFormat("Generated by    {0}", "System Administrator");
            str.AppendLine();
            str.AppendFormat("Modified    {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            str.AppendLine();
            str.AppendFormat("Modified by    {0}", "System Administrator");
            str.AppendLine();
            str.AppendLine();
            str.AppendFormat("[File Description]");
            str.AppendLine();
            str.AppendLine();
            str.AppendLine();
            str.AppendFormat("[Instrument Type]");
            str.AppendLine();
            str.AppendFormat("Type    0");
            str.AppendLine();
            str.AppendLine();
            str.AppendLine("[ASCII Convert]");
            str.AppendLine("Mode    1");
            str.AppendLine("File    C:\\Users\\peng\\Desktop\\LIMS_Export\\ASCIIData.txt");
            str.AppendLine("Auto-Increment    1");
            str.AppendLine("Items    1.2.3");
            str.AppendLine("Delimiter    \", \"");
            str.AppendLine();
            str.AppendFormat("[Batch Table]");
            str.AppendLine();
            str.AppendFormat("# of Row    1");
            str.AppendLine();
            str.AppendFormat("运行模式\t样品瓶架\t样品瓶号\t样品名\t样品ID\t样品类型\t分析类型\t方法文件\t数据文件\t背景\t背景文件\t级别号\t进样体积\t内标量\t样品量\t稀释因子\t系统检查\t输出报告\t报告格式文件\t用户程序\t措施\t数据描述\t自动排气\t基线检查\t选项1\t选项2\t选项3\t选项4\t选项5\t定制参数\t自动调谐\t调谐文件\tPsiPort Instrument Method File\t输出汇总报告\t汇总报告格式文件\t选项6\t选项7\t选项8\t选项9\t选项10\t浓度覆盖");
            //str.AppendFormat("运行模式\t样品瓶架\t样品瓶号\t样品名\t样品ID\t样品类型\t数据文件\t级别号\t数据描述\t");
            str.AppendLine();
            #region
            if (sampTable != null && sampTable.Rows.Count > 0)
            {
                int index = 0;
                for (int i = 0; i < sampTable.Rows.Count; i++)
                {
                    //进样次数
                    int jycsInt = 0;
                    string jycs = sampTable.Rows[i]["进样次数"].ToString();
                    if (int.TryParse(jycs, out jycsInt))
                    {
                        jycsInt = int.Parse(jycs);
                    }
 
                    //进样次数是多少就生成多少行
                    for (int j = 0; j < jycsInt; j++)
                    {
                        index++;
                        str.AppendFormat("{0}\t", "DL AQ DP");//运行模式           
                        str.AppendFormat("{0}\t", "1");//样品瓶架            
                        str.AppendFormat("{0}\t", "1");//样品瓶号            
                        str.AppendFormat("{0}\t", sampTable.Rows[i]["别名"].ToString() + "/" + sampTable.Rows[i]["项目"].ToString() + "/" + sampTable.Rows[i]["称量次数"].ToString());//样品名            
                        str.AppendFormat("{0}\t", sampTable.Rows[i]["名称"].ToString());//样品ID            
                        str.AppendFormat("{0}\t", "1:标准");//样品类型  
                        str.AppendFormat("{0}\t", "");//分析类型    
                        str.AppendFormat("{0}\t", "1");//方法文件            
                        str.AppendFormat("{0}\t", sampTable.Rows[i]["名称"].ToString() +"_"+ index);//数据文件            
                        str.AppendFormat("{0}\t", "");//背景            
                        str.AppendFormat("{0}\t", "");//背景文件            
                        str.AppendFormat("{0}\t", "1");//级别号            
                        str.AppendFormat("{0}\t", "");//进样体积            
                        str.AppendFormat("{0}\t", "");//内标量            
                        str.AppendFormat("{0}\t", "");//样品量            
                        str.AppendFormat("{0}\t", "");//稀释因子            
                        str.AppendFormat("{0}\t", "");//系统检查            
                        str.AppendFormat("{0}\t", "");//输出报告            
                        str.AppendFormat("{0}\t", "");//报告格式文件            
                        str.AppendFormat("{0}\t", "");//用户程序            
                        str.AppendFormat("{0}\t", "");//措施            
                        str.AppendFormat("{0}\t", sampTable.Rows[i]["项目编号"].ToString());//数据描述           
                        str.AppendFormat("{0}\t", "");//自动排气            
                        str.AppendFormat("{0}\t", "");//基线检查            
                        str.AppendFormat("{0}\t", "");//选项1            
                        str.AppendFormat("{0}\t", "");//选项2            
                        str.AppendFormat("{0}\t", "");//选项3            
                        str.AppendFormat("{0}\t", "");//选项4            
                        str.AppendFormat("{0}\t", "");//选项5            
                        str.AppendFormat("{0}\t", "");//定制参数            
                        str.AppendFormat("{0}\t", "");//自动调谐            
                        str.AppendFormat("{0}\t", "");//调谐文件            
                        str.AppendFormat("{0}\t", "");//PsiPort Instrument Method File            
                        str.AppendFormat("{0}\t", "");//输出汇总报告
                        str.AppendFormat("{0}\t", "");//汇总报告格式文件            
                        str.AppendFormat("{0}\t", "");//选项6            
                        str.AppendFormat("{0}\t", "");//选项7            
                        str.AppendFormat("{0}\t", "");//选项8            
                        str.AppendFormat("{0}\t", "");//选项9
                        str.AppendFormat("{0}\t", "");//选项10            
                        str.AppendFormat("{0}\t", "");//浓度覆盖  
                        str.AppendLine();
                    }
                }
            }
            #endregion
            #region
            if (zfTable != null && zfTable.Rows.Count > 0)
            {
                int index = 0;
                for (int i = 0; i < zfTable.Rows.Count; i++)
                {
                    //进样次数
                    int jycsInt = 0;
                    string jycs = zfTable.Rows[i]["进样次数"].ToString();
                    if (int.TryParse(jycs, out jycsInt))
                    {
                        jycsInt = int.Parse(jycs);
                    }

                    for (int j = 0; j < jycsInt; j++)
                    {
                        index++;
                        str.AppendFormat("{0}\t", "DL AQ DP");//运行模式           
                        str.AppendFormat("{0}\t", "1");//样品瓶架            
                        str.AppendFormat("{0}\t", "1");//样品瓶号            
                        str.AppendFormat("{0}\t", zfTable.Rows[i]["检品名称"].ToString() + "/" + zfTable.Rows[i]["项目"].ToString() + "/" + zfTable.Rows[i]["称量次数"].ToString());//样品名            
                        str.AppendFormat("{0}\t", zfTable.Rows[i]["检品编号"].ToString());//样品ID            
                        str.AppendFormat("{0}\t", "0:未知");//样品类型  
                        str.AppendFormat("{0}\t", "");//分析类型
                        str.AppendFormat("{0}\t", "1");//方法文件            
                        str.AppendFormat("{0}\t", zfTable.Rows[i]["检品编号"].ToString() + "_" + index);//数据文件            
                        str.AppendFormat("{0}\t", "");//背景            
                        str.AppendFormat("{0}\t", "");//背景文件            
                        str.AppendFormat("{0}\t", "1");//级别号            
                        str.AppendFormat("{0}\t", "");//进样体积            
                        str.AppendFormat("{0}\t", "");//内标量            
                        str.AppendFormat("{0}\t", "");//样品量            
                        str.AppendFormat("{0}\t", "");//稀释因子            
                        str.AppendFormat("{0}\t", "");//系统检查            
                        str.AppendFormat("{0}\t", "");//输出报告            
                        str.AppendFormat("{0}\t", "");//报告格式文件            
                        str.AppendFormat("{0}\t", "");//用户程序            
                        str.AppendFormat("{0}\t", "");//措施            
                        str.AppendFormat("{0}\t", zfTable.Rows[i]["项目编号"].ToString());//数据描述           
                        str.AppendFormat("{0}\t", "");//自动排气            
                        str.AppendFormat("{0}\t", "");//基线检查            
                        str.AppendFormat("{0}\t", "");//选项1            
                        str.AppendFormat("{0}\t", "");//选项2            
                        str.AppendFormat("{0}\t", "");//选项3            
                        str.AppendFormat("{0}\t", "");//选项4            
                        str.AppendFormat("{0}\t", "");//选项5            
                        str.AppendFormat("{0}\t", "");//定制参数            
                        str.AppendFormat("{0}\t", "");//自动调谐            
                        str.AppendFormat("{0}\t", "");//调谐文件            
                        str.AppendFormat("{0}\t", "");//PsiPort Instrument Method File            
                        str.AppendFormat("{0}\t", "");//输出汇总报告
                        str.AppendFormat("{0}\t", "");//汇总报告格式文件            
                        str.AppendFormat("{0}\t", "");//选项6            
                        str.AppendFormat("{0}\t", "");//选项7            
                        str.AppendFormat("{0}\t", "");//选项8            
                        str.AppendFormat("{0}\t", "");//选项9
                        str.AppendFormat("{0}\t", "");//选项10            
                        str.AppendFormat("{0}\t", "");//浓度覆盖  
                        str.AppendLine();
                    }
                }
            }
            #endregion
            sw.Write(str);
            sw.Close();
            fs.Close();
        }
    }
}

Copyright © 2024 哥德巴赫猜
Powered by .NET 9.0 on Kubernetes