using Excel = Microsoft.Office.Interop.Excel; void CreateExcel() { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application();//new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int i = 3; int j = 0; xlWorkSheet.Cells[1, 1] = "Protocol from " + DateTime.Now.ToShortDateString(); Excel.Range rg = (Excel.Range)xlWorkSheet.Cells[1, 1]; rg.EntireColumn.NumberFormat = "MM/DD/YYYY"; xlWorkSheet.Columns.AutoFit(); xlWorkBook.SaveAs("sdfgsdfg.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } }
C# How to work with Excel. Simple example.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.