Пример #1
0
        //fi: fileinfo of a single file (path, and so on..)
        public static bool readExcelFile(FileInfo fi, string dstpath, ExcelObj dst, int count)
        {
            string filePath = "not initialized";
            int    internalCount;
            Dictionary <string, string> sysInfo;
            ExcelObj src = null;

            try
            {
                //full path of a xlsx file
                filePath      = fi.FullName;
                internalCount = 1;
                Console.WriteLine(filePath);

                sysInfo = new Dictionary <string, string>();

                src = new ExcelObj(filePath, false);
                Console.WriteLine("Rows: " + src.row + ", Column: " + src.column);

                #region readingSysInfo

                //reading everything that is on these fixed cells (most of the time)

                sysInfo.Add("IP-Name", src.getValue(1, 1));

                sysInfo.Add("Benutzer", src.getValue(8, 1));

                sysInfo.Add("CPU", src.getValue(3, 1));

                sysInfo.Add("Typenbezeichner", src.getValue(5, 1));

                sysInfo.Add("Seriennummer", src.getValue(10, 1));

                sysInfo.Add("Massenspeicher", src.getValue(12, 1) + " " + src.getValue(14, 1));


                /*
                 * sysInfo.Add("IP-Adresse", src.getValue(28, 1));
                 */

                //filtering ip adress -> not in the same cell every time
                string value = "empty";
                for (int i = 20; i <= 40; i++)
                {
                    value = src.getValue(i, 1);

                    if (value != null)
                    {
                        //looking for possible ip adresses
                        if (value.Contains("IPv4-Adresse") && (value.Contains("192.") || value.Contains("169.") || value.Contains("10.")))
                        {
                            //trimming ip adress, neccessary because a lot of useless information is attached here
                            value = src.getValue(i, 1);
                            string[] seperator = new string[1];
                            seperator[0] = ":";
                            string[] trimmed = value.Split(seperator, StringSplitOptions.RemoveEmptyEntries);

                            value = trimmed[1];
                            break;
                        }
                    }
                    else
                    {
                        value = null;
                    }
                }
                sysInfo.Add("IP-Adresse", value);

                #endregion

                //writing to the console every read info, debug purposes
                foreach (KeyValuePair <string, string> kvp in sysInfo)
                {
                    if (kvp.Key != null && kvp.Value != null)
                    {
                        Console.WriteLine(kvp.Key + ", " + kvp.Value);
                    }
                }
                //free excel and free resources
                //src.Free(true);

                #region saving in Summary
                //save to big summary
                //IP-Name
                //validating everything and writing it into the summary excel
                Console.WriteLine(dst.row + 1);
                if (sysInfo["IP-Name"] != null)
                {
                    //writing to excel
                    Console.WriteLine("Saved IP-name");
                    dst.saveValue(dst.row + 1, 9, sysInfo["IP-Name"]);
                }
                else
                {
                    //writing none to excel
                    dst.saveValue(dst.row + 1, 9, "none");
                }
                //CPU
                if (sysInfo["CPU"] != null)
                {
                    Console.WriteLine("Saved CPU");
                    dst.saveValue(dst.row + 1, 10, sysInfo["CPU"]);
                }
                else
                {
                    dst.saveValue(dst.row + 1, 10, "none");
                }
                //Typenbezeichner
                if (sysInfo["Typenbezeichner"] != null)
                {
                    Console.WriteLine("Saved typenbezeichner");
                    dst.saveValue(dst.row + 1, 7, sysInfo["Typenbezeichner"]);
                }
                else
                {
                    dst.saveValue(dst.row + 1, 7, "none");
                }
                //Benutzer
                if (sysInfo["Benutzer"] != null)
                {
                    Console.WriteLine("Saved Benutzer");
                    dst.saveValue(dst.row + 1, 4, sysInfo["Benutzer"]);
                }
                else
                {
                    dst.saveValue(dst.row + 1, 4, "none");
                }
                //Seriennummer
                if (sysInfo["Seriennummer"] != null)
                {
                    Console.WriteLine("Saved Seriennummer");
                    dst.saveValue(dst.row + 1, 8, sysInfo["Seriennummer"]);
                }
                else
                {
                    dst.saveValue(dst.row + 1, 8, "none");
                }
                //Massenspeicher
                if (sysInfo["Massenspeicher"] != null)
                {
                    Console.WriteLine("Saved massenspeicher");
                    dst.saveValue(dst.row + 1, 12, sysInfo["Massenspeicher"]);
                }
                else
                {
                    Console.WriteLine("no mass storage");
                    dst.saveValue(dst.row + 1, 12, "none");
                }
                //IP-Adresse
                if (sysInfo["IP-Adresse"] != null)
                {
                    dst.saveValue(dst.row + 1, 13, sysInfo["IP-Adresse"]);
                }
                else
                {
                    Console.WriteLine("no ip adress");
                    dst.saveValue(dst.row + 1, 13, "none");
                }

                dst.IncrementRow();
                #endregion

                internalCount++;
                return(true);
            }
            catch (Exception ex)
            {
                errorCount++;
                errorFiles[errorCount] = filePath;

                /*
                 * if(src != null)
                 * {
                 *  Console.WriteLine("tried to error free");
                 *  src.Free(true);
                 * }
                 */
                Console.WriteLine("Error Single Read and Save, " + ex.Message);
                Console.WriteLine("error file: " + filePath);
            }
            finally
            {
                if (src != null)
                {
                    Console.WriteLine("trying to free single read");
                    src.Free(true);
                }
            }
            return(false);
        }
Пример #2
0
        //src: file path where the path for the source excel files is saved
        //dst: file path where the path for the destination excel file is saved
        public static bool readAndWriteExcelFiles(string src, string dst)
        {
            successful = 0;
            count      = 0;
            ExcelObj dstExcel = null;

            errorFiles = new string[10];
            try
            {
                //checking if both paths were entered
                if (src == null || dst == null)
                {
                    return(false);
                }

                //get all the .xlsx files in the folder
                DirectoryInfo d;
                FileInfo[]    files;
                try
                {
                    //getting every excel file needed
                    d          = new DirectoryInfo(src);
                    files      = d.GetFiles("*.csv");
                    errorFiles = new string[files.Count <FileInfo>()];
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("error while scanning direcotry");
                    return(false);
                }

                count = files.Count <FileInfo>();

                //open Destination excel file
                dstExcel = new ExcelObj(getDstPath(), true);
                int oldRow = dstExcel.row;

                foreach (FileInfo file in files)
                {
                    //send each file to reader function
                    if (readExcelFile(file, dst, dstExcel, count))
                    {
                        successful++;
                    }
                }

                //release every process needed for the destination excel
                dstExcel.deleteCells(oldRow);

                Console.WriteLine();
                Console.WriteLine("Error trying to read these Files: ");
                foreach (string errorfile in errorFiles)
                {
                    //writing every failed excel file to the console
                    if (errorfile != null)
                    {
                        Console.WriteLine(errorfile);
                    }
                }
                Console.WriteLine();
                Console.WriteLine(successful + " of " + count + " have been read and re-saved");
                Console.WriteLine("Hit save on the Windows Save Prompt to continue. (if you dont see it use alt+tab to look for it)");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(errorFiles.Count <string>());
                Console.WriteLine("Big error");
            }
            finally
            {
                if (dstExcel != null)
                {
                    Console.WriteLine("trying to free data collection excel");
                    dstExcel.Free(true);
                }
            }
            return(true);
        }