How would you open an excel file without the help of Microsoft Excel installed? By using C#, you can easily open excel file without microsoft excel installed. Use c# open excel file can save you a lot of money if you don't plan to purchase Microsoft Office. This simple article is a simple guide of how to use C# open excel file for all CSharp beginners. Before we start the process, we need add Microsoft Excel 12.0 Object Library in the project and a new project should be created and add the common button to the form. Then, C# open excelfile process starts:
1, Add excel reference library in the project. From the following picture, you can find the path to add excel reference library in your project.
a) Click "Project" menu and select "Add reference" dialogue as the image shows
b) Click "COM" menu and select "Microsoft Excel 12.0 Object Library". Push "OK" button as the picture showing
2, In your C# project file, copy and paste the following source code
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.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
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;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
When you finish the simple steps above, it will open the excel file and you can read the content in the cell A1.
Friendly Reminder: It's necessary to add Microsoft.Office.Interop.Excel to your source code.
using Excel = Microsoft.Office.Interop.Excel;
1, Add excel reference library in the project. From the following picture, you can find the path to add excel reference library in your project.
a) Click "Project" menu and select "Add reference" dialogue as the image shows
b) Click "COM" menu and select "Microsoft Excel 12.0 Object Library". Push "OK" button as the picture showing
2, In your C# project file, copy and paste the following source code
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.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
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;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
When you finish the simple steps above, it will open the excel file and you can read the content in the cell A1.
Friendly Reminder: It's necessary to add Microsoft.Office.Interop.Excel to your source code.
using Excel = Microsoft.Office.Interop.Excel;