Net Core教程

C#读取主从文件的excel并把结果为pass的文件打包

本文主要是介绍C#读取主从文件的excel并把结果为pass的文件打包,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

主入口

static void Main(string[] args)
{
TestingDataChecker dataChecker = new TestingDataChecker("IBW087NB", new DateTime(2021, 5, 11), "IBW087NB-10000020");
var r = dataChecker.Check("IBW087NB-1005210016");
var r1 = dataChecker.Check("IBW087NB-1005210017");
if (r.Success)
{
dataChecker.Packing("IBW087NB-10000020");
}
}

 

TestingDataChecker.cs

public class TestingDataChecker
    {
        public TestingDataChecker(string project,DateTime date)
        {
            Project = project;
            Date=date.ToString("yyyyMMdd");
            OringalTestingTotalResult= LoadTotalResult();
            EnsureOutputPath();
            //ResultColumns = new string[] { "OV_Result", "COV1_Result", "CUV1_Result", "OCCHG1_Result", "OCDSG1_Result", "NormalSCC_Result", "IRN_Result", "THR_Result" };
            ResultColumns = new string[] { "Result"};
        }
        string TestingResultRoot
        {
            get
            {
                return @"D:\smt";
            }
        }

        string TestingTotalResultFile { get; set; }
        string OutPutPath
        {
            get
            {
                return "SMTCheck";
            }
        }

        string Project { get; set; }

        string Date { get; set; }

        DataTable OringalTestingTotalResult
        {
            get; set;
        }
        DataTable OutputTestingTotalResult
        {
            get; set;
        }

        string[] ResultColumns
        {
            get;set;
        }

        void EnsureOutputPath()
        {
            var target = Path.Combine(OutPutPath, Date);
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            OutputTestingTotalResult = OringalTestingTotalResult.Clone();
        }

        DataTable LoadTotalResult()
        {
            var path = string.Format("{0}_{1}*.csv", Date, Project);
            var files=Directory.GetFiles(TestingResultRoot, path);
            if (files.Length > 0)
            {
                TestingTotalResultFile = files[0];
                ExcelHelper excel = new ExcelHelper();
                var table = excel.Read(files[0]);
                return table;
            }
            else
                throw new Exception("无法找到主文件");
        }

        public JeffSoft.OperationResult Check(string sn)
        {
            var result = new JeffSoft.OperationResult(); 
            var newsn="\""+sn+ "\t\""; //发现文件中记录的数据后面有\t,不加的话查不到。
            var rows=OringalTestingTotalResult.Select("barcode='" + newsn + "'", "StartTime desc");
            if (rows.Length > 0)
            {
                bool flag = true;
                foreach (var c in ResultColumns)
                {
                    if (rows[0][c].ToString() != "Pass")
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    OutputTestingTotalResult.Rows.Add(rows[0].ItemArray);
                    var starttime = Convert.ToDateTime(rows[0]["StartTime"]);
                    var subfile = string.Format("{0}_{1}_{2}*.xls",sn, starttime.ToString("yyyy-MM-dd"), starttime.ToString("hhmm"));
                    var files = Directory.GetFiles(Path.Combine(TestingResultRoot, Date), subfile);
                    if (files.Length > 0)
                    {
                        File.Copy(files[0], Path.Combine(OutPutPath, Date, new FileInfo(files[0]).Name),true);
                    }
                    else
                        result.Errors.Add("找不到子文件:" + subfile);
                }
                else
                    result.Errors.Add("存在不通过的检查项");
            }
            else
                result.Errors.Add("主文件中找不到指定的条码");
            return result;
        }

        /// <summary>
        /// 将生成的文件压缩成一个文件方便发送
        /// </summary>
        public string Packing()
        {
            ExcelHelper helper = new ExcelHelper();
            helper.DataSource = OutputTestingTotalResult;
            var tmp=helper.Export();
            var totalfile = Path.Combine(OutPutPath, new FileInfo(TestingTotalResultFile).Name);
            File.Copy(tmp, totalfile,true);
            List<string> files = new List<string>();
            List<string> zipfiles = new List<string>();
            files.Add(new FileInfo(totalfile).FullName);
            var subfiles = Directory.GetFiles(Path.Combine(OutPutPath, Date));
            foreach(var s in subfiles)
            {
                files.Add(new FileInfo(s).FullName);
            }
            foreach(var f in files)
            {
                zipfiles.Add(f.Replace(new DirectoryInfo(OutPutPath).FullName,""));
            }
            JeffSoft.Compress compress = new JeffSoft.Compress();
            string zippath = Path.Combine(OutPutPath, string.Format("{0}-{1}.zip", Project, Date));
            compress.CompressFile(files.ToArray(),zippath, zipfiles.ToArray());
            return zippath;
        }
    }
TestingDataChecker.cs

 

拓展:给定文件的路径,读取文件的二进制数据,判断文件的编码类型

internal class Common
    {
        /// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
        /// <param name="FILE_NAME">文件路径</param>
        /// <returns>文件的编码类型</returns>

        public static System.Text.Encoding GetType(string FILE_NAME)
        {
            System.IO.FileStream fs = new System.IO.FileStream(FILE_NAME, System.IO.FileMode.Open,
                System.IO.FileAccess.Read);
            System.Text.Encoding r = GetType(fs);
            fs.Close();
            return r;
        }

        /// 通过给定的文件流,判断文件的编码类型
        /// <param name="fs">文件流</param>
        /// <returns>文件的编码类型</returns>
        public static System.Text.Encoding GetType(System.IO.FileStream fs)
        {
            byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
            byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
            byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
            System.Text.Encoding reVal = System.Text.Encoding.Default;

            System.IO.BinaryReader r = new System.IO.BinaryReader(fs, System.Text.Encoding.Default);
            int i;
            int.TryParse(fs.Length.ToString(), out i);
            byte[] ss = r.ReadBytes(i);
            if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
            {
                reVal = System.Text.Encoding.UTF8;
            }
            else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
            {
                reVal = System.Text.Encoding.BigEndianUnicode;
            }
            else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
            {
                reVal = System.Text.Encoding.Unicode;
            }
            r.Close();
            return reVal;
        }

        /// 判断是否是不带 BOM 的 UTF8 格式
        /// <param name="data"></param>
        /// <returns></returns>
        private static bool IsUTF8Bytes(byte[] data)
        {
            int charByteCounter = 1;  //计算当前正分析的字符应还有的字节数
            byte curByte; //当前分析的字节.
            for (int i = 0; i < data.Length; i++)
            {
                curByte = data[i];
                if (charByteCounter == 1)
                {
                    if (curByte >= 0x80)
                    {
                        //判断当前
                        while (((curByte <<= 1) & 0x80) != 0)
                        {
                            charByteCounter++;
                        }
                        //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
                        if (charByteCounter == 1 || charByteCounter > 6)
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    //若是UTF-8 此时第一位必须为1
                    if ((curByte & 0xC0) != 0x80)
                    {
                        return false;
                    }
                    charByteCounter--;
                }
            }
            if (charByteCounter > 1)
            {
                 
            }
            return true;
        }
    }
Common.cs

 

这篇关于C#读取主从文件的excel并把结果为pass的文件打包的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!