遍历目录中的子目录 [英] Loop through sub directories in directory

查看:133
本文介绍了遍历目录中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录文件夹,其中包含许多子目录。每个子目录中都有许多图像。我想遍历文件夹目录中的子目录,然后遍历每个目录中的所有图像,以将图像导出到Excel,并将来自每个子目录的图像导出到一个Excel工作表中。

I have a directory 'Folder' with many subdirectories inside this directory. Inside every subdirectory there are many images. I want to loop through subdirectories in the 'Folder' directory, then loop through all images in every directory to export the images out to Excel, with images from each subdirectory in one Excel worksheet.

例如如果我有10个子目录,则我应该有一个包含10个Excel工作表的Excel工作簿,然后在每个Excel工作表中,每个子目录中都会有图像。

For e.g. if I have ten subdirectories, I should have one Excel workbook with ten Excel worksheets, then in each Excel worksheet there will be images from each subdirectory.

我尝试过的操作,但是图像仅显示在Worksheet1上,而不是所有工作表上:

   public void ExportToExcel()
        {
            //for export
            ExcelPackage objExcelPackage = new ExcelPackage();   //create new workbook

            string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));
            int count = 0;
            int count1 = 0;
            int x = 25;
            int finalValue = 0;

            foreach (string subdir in filesindirectory)
            {                      
                count++;
                ExcelWorksheet ws = objExcelPackage.Workbook.Worksheets.Add("Worksheet" + count); //create new worksheet

            foreach (string img in Directory.GetFiles(subdir))
            {
                count1++;
                System.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
                System.Drawing.Image myImage = System.Drawing.Image.FromFile(img);
                var pic = ws.Drawings.AddPicture(count1.ToString(), myImage);
                // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                if (count1 > 1)
                {
                    pic.SetPosition(finalValue, 0, 2, 0);
                    finalValue += (x + 1); // Add 1 to have 1 row of empty row
                }
                else
                {
                    pic.SetPosition(count1, 0, 2, 0);
                    finalValue = (count1 + x) + 1; // Add 1 to have 1 row of empty
                }
            }
            }

            var filepath = new FileInfo(@"C:\Users\user\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx");
            objExcelPackage.SaveAs(filepath);
        }

如何遍历目录中的每个子目录,然后遍历所有图像

How to loop through each sub directories in a directory, then loop through all images from each sub directory using C#?

推荐答案

Janne Matikainen 是正确的,但是您需要知道如何修改您的代码...

Janne Matikainen answer is correct but you need to know how to modify in your code...

首先在此行更改您的代码

First change your code this line

 string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Folder"));

 string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));

第二步,您需要在子文件夹路径中搜索文件

Secondly you need to search file in your sub folder path which is your

foreach (string subdir in filesindirectory)

subdir 是您的目录路径。

只需执行与获取文件相同的操作

Just do back the same thing what you did to get the files

foreach (string img in  Directory.GetFiles(subdir))

完成foreach子目录

After you finish the foreach subdirectory

foreach (string img in  Directory.GetFiles(subdir)) 
{
   // Your Code
}
// Reset the Count1
count1 = 0;

重置它是因为您正在增加每张图纸的动态行生成。

然后,您进入了新工作表,但没有重置它。
它将继续按照上一张表进行计数。

Reset it because you are increasing for dynamic row generating for each sheet.
Then you at new sheet and you didn't reset it.
It will continue the count as per previous sheet.

要获取文件夹名称,您可以轻松地将其拆分。

在创建工作表之前,请按照以下步骤进行操作。

To get the folder name you can easily get it by split.
Before you create the worksheet you do as per below.

string[] splitter = subdir.Split('\\');
string folderName = splitter[splitter.Length - 1];

请注意(如果 folderName 包含一些注释)符号,它可能无法将其设置为excel工作表,并且名称不能太长。

请确保您用excel工作表的可支持符号替换

Take NOTES if the folderName contain some symbol it might not able to set it to excel worksheet and also the name cannot be too long.
Please ensure that you replace with supportable symbol for excel worksheet

这篇关于遍历目录中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆