我写过逻辑文件是否存在。 (在excel中读取文件名并检查文件是否在另一个文件夹中)

时间:2018-07-21 14:51:55

标签: c#

在excel文件中将存在文件名。我写的逻辑是它需要遍历excel中的所有文件名,并需要检查文件是否在另一个文件夹中。我写的逻辑工作正常,但对于最后一行,即使文件存在,它也显示为文件不存在。请您帮忙

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.IO;
namespace ConsoleApp6
{
    class Class1
    {
        public void Data()
        {
            string file = @"D:\files\"; //this is the path for pdf file
            Application application = new Application();
            if (application == null)
            {
                Console.WriteLine("is not installed");
            }
            Workbook workbook = application.Workbooks.Open(@"D:\abc1.xlsx"); //this is the excel path
            _Worksheet excelSheet = workbook.Sheets[1];
            Range excelRange = excelSheet.UsedRange;
            int rows = excelRange.Rows.Count;
            int cols = excelRange.Columns.Count;
            //reading the rows and columns of excel file.
            for (int i = 1; i <= rows; i++)
            {
                //create new line
                Console.Write("\r\n");
                for (int j = 2; j < cols; j++)
                {
                    //write the console
                    if (excelRange.Cells[i, j] != null && excelRange.Cells[i, j].Value2 != null)
                    {
                        string xyz1 = excelRange.Cells[i, j].Value2;
                        string xyz = excelRange.Cells[i, j].Value2.ToString();
                        //File.Exists(file + xyz);
                        //below is the line of code where we are appending the file name with excel file.
                        if (!File.Exists(file + xyz))
                            Console.WriteLine("File does not exist.");
                        //Console.Write(excelRange.Cells[i, j].Value2.ToString() + "\t");
                    }
                }

            }
            //after reading, relaase the excel project
            application.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
            Console.ReadLine();
        }
    }
}

0 个答案:

没有答案