Insert excel sheet in c # user control

I need to embed part of an excel sheet (not the whole sheet) in a C # user control. And I want to manipulate the data on it and save it in a collection.

What is the best way to do this?

+5
source share
3 answers

I would open a spreadsheet using the COM COM library. If you add a link to the Microsoft Excel object library, you can go to the Com interface.

Add these statements:

using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;

Then you can read from the spreadsheet by doing something like this:

   private void GetData(string fileName, string tabName)
    {
        Workbook theWorkbook;

        Application ExcelObj = null;
        ExcelObj = new Application();

        theWorkbook = ExcelObj.Workbooks.Open(fileName,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);


        Sheets sheets = theWorkbook.Worksheets;
        Worksheet worksheet = (Worksheet)sheets[tabName];

        Range range = worksheet.get_Range("A1:A1", Type.Missing);

        string data = range.Text as string;

        //
        // TODO : store the data
        //

        theWorkbook.Close(false, fileName, null);
    }

This code will read the contents of cell A1 into a string.

COM Excel , , . , , , .

/ .

ODBC Excel, . 1. COM-.

, DataSet. DataGridView, WinForms ListBox WPF. XML, DataSet WriteXml .

+6

DataGridView, , ?

, DataGridView Excel, .

!

+5

Microsoft.Office.Interop.Excel. excel, excel.

0

All Articles