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
|
class ExcelFiles
{
private ExcelFile file;
private XL.Application app;
public ExcelFiles()
{
app = new XL.Application();
app.Visible = false;
}
public void Process(string fileName)
{
file = new ExcelFile(fileName);
try
{
file.Open(app);
//file.Close();
}
catch(ExcelFileException e)
{
// Ecriture de l'erreur dans la db
}
}
public class ExcelFile
{
private XL.Workbook wb = null;
private static System.Timers.Timer chrono;
public ExcelFile(string fileName)
{
this.FileName = fileName;
chrono = new System.Timers.Timer(10000);
chrono.Elapsed += new System.Timers.ElapsedEventHandler(chronotick);
}
#region Properties
public string FileName { get; private set; }
#endregion
/**
* Ouvre le fichier renseigné dans wb
* I app application Excel ouverte
*/
public void Open(XL.Application app)
{
chrono.Start();
wb = app.Workbooks.Open(this.FileName);
chrono.Stop();
swf.MessageBox.Show("Le fichier est ouvert");
}
/**
* Ferme le fichier sans l'enregister
*/
public void Close()
{
wb.Close(false);
}
/**
* Se déclenche après le temps d'attente si le fichier n'a pas été ouvert
* après le temps imparti à chrono
*/
private void chronotick(object source, System.Timers.ElapsedEventArgs e)
{
chrono.Stop();
throw new ExcelFileException(this.FileName);
}
}
public class ExcelFileException : Exception
{
public ExcelFileException(string fileName)
: base("Problème à l'ouverture")
{
this.FileName = fileName;
}
public string FileName { get; private set; }
} |
Partager