导入数据excel到WPF [英] Import data excel to WPF
本文介绍了导入数据excel到WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个WPF,我想要通过OpenFileDialog选择的数据(来自excel文件),导入到GridControl,所以写了这个。
I have a WPF, and i want the data which selected by a OpenFileDialog (from excel files), imported to GridControl, so wrote this.
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;
}
但它给出了三个错误(带下划线的)
前两个: 无法找到编译动态表达式所需的一种或多种类型
最后一个: 当前上下文中不存在名称Missing
为什么我会收到这些错误?
提前致谢。
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.
推荐答案
也许看这里:
http://stackoverflow.com/questions/11725514/one-or-more-types-required-to-compile-a-dynamic-expression-cannot-be-found-are [ ^ ]
http://stackoverflow.com/questions/7115055/why-am-i -getting-one-or-more-types-required-to-compile-a-dynamic-expression-can [ ^ ]
您可能会遗漏一些参考文献。
Maybe look here:
http://stackoverflow.com/questions/11725514/one-or-more-types-required-to-compile-a-dynamic-expression-cannot-be-found-are[^]
http://stackoverflow.com/questions/7115055/why-am-i-getting-one-or-more-types-required-to-compile-a-dynamic-expression-can[^]
You might be missing some references.
这篇关于导入数据excel到WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文