简体   繁体   中英

Insert 2D array into excel in C# WPF

I want to insert String[][] (2D array) to excel instead of "cell by cell" or by Row.

Another thing: I want to open a template XLS file, write into it and save it with a different name. I tried to search google for 2 days already.

Please help me :-)

(in C# WPF)

You want something like:

Although I think it will only work on jagged arrays and not 2d arrays. Not tested on 2d arrays.

 var startCell = Worksheet.Cells[row, col];
 var endCell = Worksheet.Cells[row, col];
 var writeRange = (Excel.Range)Worksheet.Cells[startCell, endCell];
 writeRange.Value = myArray;

what i finally did is using object[,], and insert ro by row

      xlApp = new Excel.Application();

        xlWorkBook = xlApp.Workbooks.Open(ExcelFile);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        Excel.Range firstCell = xlWorkSheet.get_Range("A1", Type.Missing);

        Excel.Range lastCell = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);


        Excel.Range worksheetCells = xlWorkSheet.get_Range(firstCell, lastCell);

        int rowCount = worksheetCells.Rows.Count;
        int colCount = worksheetCells.Columns.Count;

         object[,] cellFormulas = new String[rowCount, colCount];

         cellFormulas = worksheetCells.Formula as object[,];

       //  String[][] ResultMatrix = new String[rowCount][];



        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Workbooks.Close();'

I can solve the first problem which is:

I want to insert String [] [] (2D array) to excel instead "cell by cell" or by Row.

In first you should convert String[][] to String[,] and then use this (I tested it):

        public static void ExportToExcel(String[,] data, String sheetName, String path)
        {
            var dt = new DataTable();

            for (var row = 0; row < data.GetLength(0); ++row)
            {
                for (var col = 0; col < data.GetLength(1); col++)
                {
                    dt.Rows[row][col] = data[row, col];
                }
            }

            Excel.Application oXL;
            Excel.Workbook oWB;
            Excel.Worksheet oSheet;
            //Excel.Range oRange;

            oXL = new Excel.Application();

            oXL.Visible = true;
            oXL.DisplayAlerts = false;

            oWB = oXL.Workbooks.Add(Missing.Value);

            oSheet = (Excel.Worksheet)oWB.ActiveSheet;
            oSheet.Name = sheetName;

            var rowCount = 1;

            foreach (DataRow dr in dt.Rows)
            {
                rowCount += 1;
                for (var i = 1; i < dt.Columns.Count + 1; i++)
                {
                    if (rowCount == 2)
                    {
                        oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                    }
                    oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
                }
            }

            oSheet = null;
            oWB.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Excel.XlSaveAsAccessMode.xlExclusive,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);
            oWB.Close(Missing.Value, Missing.Value, Missing.Value);
            oWB = null;

            oXL.Quit();

            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

Note: use should add Microsoft.Office.Interop.Excel in your references.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM