单击打开时,将我使用 wpf 中的对话框打开的 excel 文件加载到数据网格中
Load the excel file which i open using dialog box in wpf into datagrid when i click open
private void btndilog_Click(object sender, RoutedEventArgs e)
{
var dilog = new Microsoft.Win32.OpenFileDialog();
dilog.FileName = "Excel files";
dilog.DefaultExt = ".xlsx";
dilog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
///show open file dialog box
bool? result = dilog.ShowDialog();
///process open file dialog box results
if(result==true)
{
string filename = dilog.FileName;
}
}
**我编写这段代码是为了使用对话框加载一个 excel 文件,现在我希望每当我单击打开按钮时,该 excel 文件的所有数据都会加载到我的文件中datagrid,因为我是 c# 的新手,而且 wpf 在做这件事时遇到了麻烦任何人都可以帮助我
Datagrid where i want to display the details of excel file
Excel file that i want to load to my data grid my i click on open button
添加引用Microsoft.Office.Interop.Excel并使用这段代码首先将文件读入数据表,然后将数据表与数据网格绑定
//using a click event
private void MnBrowse_Click(object sender, RoutedEventArgs e)
{
//using dialog box to open the excel file and giving the extension as .xlsx to open the excel files
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".xlsx";
openfile.Filter = "(.xlsx)|*.xlsx";
///show open file dialog box
bool? result = openfile.ShowDialog();
///process open file dialog box results
if (result == true)
{
//copying the path of the excel file to a textbox named txtFilePath
txtFilePath.Text = openfile.FileName;
//Add reference then select Microsoft .Office.Interop.Excel
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Static File From Base Path...........
//Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "TestExcel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Dynamic File Using Uploader...........
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;
//reading the excel file in the datatable
string strCellData = "";
double douCellData;
int rowCnt = 0;
int colCnt = 0;
DataTable dt = new DataTable();
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
string strColumn = "";
strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
dt.Columns.Add(strColumn, typeof(string));
}
for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
{
string strData = "";
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
try
{
strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += strCellData + "|";
}
catch (Exception Ex)
{
douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += douCellData.ToString() + "|";
}
}
strData = strData.Remove(strData.Length - 1, 1);
dt.Rows.Add(strData.Split('|'));
}
dtGrid.ItemsSource = dt.DefaultView;
excelBook.Close(true, null, null);
excelApp.Quit();
}
}
private void btndilog_Click(object sender, RoutedEventArgs e)
{
var dilog = new Microsoft.Win32.OpenFileDialog();
dilog.FileName = "Excel files";
dilog.DefaultExt = ".xlsx";
dilog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
///show open file dialog box
bool? result = dilog.ShowDialog();
///process open file dialog box results
if(result==true)
{
string filename = dilog.FileName;
}
}
**我编写这段代码是为了使用对话框加载一个 excel 文件,现在我希望每当我单击打开按钮时,该 excel 文件的所有数据都会加载到我的文件中datagrid,因为我是 c# 的新手,而且 wpf 在做这件事时遇到了麻烦任何人都可以帮助我
Datagrid where i want to display the details of excel file
Excel file that i want to load to my data grid my i click on open button
添加引用Microsoft.Office.Interop.Excel并使用这段代码首先将文件读入数据表,然后将数据表与数据网格绑定
//using a click event
private void MnBrowse_Click(object sender, RoutedEventArgs e)
{
//using dialog box to open the excel file and giving the extension as .xlsx to open the excel files
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".xlsx";
openfile.Filter = "(.xlsx)|*.xlsx";
///show open file dialog box
bool? result = openfile.ShowDialog();
///process open file dialog box results
if (result == true)
{
//copying the path of the excel file to a textbox named txtFilePath
txtFilePath.Text = openfile.FileName;
//Add reference then select Microsoft .Office.Interop.Excel
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Static File From Base Path...........
//Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "TestExcel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Dynamic File Using Uploader...........
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;
//reading the excel file in the datatable
string strCellData = "";
double douCellData;
int rowCnt = 0;
int colCnt = 0;
DataTable dt = new DataTable();
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
string strColumn = "";
strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
dt.Columns.Add(strColumn, typeof(string));
}
for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
{
string strData = "";
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
try
{
strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += strCellData + "|";
}
catch (Exception Ex)
{
douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += douCellData.ToString() + "|";
}
}
strData = strData.Remove(strData.Length - 1, 1);
dt.Rows.Add(strData.Split('|'));
}
dtGrid.ItemsSource = dt.DefaultView;
excelBook.Close(true, null, null);
excelApp.Quit();
}
}