Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF, and i want the data which selected by a OpenFileDialog (from excel files), imported to GridControl, so wrote this.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.Printing;
using System.ComponentModel;
using System.Collections.ObjectModel;
using DevExpress.Xpf.NavBar;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;


...


    InitializeComponent();
        DataContext = new DataSource();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {


        // Create OpenFileDialog
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



        // Set filter for file extension and default file extension
        dlg.DefaultExt = ".txt";
        dlg.Filter = "EXCEL Files (*.xlsx)|*.xlsx";


        // Display OpenFileDialog by calling ShowDialog method
        Nullable<bool> result = dlg.ShowDialog();


        // Get the selected file name and display in a TextBox
        if (result == true)
        {
            // Open document
            string filename = dlg.FileName;
        }
    }
     public class ExcelData
{
    public DataView Data
    {
        get
        {
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook workbook;
            Excel.Worksheet worksheet;
            Excel.Range range;
            workbook = excelApp.Workbooks.Open(Environment.CurrentDirectory + "\\Excel.xlsx");
            worksheet = (Excel.Worksheet)workbook.Sheets["Test Sheet"];//.get_Item(1);

            int column = 0;
            int row = 0;
             range = worksheet.UsedRange;
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Position");
            dt.Columns.Add("Web Site");
            for (row = 2; row <= range.Rows.Count; row++)
            {
             DataRow dr = dt.NewRow();
             for (column = 1; column <= range.Columns.Count; column++)
                {
             dr[column - 1] = (range.Cells[row, column] as Excel.Range).Value2.ToString();
                }
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }
            workbook.Close(true, Missing.Value, Missing.Value);
            excelApp.Quit();
            return dt.DefaultView;

        }




But it gave three errors (the underlined ones)
first two :"One or more types required to compiled a dynamic expression cannot be found"
last one : "The name "Missing" does not exist in the current context

Why i get those errors?

Thanks in advance.
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900