0

I have the next function in c# code:

using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Windows.Controls;
using Excel = Microsoft.Office.Interop.Excel;

public void ToExcel<T>(T[,] array2D, string fullPath)
{
    // file path
    string strFullFilePathNoExt = fullPath + ".xlsx";
    // initializing Excel file & working sheet
    Excel.Application ExcelApp = new Excel.Application();
    Excel._Workbook ExcelBook;
    Excel._Worksheet ExcelSheet;
    ExcelBook = ExcelApp.Workbooks.Add(1);
    ExcelSheet = (Excel._Worksheet)ExcelBook.ActiveSheet;

    for (int i = 0; i < array2D.GetLength(0); i++)
    {
        for (int j = 0; j < array2D.GetLength(1); j++)
        {
            ExcelSheet.Cells[i + 1, j + 1] = array2D[i, j].ToString().Trim();
        }
    }

    // Saving file
    ExcelApp.Visible = false;
    ExcelBook.SaveAs(strFullFilePathNoExt, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
    Excel.XlSaveConflictResolution.xlUserResolution, true,
    Missing.Value, Missing.Value, Missing.Value);
    ExcelBook.Close(strFullFilePathNoExt, Missing.Value, Missing.Value);
    ExcelSheet = null;
    ExcelBook = null;
    ExcelApp = null;
}

Also, I have the next way I use it: I get some string 2D array and want extend it by one table and row both (for titles):

SomeFunction()
{
    var analysis = new AscendingAnalysis();
    string[,] table = analysis.GetPrecedenceRelationTable();

    string[,] printTable = new string[table.GetLength(0) + 1, table.GetLength(1) + 1];
    // Is only needed for table without nulls (just tested)
    for (int i = 0; i < printTable.GetLength(0); i++)
    {
        for (int j = 0; j < printTable.GetLength(1); j++)
        {
            printTable[i, j] = "o";
        }
    }
    // first alternative
    //for (int i = 1; i < printTable.GetLength(0); i++)
    //{
    //  printTable[i, 0] = (i * i).ToString();
    //  printTable[0, i] = (i * i).ToString();
    //}
    //second alternative
    //for (int i = 1; i < printTable.GetLength(0); i++)
    //{
    //  printTable[i, 0] = analysis.UniqueItems[i - 1];
    //  printTable[0, i] = analysis.UniqueItems[i - 1];
    //}
    for (int i = 1; i < printTable.GetLength(0); i++)
    {
        for (int j = 1; j < printTable.GetLength(1); j++)
        {
            printTable[i, j] = table[i - 1, j - 1];
        }
    }

    var doc = new Export();
    doc.ToExcel(printTable, "D:\\Table");
}

So the issue is in commented blocks of code:

  1. When I do not add any of them, code works, but I still need my headers.
  2. When I add first alternative, it works, but I still need no such filling
  3. When I add second alternative, the exception is thrown :

System.Runtime.InteropServices.COMException: "Exception from HRESULT: 0x800A03EC"

inside function, in the loop, when i = 0, j = 52 (I have array 55x55 but there is no matter). So, the function partly works. Explain me, please, why it doesn't work and how to fix it.

Of course, I`m not forced to export data in excel, I could to do it in DataGrid, for example. I just need to show results in table. But as for me, excel is more convenient to use for big tables and imports (It is needed to use this table further in my project).

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
kuprian2
  • 1
  • 2
  • Did you try any of the answers [shown here](https://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range), if not, please do. – rene Dec 23 '17 at 10:48
  • If you only want to create the Excel sheet and don't need any fancy features of excel it self you might try EPPlus or ClosedXML to create the xlsx file. – rene Dec 23 '17 at 10:51
  • @rene, I`ve done manipulations with DCOM config and the problem is not in extenison (xls /xslx). It did not help. – kuprian2 Dec 23 '17 at 10:58
  • @rene, I`ll try, thx – kuprian2 Dec 23 '17 at 10:59
  • @rene, could you make your comment as an answer, I`ll check it as correct. – kuprian2 Dec 23 '17 at 11:36
  • Well, that wouldn't answer the question: *Explain me, please, why it doesn't work and how to fix it.*. Consider a self-answer, which is allowed. – rene Dec 23 '17 at 11:43

0 Answers0