对 Excel 工作表进行排序 c# ||如何排除缺少特定单元格的行

问题描述 投票:0回答:1

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

我正在尝试实现代码,该代码将跳过工作表中没有帐户代码的所有行(第 2 列)

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

Excel 工作表需要从此(合作伙伴)转换...

对此(进化)...

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

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");
        }
    }
}
c# excel csv openfiledialog
1个回答
1
投票

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

string[] fields = lines[i].Split(',');
if (fields[1] != "") <-- check if account code is an empty string 
{
    if (recordMatches(searchTerm , fields , positionOfSearch))
    {
    ... 
© www.soinside.com 2019 - 2024. All rights reserved.