为什么我无法在C#中打开excel文件? [英] Why I cannot open the excel file in C#?

查看:148
本文介绍了为什么我无法在C#中打开excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输出但当我试图点击打开按钮并选择我需要的excel文件时,无法打开文件...



这就是我得到的:

myproject.exe中发生了'System.NullReferenceException'类型的未处理异常



附加信息:对象引用未设置为对象的实例。



过程入口点ucrtbase.terminate无法位于动态链接库api-ms-win-crt- runtime- | 1-1-0.dll。



+ $ exception {System.NullReferenceException:对象引用未设置为对象的实例。

at myproject.Form1.btnopen_Click(Object sender,EventArgs e)位于c:\ Users \toshiba \ Files \ Visual Studio 2012 \Projects\myproject\myproject\Form1.cs:line 33

在System.Windows.Forms.Control.OnClick(EventArgs e)

在System.Windows.Forms.Button.OnMouseUp(MouseE) ventArgs mevent)

在System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons按钮,Int32点击)

在System.Windows.Forms.Control.WndProc(消息& m)

在System.Windows.Forms.ButtonBase.WndProc(消息& m)

在System.Windows.Forms.Button.WndProc(消息& m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)

在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

在System.Windows.Forms.UnsafeNativeMethods。 DispatchMessageW(MSG& msg)

在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)中的

System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)中的

at myproject.Program.Main()in c:\ Users \toshiba \ Files'\\Visual Studio 2012 \Projects\myproject\myproject\Program.cs:第17行<在System.AppDomain._nExecuteAssembly(程序集程序集,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



我尝试了什么:



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];
        }
    }
}

推荐答案

exception {System.NullReferenceException:对象引用未设置为一个对象的实例。

at myproject.Form1.btnopen_Click(Object sender,EventArgs e)in c:\ Users \ toshiba \Documents\Visual Studio 2012 \Projects\myproject \ myproject \Form1.cs:第33行

在System.Windows.Forms.Control.OnClick(EventArgs e)

在System.Windows.Forms.Button。 OnMouseUp(MouseEventArgs mevent)

在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)

在System.Windows.Forms.Control .WndProc(Message& m)

at System.Windows.Forms.ButtonBase.WndProc(Message& m)

at System.Windows.Forms.Button.WndProc(Message& amp; ; m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)

在System.Windows.Forms.NativeWindo w.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,int32 pvLoopData)

在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)

在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)

at myproject.Program.Main()在c:\ Users \toshiba \Documents\Visual Studio 2012 \Projects\myproject\myproject\Program.cs:第17行

at System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System。 Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)

在System.Threading.ThreadHelper.ThreadStart()} System.Ex ception {System.NullReferenceException}

+ ofd {System.Windows.Forms.OpenFileDialog:Title:,FileName:C:\Users\toshiba \Documents\PRODUCT.xlsx} System.Windows .Forms.OpenFileDialog



我尝试过:



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];
        }
    }
}


首先使用调试器找出问题的确切位置:

在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


问题可能与您的文件系统或已经打开文件的应用程序有关,但没有错误消息及其出现的确切行,我们甚至无法开始猜测将会治愈它的原因!所以使用调试器,检查所有变量内容,看看你能找到什么。

如果你不能发现问题,那么请回到我们的实际信息,我们会看到我们可以做什么 - 但我们不能访问您的文件系统,并且您需要运行应用程序才能找到问题。
Start by using the debugger to find out exactly where the problem is:
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

The problem is probably something to do with your file system or your app having the file open already, but without the error message and the exact line it occurs on we can't even begin to guess what will cure it! So use the debugger, check all the variable contents, and see what you can find.
If you can't spot the problem from that, then come back to us with actual information and we'll see what we can do - but we don;t have access to your file system, and you need your app running to find the problem.


您使用的是错误的阅读器类型:使用IExcelDataReader [ ^ ]。
You are using the wrong reader type: Using IExcelDataReader[^].


这篇关于为什么我无法在C#中打开excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆