i have the output but when i tried to click at the open button and select the excel file that i need, the file cannot be opened...
and this is what i get :
An unhandled exception of type 'System.NullReferenceException' occurred in myproject.exe
Additional information: Object reference not set to an instance of an object.
the procedure entry point ucrtbase.terminate could not be located in the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll.
+ $exception {System.NullReferenceException: Object reference not set to an instance of an object.
at myproject.Form1.btnopen_Click(Object sender, EventArgs e) in c:\Users\toshiba\Documents\Visual Studio 2012\Projects\myproject\myproject\Form1.cs:line 33
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at myproject.Program.Main() in c:\Users\toshiba\Documents\Visual Studio 2012\Projects\myproject\myproject\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()} System.Exception {System.NullReferenceException}
+ ofd {System.Windows.Forms.OpenFileDialog: Title: , FileName: C:\Users\toshiba\Documents\PRODUCT.xlsx} System.Windows.Forms.OpenFileDialog
What I have tried:
using Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace myproject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet result;
private void btnopen_Click(object sender, EventArgs e)
{
using (OpenFileDialog opfd = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx", ValidateNames = true })
{
if (opfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(opfd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
Sheetname.Items.Clear();
foreach (DataTable dt in result.Tables)
Sheetname.Items.Add(dt.TableName);
reader.Close();
}
}
}
private void Sheetname_SelectedIndexChanged(object sender, EventArgs e)
{
dataview.DataSource = result.Tables[Sheetname.SelectedIndex];
}
}
}