本文主要和大家分享php根據excel裡面的內容尋找檔案的代碼,希望能協助到大家。
using Aspose.Cells;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text.RegularExpressions;namespace FindFileByExcel{ class Program { public static void Main(string[] args) { //遺漏代碼的excel記錄檔案夾 string excelPath = @"C:\Users\qingping.li\Desktop\股東報告自動化\各種bug\未解決\十大股東遺漏代碼.xlsx"; //老的excel隱藏檔夾 string oldExcelStoragePath = @"\\10.10.129.77\巨潮下載測試\2017\第三季度年報測試Excel\"; //被挑選出來的excel檔案夾 string newExcelStoragePath = @"\\10.10.129.77\巨潮下載測試\2017\第三季度年報測試Excel\十大股東遺漏\"; //程式集 Aspose.Cells.dll, v5.3.1.0(運用cell操作類) Cells cells = GetExcel(excelPath); //獲得所有檔案 string[] excelFiles = Directory.GetFiles(oldExcelStoragePath); string signValue = "002768"; //找到第一個 Cell celltable = cells.FindStringContains(signValue, cells.FirstCell); List<string> list = new List<string>(); if (celltable != null && celltable.Value != null) { list.Add(celltable.StringValue); Cell cellCount = cells.GetCell(celltable.Row + 1, celltable.Column); while (cellCount != null && cellCount.StringValue != null && Regex.IsMatch(cellCount.StringValue, @"\d")) { cellCount = cells.GetCell(cellCount.Row + 1, cellCount.Column); //獲得所有的list列表 if (cellCount != null) if (!list.Contains(cellCount.StringValue)) list.Add(cellCount.StringValue); } //開始複製所有excel中存在的資料到另外一個檔案夾下面 List<string> copyConpanyExcelFile = new List<string>(); foreach (string countVaue in list) { foreach (string excelFile in excelFiles) { //如果已經找到了,就不再繼續尋找 if (copyConpanyExcelFile.Contains(excelFile)) continue; if (excelFile.IndexOf(countVaue) > -1) { if (!Directory.Exists(newExcelStoragePath)) Directory.CreateDirectory(newExcelStoragePath); string fileName = excelFile.Split('\\')[excelFile.Split('\\').Length - 1]; try { if (!File.Exists(newExcelStoragePath + "\\" + fileName)) File.Copy(excelFile, newExcelStoragePath + "\\" + fileName); //將找到的excel檔案儲存體起來;下次就不再尋找 copyConpanyExcelFile.Add(excelFile); //跳出當前迴圈 break; } catch { continue; } } } } } Console.Write("複製完畢!"); Console.ReadKey(); } public static Cells GetExcel(string excelPath) { Workbook workbook = new Workbook(); try { workbook.Open(excelPath); } catch { return null; } return workbook.Worksheets[0].Cells; } }}