txt文件帮助类

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

原文地址:https://www.cnblogs.com/wenbing/p/3605171.html