Open Save Excel File PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Create Excel file, Open Excel file, Write and Read Excel file in C# by 4MicrosoftSolutions 1.

October 2012 08:47

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;

using Excel = Microsoft.Office.Interop.Excel;

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.

private void button1_Click(object sender, EventArgs e)

Excel.Application oXL = new Excel.Application();

Excel.Workbook oWB = oXL.Workbooks.Add(Missing.Value);

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);

oWB.Close(true, 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.

private void button2_Click(object sender, EventArgs e)

Excel.Application oXL = new Excel.Application();

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);

//read Excel sheets

foreach (Excel.Worksheet ws in oWB.Sheets)

MessageBox.Show(ws.Name);

//save as separate copy

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);

oWB.Close(true, 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.

private void button3_Click(object sender, EventArgs e)

Excel.Application oXL = new Excel.Application();

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);

Excel.Worksheet oWS = oWB.Worksheets[1] as Excel.Worksheet;

//rename the Sheet name

oWS.Name = "Excel Sheet";

for (int i = 1; i < 10; i++)

oWS.Cells[i, 1] = "Cell " + i.ToString();

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);

Process.Start(Application.StartupPath + "\\Sample1.xlsx ");

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.

private void button4_Click(object sender, EventArgs e)

Excel.Application oXL = new Excel.Application();

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.Worksheet oWS = oWB.Worksheets[1] as Excel.Worksheet;

Excel.Range range;

range = oWS.UsedRange;

//read first row, first cell value

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.

You might also like