Open Save Excel File PDF
Open Save Excel File PDF
Open Save Excel File PDF
Sometimes you might have requirement to work with Excel files in .Net. We can easily create the Excel files in C#.Net by using Microsoft.Office.Interop.Excel dll. Microsoft.Office.Interop.Excel dll has Workbook class which is used to create Excel files.
We will discuss how to create Excel files and how to open existing Excel files in C#.Net with simple C# windows application. First we have to add Microsoft.Office.Interop.Excel dll to our project by right click on solution explorer and select "Add Reference" option as shown below.
Now add four button controls to win form and name them as "Create Excel", "Open Excel", "Write to Excel" and "Read Excel". Add required namespaces in .cs class as shown below.
using System;
using System.Reflection;
using System.Windows.Forms;
As shown above we are importing System.Reflection and Microsoft.Office.Interop.Excel namespaces. We are using alias as Excel for Microsoft.Office.Interop.Excel namespace to save our coding time. Now in the Create Button click event, create object for Excel Application class and create Workbook by using the Application object as shown below and save it at required location.
oWB.SaveAs(Application.StartupPath + "\\Sample.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
We are using the Workbook object SaveAs method to save the Excel file as shown above.
But for some times you may need to open existing Excel file and need to modify that Excel file and save it in separate location. Then we need to use Workbook object Open method to open the Existing file as shown below. Add below code for "Open Excel" button click event.
Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\Sample.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
MessageBox.Show(ws.Name);
oWB.SaveAs(Application.StartupPath + "\\Sample-new.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Even you can read the Excel file sheets by using WorkSheets class as shown above.
You Can write the content to Excel sheet by using Cells object of WorkSheet class as shown below.
Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\Sample.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oWB.SaveAs(Application.StartupPath + "\\Sample1.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
As shown above, we are writing the content to Excel sheet first 10 rows and for first cell.
Even you can read the Excel file content by using Range class as shown below.
Excel.Workbook oWB = oXL.Workbooks.Open(Application.StartupPath + "\\Sample1.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Range range;
range = oWS.UsedRange;
MessageBox.Show((string)(range.Cells[1,1] as Excel.Range).Value2);
As shown above we are reading the first row and first cell value by using Range class.