How to compare if an excel text is equal to part of the name of a pdf file in C#?

Asked

Viewed 42 times

-2

I would like to start a code that reads a column in an excel spreadsheet that contains CNPJ numbers and compares with PDF file names in a folder.

Example: value in an excel cell verses PDF file name, if true, if false.

Could give me a light.

Read the spreadsheet I’ve done but don’t know how to go through the PDF files saved in the folder.

BELOW the code I read the files:

static void Main(string[] args)
{
    
    string[] arquivos = Directory.GetFiles(@"C:\Projects\MOD01-EnvioDeE-mail\fila\");


    Console.WriteLine("Arquivos:");
    foreach (string arq in arquivos)
    {
        string newArq = arq;
        
        Console.WriteLine(arq);

    }

}

BELOW the code I read the spreadsheet.

using System;
using System.Collections.Generic;
using System.Linq;
using ClosedXML.Excel;
using System.Text;
using System.Threading.Tasks;
using System.IO; // A BIBLIOTECA DE ENTRADA E SAIDA DE ARQUIVOS
using iTextSharp; //E A BIBLIOTECA ITEXTSHARP E SUAS EXTENÇÕES
using iTextSharp.text; //ESTENSAO 1 (TEXT)
using iTextSharp.text.pdf;//ESTENSAO 2 (PDF)

namespace LerPlanilhaExcel
{
   class Program
   {
     static void Main(string[] args)
     {
        IXLWorkbook wb = new XLWorkbook();
        IXLWorksheet ws = wb.Worksheets.Add("Sheet");
        wb.SaveAs(@"C:\Projects\LerPlanilhaExcel\LerPlanilhaExcel\excel\MeuExcel.xlsx");

        var workbook = new XLWorkbook(@"C:\Projects\LerPlanilhaExcel\LerPlanilhaExcel\excel\MEI_ATIVO26072021.xlsx");
        StreamWriter logCnpj = new StreamWriter(@"C:\Projects\LerPlanilhaExcel\LerPlanilhaExcel\log\logsCnpj.txt");
        
        int contador = 0;
        int cont = 1;
        
        for (int i = 1; i <= 89; i++) //são 89 praças
        {
            var sheet = workbook.Worksheet(i);
            var linha = 2;
            
            while (true)
            {

                var email = sheet.Cell("O" + linha).Value.ToString();
                var cnpj = sheet.Cell("C" + linha).Value.ToString();
                
                                          
                if (contador == 5) break;
                if (string.IsNullOrEmpty(cnpj) && (string.IsNullOrEmpty(email)))
                {
                    ws.Cell(cont, 1).Value = "CAMPO VAZIO";
                    ws.Cell(cont, 2).Value = "CAMPO VAZIO";
                    wb.SaveAs(@"C:\Projects\LerPlanilhaExcel\LerPlanilhaExcel\excel\MeuExcel.xlsx");
                    contador++;
                }
                else
                {
                    Console.WriteLine(sheet.ToString() + linha + ": " + cnpj + "---" + email);
                    logCnpj.Write(cnpj + "###" + email + "\r\n");

                    ws.Cell(cont, 1).Value = cnpj;
                    ws.Cell(cont, 2).Value = email;
                    wb.SaveAs(@"C:\Projects\LerPlanilhaExcel\LerPlanilhaExcel\excel\MeuExcel.xlsx");
                }

                if (cnpj == )
                {

                }
                
                                    
                //System.Threading.Thread.Sleep(100);
                linha++;
                cont++;

            }
                        
            contador = 0;

        }
        
        workbook.Dispose();
        logCnpj.Close();
        Console.WriteLine("END");
    }
}

}

1 answer

0

To go through the files of a folder you can use the method GetFiles of DirectoryInfo.

DirectoryInfo directory = new DirectoryInfo("Caminho completo da sua pasta");

//Irá selecionar todos os arquivos que possuem extensão .PDF
FileInfo[] files = directory.GetFiles("*.pdf");

The method GetFiles returns an array of FileInfo, this way you can go through the array and make the comparison you need.

foreach (var item in files)
{
    //item.Name é o nome do arquivo 
    if(item.Name == "")
    {

    }
}
  • Right. I would like to compare a number type like this: value in the spreadsheet of exce: 24585524000150 and this is the name of the file DAS-PGMEI-24585524000150.pdf I think I forgot to explain it better.

  • All right, you just need to manipulate this string and extract the number you want to compare. Is this a CNPJ and will it always be at the end of the name? try this way: string cnpj = item.Name.Substring(item.Name.Length - 18, 14); I am giving a substring in the last 14 characters which is the size of a CNPJ. It will depend on how this file name was created.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.