TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ahmet Taha
NA
381
20.6k
cannot import excel to datagridview in c#
May 28 2021 7:33 AM
hello guyz i need your help, i cannot import excel to datagridview in c#.net even though I add a reference using Microsoft.Office.Interlop.Excel , I m using a visual studio 2010 and this is my source code
private
void
button1_Click(
object
sender, EventArgs e)
{
_Application importExcelToDataGridViewAppl;
_Workbook importExcelToDataGridViewWorkbook;
_Worksheet importExcelToDataGridViewWorksheet;
Range importExcelToDataGridViewRange;
try
{
importExcelToDataGridViewAppl =
new
Microsoft.Office.Interop.Excel.Application();
OpenFileDialog importExcelToDataGridViewFileDialog =
new
OpenFileDialog();
importExcelToDataGridViewFileDialog.Title=
"yahoo"
;
importExcelToDataGridViewFileDialog.Filter =
"Choose Excel File | *.xlsx;*.xls;*.xlsm"
;
// importExcelToDataGridViewFileDialog.Title = "Import Excel File";
if
(importExcelToDataGridViewFileDialog.ShowDialog() == DialogResult.OK)
{
importExcelToDataGridViewWorkbook = importExcelToDataGridViewAppl.Workbooks.Open(importExcelToDataGridViewFileDialog.FileName);
importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet;
importExcelToDataGridViewRange = importExcelToDataGridViewWorksheet.UsedRange;
//
for
(
int
excelWorkSheetRowIndex=0;excelWorkSheetRowIndex < importExcelToDataGridViewRange.Rows.Count; excelWorkSheetRowIndex++)
{
dataGridView1.Rows.Add(importExcelToDataGridViewWorksheet.Cells[excelWorkSheetRowIndex,1].Value,);
}
}
}
catch
(Exception ex)
{
}
finally
{
}
}
Reply
Answers (
3
)
how to assign c# class object to a list object after convert?
Null Reference exception object reference not set to instance of an ob