1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ChercherXlLiens
{
class Program
{
public static Excel.Workbook wb;
public static Excel.Application xl;
public static string nLine;
public static string preport;
public static string preport2;
public static Scripting.TextStream report;
public static Scripting.TextStream report2;
public static Scripting.FileSystemObject FSO;
static void Main(string[] args)
{
FSO = new Scripting.FileSystemObject();
xl = new Excel.Application();
xl.EnableEvents = false;
Console.Out.WriteLine("GO !");
preport = "H:/DATA/DataProject/Report.csv";
preport2 = "H:/DATA/DataProject/Report2.csv";
report = FSO.OpenTextFile(preport, Scripting.IOMode.ForAppending, false, Scripting.Tristate.TristateFalse);
report2 = FSO.OpenTextFile(preport2, Scripting.IOMode.ForWriting, false, Scripting.Tristate.TristateFalse);
LookDirect("H:/DATA");
Console.Out.WriteLine("c'est fini!");
Console.In.ReadLine();
report.Close();
report2.Close();
xl.Application.EnableEvents = true;
xl.Quit();
NAR(xl);
}
static void LookDirect(string MD)
{
double fsize;
foreach (Scripting.Folder Fold in FSO.GetFolder(MD).SubFolders)
{
LookDirect(Fold.Path);
}
foreach (Scripting.File Fi in FSO.GetFolder(MD).Files)
{
try
{
if (Fi.Path.Substring(Fi.Path.Length - 3, 3) == "xls")
{
report2.WriteLine(Fi.Path + ";" + Fi.Size.ToString());
fsize = Convert.ToDouble(Fi.Size.ToString());
if (fsize < 35000)
{
if (Haslink(Fi.Path))
{
Console.Out.WriteLine(nLine);
report.WriteLine(nLine);
}
}
}
}
catch
{
Console.Out.WriteLine(Fi.Path + " : a donné une erreur");
}
finally
{
Console.Out.Write("1");
}
}
}
static Boolean Haslink(string Fi)
{
Boolean rt = false;
try
{
nLine = Fi;
wb = xl.Workbooks.Open(Fi, false, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
object src = (object)wb.LinkSources(Excel.XlLinkType.xlLinkTypeExcelLinks);
System.Array LnkSrc = (Array)src;
if (LnkSrc != null)
{
for (int i = 1; i <= LnkSrc.Length; i++)
{
nLine.Insert(nLine.Length, LnkSrc.GetValue(i).ToString());
nLine = nLine + ";" + i.ToString() + ";" + LnkSrc.GetValue(i).ToString();
}
wb.Close(false, "", false);
NAR(wb);
if (LnkSrc.Length != 0)
{
rt = true;
}
}
}
catch
{
wb.Close(false, Missing.Value, Missing.Value);
NAR(wb);
xl.Quit();
NAR(xl);
xl = new Excel.Application();
xl.Application.EnableEvents = false;
}
return rt;
}
static void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}
}
} |