排序 excel 张 c# ||如何排除缺少特定单元格的行

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

我目前正在使用 c# 开发一个 windows 表单应用程序,它将能够将 Pastel Partner 试算天平转换为 Pastel Evolution 兼容天平。

我正在尝试实现将跳过 sheet 中没有帐户代码(第 2 列)

中所有行的代码

请提供有关如何执行此操作的代码示例,我们将不胜感激!

excel sheet 需要从这个(合作伙伴)转换...

为此(进化)...

到目前为止,这是我的表格和代码...

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");
        }
    }
}

如果帐户代码在第 2 列,那么您可以在已有的循环中检查空值

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