Search code examples
c#excelcsvopenfiledialog

Sorting excel sheets c# || how to exclude lines that are missing a specific cell


I am currently working on a windows forms app using c# that will be able to convert Pastel Partner trial balances to Pastel Evolution compatible balances.

I am trying to implement code that will skip all lines in the sheet that do not have account codes (col 2)

Please assist with a code example on how to do this, it would be highly appreciated!

The excel sheet needs to convert from this (Partner)...

enter image description here

To this (Evolution)...

enter image description here

This is my form and code so far...

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Evo_Mod
{

    public partial class Form1 : Form
    {
        public string date; // set by user
        public string codeDate; // set by date and OB string
        public string OB = "OB"; // set value
        public string zero = "0"; // set value 
        public string accCode; // needs to be read from the first excell sheet and printed with a ">"
        public string contraCode = "9990>001"; // set value 
        public bool credCheck = false; // bool that is interactive
        public bool debCheck = false; // bool that is interactive depending on whether the account is in credit or debit
        public string fileName = ""; // set from the openFileDialog

 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e) // lets user select  csv file.
        {
            openFileDialog1.ShowDialog();
            openFileDialog1.Filter = "CSV Files (L*.csv)|L*.csv"; // Filters to only .csv files
            fileName = openFileDialog1.FileName;
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                lblFileName.Text = openFileDialog1.FileName; // prints file directory
            }

        }
        
        public static string[] ReadCSV(string searchTerm , string filePath , int positionOfSearch)
        {   
            positionOfSearch--; // moves over by 1 (more understandable) 
            string[] recordNotFound = { " Record Not Found " }; // array for errors

            try
            {
                string[] lines = System.IO.File.ReadAllLines(@filePath);
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] fields = lines[i].Split(',');
                    if (recordMatches(searchTerm , fields , positionOfSearch))
                    {
                        return fields;
                    }
                }

                return recordNotFound;

            }
            catch(Exception ex)
            {
                Console.WriteLine("error");
                return recordNotFound;
                throw new ApplicationException("Error : ", ex);
            }

        }

        public static bool recordMatches(string searchTerm , string[] record , int positionOfSearch)
        {
            if (record[positionOfSearch] != " ")
            {
                return true;
            }
            return false;

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            DateTime tempDate = dateTimePicker1.Value;  
            codeDate = "OB " + tempDate.ToString("dd/MM/yyyy");
            date = tempDate.ToString("dd/MM/yyyy");
        }
    }
}

Solution

  • If the account codes are in col 2, then you can check for empty values in the loop which you already have

    string[] fields = lines[i].Split(',');
    if (fields[1] != "") <-- check if account code is an empty string 
    {
        if (recordMatches(searchTerm , fields , positionOfSearch))
        {
        ...