using System; using System.IO; namespace ExcelLateBinding { public class ExcelWriter { // Champs private object m_Application; private object m_Workbook; private object m_Worksheet; private object _mis = Type.Missing; private int m_ColumnCount; private int m_ColumnIndex; private int m_RowCount; private int m_RowIndex; private bool m_StreamEnded; private string m_filename; // Constructeur public ExcelWriter() { m_StreamEnded = false; m_ColumnCount = -1; m_ColumnIndex = -1; m_RowCount = -1; m_RowIndex = -1; } // Propriétés public object Application { get { return m_Application; } } public object Workbook { get { return m_Workbook; } } public object Worksheet { get { return m_Worksheet; } } public bool StreamEnded { get { return m_StreamEnded; } } public string Version { get { return (string)LateBindings.Get(m_Application, "Version"); } } // Méthodes // Ferme le processus Excel public void Close() { if (m_Workbook != null) { LateBindings.Invoke(m_Workbook, "Close", new object[3] { false, _mis, _mis }); m_Workbook = null; } if (m_Application != null) { LateBindings.Set(m_Application, "DisplayAlerts", true); LateBindings.Invoke(m_Application, "Quit"); LateBindings.ReleaseInstance(m_Application); m_Application = null; } } // Supprime le contenu de toute la feuille public void Clear() { LateBindings.Invoke(LateBindings.Get(m_Worksheet, "Cells"), "Clear"); } // Enregistre le fichier public void Save() { if (File.Exists(m_filename)) LateBindings.Invoke(m_Workbook, "Save"); else { int fileFormat = string.Compare("11.0", this.Version) < 0 && m_filename.EndsWith(".xlsx", StringComparison.CurrentCultureIgnoreCase) ? 51 : -4143; object[] param = new object[8] { m_filename, fileFormat, string.Empty, string.Empty, false, false, 1, 1 }; LateBindings.Invoke(m_Workbook, "SaveAs", param); } } // Ouvre le processus Excel et le fichier donné (surchargé) public void Open(string filename) { Open(filename, null); } public void Open(string filename, string worksheetName) { m_Application = LateBindings.GetOrCreateInstance("Excel.Application"); LateBindings.Set(m_Application, "DisplayAlerts", false); object workbooks = LateBindings.Get(m_Application, "Workbooks"); m_filename = filename; if (File.Exists(filename)) m_Workbook = LateBindings.Invoke(workbooks, "Open", filename); else m_Workbook = LateBindings.Invoke(workbooks, "Add", _mis); if (string.IsNullOrEmpty(worksheetName)) m_Worksheet = LateBindings.Get(m_Workbook, "Activesheet"); else m_Worksheet = GetWorksheet(worksheetName); SetWorksheetAttributes(); } // Change de ligne public void NewLine() { if (m_StreamEnded) throw new EndOfStreamException(); if (m_RowIndex < m_RowCount) { m_RowIndex++; m_ColumnIndex = 1; } else m_StreamEnded = true; } // Ecrit la valeur donnée dans la cellule courante et avance d'une cellule // ou change de ligne s'il arrive à la fin d'une ligne. public void Write(object value) { if (m_StreamEnded) throw new EndOfStreamException(); object cells = LateBindings.Get(m_Worksheet, "Cells"); object range = LateBindings.Get(cells, "Item", new object[2] { m_RowIndex, m_ColumnIndex }); if (value is String) LateBindings.Set(range, "NumberFormat", "@"); LateBindings.Set(range, "Value2", value); if (m_ColumnIndex == m_ColumnCount) { if (m_RowIndex < m_RowCount) { m_RowIndex++; m_ColumnIndex = 1; } else m_StreamEnded = true; } else m_ColumnIndex++; } // Ecrit un ensemble de valeurs donné sous forme de tableau dans des cellules, // puis change de ligne à la fin. public void WriteLine(object[] values) { if (m_StreamEnded) throw new EndOfStreamException(); foreach (object val in values) { if (m_ColumnIndex <= m_ColumnCount) { object range = LateBindings.Get(m_Worksheet, "Cells"); object cell = LateBindings.Get(range, "Item", new object[2] { m_RowIndex, m_ColumnIndex }); if (val is String) LateBindings.Set(cell, "NumberFormat", "@"); LateBindings.Set(cell, "Value2", val); if (m_ColumnIndex < m_ColumnCount) m_ColumnIndex++; } } if (m_RowIndex < m_RowCount) { m_RowIndex++; m_ColumnIndex = 1; } } // Colle le contenu du presse papier dans la feuille courante public void PasteFromClipboard() { object cells = LateBindings.Get(m_Worksheet, "Cells", new object[2] { 1, 1 }); LateBindings.Invoke(m_Worksheet, "Paste", new object[2] { cells, false }); } // Met à jour les nombres de lignes et de colonnes et la position du lecteur. private void SetWorksheetAttributes() { m_RowIndex = 1; m_ColumnIndex = 1; object rows = LateBindings.Get(m_Worksheet, "Rows"); m_RowCount = (int)LateBindings.Get(rows, "Count"); object cols = LateBindings.Get(m_Worksheet, "Columns"); m_ColumnCount = (int)LateBindings.Get(cols, "Count"); } // Retourne l'instance de la feuille donnée en paramètre, l'ajoute si elle n'existe pas. private object GetWorksheet(string worksheetName) { object worksheets = LateBindings.Get(m_Workbook, "Worksheets"); try { return LateBindings.Get(worksheets, "Item", worksheetName); } catch { object ws = LateBindings.Invoke(worksheets, "Add", _mis); LateBindings.Set(ws, "Name", worksheetName); return ws; } } } }