Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Example: Read Excel sheet and cells in C #

25.10.2018 (👁7977)


 

Code Example in C #, VSTO Addin

This example shows how to read and evaluate C # Excel cells.

 

Excel

Here all cells of an Excel sheet are read and displayed as a message.

 

 

C # code

Under the file Ribbon1.cs (Ribbonbar example)

 

First you have to integrate the Office package in Visual Studio (Visual Studio For Office ... VSTO COM, etc.)

using Microsoft.Office.Interop.Excel;

 

Then you include the current Excel worksheet

Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;

 

You can read a cell like this.

Excel cell

The Excel cell is bound with Cells [row, column] as Range

Microsoft.Office.Interop.Excel.Range cell = usedRange.Cells[1, iColumn] as Range;

 

Read cell value

The displayed value in Excel is actually read out with .Value2.

But you should use .Value2.ToString () so that no conversion error from integer int to string is generated at runtime.

String sValue = cell.Value2.ToString();

 

Direct access to .Value2 at runtime often causes the error:

Error message:

The double type can not be implicitly converted to string.

 

Shown code example in C #

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Microsoft.Office.Interop.Excel;

using Microsoft.Office.Tools.Ribbon;

 

namespace ExcelAddIn01

{

    public partial class Ribbon1

    {

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)

        {

 

        }

 

        private void btnExcel_load_Click(object sender, RibbonControlEventArgs e)

        {

            //< init >

            Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;

            //</ init >

 

            Range usedRange = worksheet.UsedRange;

            int nColumnsMax = 0;

            String sText = "";

            if (usedRange.Rows.Count > 0)

            {

                //----< Read_Header >----

                for (int iColumn = 1; iColumn <= usedRange.Columns.Count; iColumn++)

                {

                    Microsoft.Office.Interop.Excel.Range cell = usedRange.Cells[1, iColumn] as Range;

                    String sValue = cell.Value2;

 

                    if (sValue == ""break;

                    sText = sText + Environment.NewLine + sValue;

                    nColumnsMax = iColumn;

                }

                //----</ Read_Header >----

 

                //----< Read_DataRows >----

                for (int iRow = 2; iRow <= usedRange.Rows.Count; iRow++)

                {

                    for (int iColumn = 1; iColumn < nColumnsMax; iColumn++)

                    {

                        Microsoft.Office.Interop.Excel.Range cell = usedRange.Cells[iRow, iColumn] as Range;

                        String sValue = cell.Value2.ToString();

                        sText = sText + Environment.NewLine + sValue;

                    }

                }

                //----</ Read_DataRows >----

            }

 

            MessageBox.Show(sText);

        }

    }

}

 

 

 

Subject:

Reading Excel in cell with Value Value2 FormLocal

Programmatically, at runtime in Visual Studio VSTO Office Addin

Excel WorkSheet Cells