"Exception from HRESULT: 0x800A01A8"

本文关键字:quot 0x800A01A8 Exception from HRESULT | 更新日期: 2023-09-27 18:21:45

我写了这段代码来计算excel工作表中填充的行数。它一直工作到得到一定数量的行(而不是总数)。然后出现错误消息"HRESULT异常:0x800A01A8"非常感谢您的帮助

namespace ConsoleApplication1
{
    class ExcelClass
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Excel.Application excelApp = new Excel.Application();  // Creates a new Excel Application
            excelApp.Visible = true;  // Makes Excel visible to the user.

            // The following code opens an existing workbook
            string workbookPath = "D:''RSG_D.xls";  // Add your own path here
            Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
                false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
                false, 0, true, false, false);
            // The following gets the Worksheets collection
            Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            // The following gets Sheet1 for editing
            string currentSheet = "Sheet1";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
            //declare a variable to hold the CurrentCulture  
            System.Globalization.CultureInfo oldCI; 
            //get the old CurrenCulture and set the new, en-US  
            //void SetNewCurrentCulture()  
            //{  
            oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;  
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");  
            //} 
            int rowCounter = 1;
            while ( rowCounter != null)
            {
                Excel.Range countRows = (Excel.Range)excelWorksheet.Cells[rowCounter, 1] as Excel.Range;
                object CountRows = countRows.Value;
                rowCounter++;
                Console.WriteLine(CountRows);
            }
             excelWorkbook.Close(0);
            excelApp.Quit();

            //reset Current Culture back to the originale  
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;  

        }
    }
}

"Exception from HRESULT: 0x800A01A8"

我今晚遇到了完全相同的问题:这是我使用过的代码,它对我来说运行正常:

        Excel.Application oExcel = new Excel.Application();
        //oExcel.Visible = true; (this caused me huge problems
        Excel.Workbook oBook = oExcel.Workbooks.Open(@"C:'Yoink'Birr Castle Demesne     Interactive Map'Birr Castle Demesne Interactive Map'bin'Debug'Red Tree Trail.xlsx");
        Excel.Worksheet oSheet1 = oBook.Worksheets["Red Tree Trail"] as Excel.Worksheet; (use your own worksheet title there)
        Excel.Range rng = oSheet1.get_Range("A1", "AJ51"); (use your own range there
        int rowCount = rng.Rows.Count;
        int colCount = rng.Columns.Count;
        string[,] tsReqs = new string[rowCount, colCount];
        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                string str = rng.Cells[i, j].Text;
                tsReqs[i - 1, j - 1] = str;
            }
        }

我认为你的问题在这一行:

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
            false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
            false, 0, true, false, false);