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:
- When I do not add any of them, code works, but I still need my headers.
- When I add first alternative, it works, but I still need no such filling
- 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).