阅读Excel并出现错误 [英] Reading Excel and get error

查看:118
本文介绍了阅读Excel并出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简短的代码来读取Excel文件.这是我的代码.

I wrote a short code to read an excel file. Here is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Excel_ref = Microsoft.Office.Interop.Excel;

namespace Excel_Sheet
{
    public partial class Excel : Form
    {
        public Excel()
        {
            InitializeComponent();
        }


        Excel_ref.Application _Excel_App = new Excel_ref.Application();

        public static Excel_ref.Workbook _curr_WorkBook;
        public static Excel_ref.Worksheet _curr_WorkSheet;
        public static Excel_ref.Range _curr_range;
        string _chosen_file = "";

        private void btnGetExcel_Click(object sender, EventArgs e)
        {

            openFileDialog1.Filter = "Excel Files (*.xls)|*.xls|Excel 2007(*xlsx)|*.xlsx";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                _chosen_file = openFileDialog1.FileName;
            }
            if (_chosen_file == string.Empty)
            {
                return;
            }
            txtWorkBook.Text = _chosen_file;
            getExcelData(txtWorkBook.Text);
        }

        private void getExcelData(string get_File)
        {
            FileStream F_stream = File.Open(get_File, FileMode.Open, FileAccess.Read);

            _Excel_App = new Excel_ref.Application();
            _curr_WorkBook = _Excel_App.Workbooks.Open(openFileDialog1.FileName, 
                0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 
                "\t", false, false, 0, true, 1, 0);
            //_curr_WorkBook = _Excel_App.Workbooks.Open(openFileDialog1.FileName,
            //               0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
            //               "\t", false, false, 0, true, 1, 0);
            _curr_WorkSheet = (Excel_ref.Worksheet)_curr_WorkBook.Worksheets.get_Item(1);
            _curr_range = _curr_WorkSheet.UsedRange;

            string str;
            int rCnt = 0, cCnt = 0;
            for (rCnt = 1; rCnt <= _curr_range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= _curr_range.Columns.Count; cCnt++)
                {
                    str = (string)(_curr_range.Cells[rCnt, cCnt] as Excel_ref.Range).Value2;
                    MessageBox.Show(str);
                }
            }
            MessageBox.Show("Read all cell!");
            //Excel_ref.Sheets sheets = _curr_WorkBook.Worksheets;

            //if (get_File.EndsWith(".xlsx"))
            //{

            //}
            
        }
    }
}



当我运行这些代码时,出现了此错误.

Excel_Sheet.exe中出现了类型为"System.Runtime.InteropServices.COMException"的未处理的异常

附加信息:来自HRESULT的异常:0x800A03EC

请帮助我解决此错误.

谢谢,



When I ran these code, I got this error.

An unhandled exception of type ''System.Runtime.InteropServices.COMException'' occurred in Excel_Sheet.exe

Additional information: Exception from HRESULT: 0x800A03EC

Please help me to fix this error.

thanks,

推荐答案

这将有助于知道在何处引发了异常,但是一段代码看起来不正确.

It would help to know where the exception is thrown but a section of the code doesn''t look right.

private void getExcelData(string get_File)
{
    FileStream F_stream = File.Open(get_File, FileMode.Open, FileAccess.Read);

    _Excel_App = new Excel_ref.Application();
    _curr_WorkBook = _Excel_App.Workbooks.Open(openFileDialog1.FileName,
        0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
        "\t", false, false, 0, true, 1, 0);



1)删除File.Open,因为要Excel打开文件.
2)用get_File替换openFileDialog1.FileName.

艾伦.



1) Remove the File.Open because you want Excel to open the file.
2) Replace openFileDialog1.FileName with get_File.

Alan.


您可以使用下面的代码直接将Excel文件读取到数据集.
使用ExcelDataReader库(提供链接).

You can use follwing piece of code for reading Excel file directly to Dataset.
Use ExcelDataReader Library (link provided).

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

// Reading from a binary Excel file (''97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

// Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

// DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();

// Free resources (IExcelDataReader is IDisposable)
excelReader.Close(); 



这是ExcelDataReader的参考.
http://exceldatareader.codeplex.com/



Here is the reference for ExcelDataReader.
http://exceldatareader.codeplex.com/


这篇关于阅读Excel并出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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