'来自HRESULT的异常:0x800A03EC在使用C#从Excel读取数据时

时间:2018-06-11 12:32:05

标签: c# c#-4.0 excel-interop

我是C#的新手我已经编写了以下代码来从C#读取Excel,并且在读取单元格时会抛出错误。

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;

namespace BasicFramework.DatabankController
{
    public class DatabankController<K,V> : Dictionary<K,V>
    {

        public static Dictionary<String, String> storeValues = new Dictionary<string, string>();
        private static List<DatabankController<String, String>> singleton = null;
        private static Microsoft.Office.Interop.Excel.Workbook myWorkBook;
        private static Microsoft.Office.Interop.Excel.Sheets myWorkSheets;
        private static Microsoft.Office.Interop.Excel.Application excel;
        private DatabankController()
        {

        }
        public static List<DatabankController<String, String>> getInstance(String fileName, String sheetName)
        {
            if (singleton == null)
            {
                singleton = data(fileName, sheetName);

            }
            return singleton;
        }
        private static List<DatabankController<String, String>> data(String filepath, String sheetName)
        {
            List<DatabankController<String, String>> mydata = new List<DatabankController<String,String>>();
            excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = true;
            excel.DisplayAlerts = false;
            string projectPath = Directory.GetParent(Environment.CurrentDirectory).FullName;
            if (projectPath.Contains("bin"))
            {
                projectPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
            }
            string xmlfilepath = projectPath + "\\BasicFramework\\"+filepath+".xlsx";

            myWorkBook = excel.Workbooks.Open(xmlfilepath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            myWorkSheets = myWorkBook.Worksheets;

            Excel.Worksheet xlWorksheet = (Excel.Worksheet)myWorkSheets.get_Item(sheetName);
            int columnCount = xlWorksheet.UsedRange.Columns.Count;
            int rowCount = xlWorksheet.UsedRange.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                DatabankController<String, String> currentHash = new DatabankController<string, string>();
                for(int j = 0;j<= columnCount;j++)
                {
                        if(i>0)
                            currentHash.Add((String)((Excel.Range)xlWorksheet.Cells[i, j]).ToString(), (String)((Excel.Range)xlWorksheet.Cells[i,j]).ToString());
                }
                mydata.Add(currentHash);
            }

            myWorkBook.Close();
            myWorkBook = null;
            excel.Quit();
            excel = null;
            return mydata;
        }
        public static DatabankController<String, String> getdataBankCounterRecord(String FileName,String SheetName,String rule)
        {
            List<DatabankController<String, String>> getValue = getInstance(FileName,SheetName);
            DatabankController<String, String> dbcontroller = null;
            bool flag = false;
            int i = 0;
            foreach(DatabankController<String, String> h in  getValue)
            {
                foreach (String h1 in h.Keys.ToArray())
                {
                    if (h[h1].Contains(rule))
                    {
                        flag = true;
                        break;
                    }
                }
                Console.WriteLine(i);
                if (flag == true)
                {
                    dbcontroller = getValue[i];
                    break;
                }
                i++;
            }
            return dbcontroller;
        }
    }
}

正好在以下行中抛出错误。我的Excel工作表正在使用open()方法打开,但之后它将读取数据失败。任何人都可以帮我这个。

currentHash.Add((String)((Excel.Range)xlWorksheet.Cells[i, j]).ToString(), (String)((Excel.Range)xlWorksheet.Cells[i,j]).ToString());

0 个答案:

没有答案