Last active
August 29, 2015 13:57
-
-
Save adamkdean/9758008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
using Excel = Microsoft.Office.Interop.Excel; | |
namespace WindowsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
Excel.Application xlApp ; | |
Excel.Workbook xlWorkBook ; | |
Excel.Worksheet xlWorkSheet ; | |
object misValue = System.Reflection.Missing.Value; | |
xlApp = new Excel.ApplicationClass(); | |
xlWorkBook = xlApp.Workbooks.Add(misValue); | |
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); | |
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com"; | |
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, | |
misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, | |
misValue, misValue, misValue, misValue, misValue); | |
xlWorkBook.Close(true, misValue, misValue); | |
xlApp.Quit(); | |
releaseObject(xlWorkSheet); | |
releaseObject(xlWorkBook); | |
releaseObject(xlApp); | |
MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls"); | |
} | |
private void releaseObject(object obj) | |
{ | |
try | |
{ | |
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); | |
obj = null; | |
} | |
catch (Exception ex) | |
{ | |
obj = null; | |
MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); | |
} | |
finally | |
{ | |
GC.Collect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment