1

I've 2 Excel files, and I want to merge them into 1 file with separate sheets..

I trying to perform the merging with Microsoft.Office.Interop.Excel, but I don't understand how to use that?

for Yahia:

here the methods for get a Range that i want to merge them with different file:

internal object[,] GetValues(string filename)
    {
        object[,] values = new object[0, 0];
        try
        {
            Workbook workBook = _excelApp.Workbooks.Open(filename,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);

            values = ExcelScanIntenal(workBook);

            workBook.Close(false, filename, null);
            Marshal.ReleaseComObject(workBook);
        }
        catch
        {
        }
        return values;
    }

    private object[,] ExcelScanIntenal(Workbook workBookIn)
    {
        object[,] valueArray = new object[0, 0];
        Worksheet sheet = (Worksheet)workBookIn.Sheets[1];

        Range excelRange = sheet.UsedRange;
        valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
        return valueArray;
    }

and here i want to merge the values:

   internal void AddWorksheetToExcelWorkbook(string filename, string worksheetName, object[,] valueArray)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = null;
        Workbook xlWorkbook = null;
        Sheets xlSheets = null;
        Worksheet xlNewSheet = null;

        try
        {
            xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
                return;

            xlWorkbook = xlApp.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            xlSheets = xlWorkbook.Sheets as Sheets;

            xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
            xlNewSheet.Name = worksheetName;

            xlWorkbook.Save();
            xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
            xlApp.Quit();
        }
        finally
        {
            Marshal.ReleaseComObject(xlNewSheet);
            Marshal.ReleaseComObject(xlSheets);
            Marshal.ReleaseComObject(xlWorkbook);
            Marshal.ReleaseComObject(xlApp);
            xlApp = null;
        }
    }

the problem is that xlNewSheet isn't have any property that can to get the values.. how can i to add??

David Michaeli
  • 777
  • 4
  • 11
  • 22

1 Answers1

1

Not really sure what the question is... but for merging excel files programmatically see for a starting point:

IF you need more information then show some code please and tell what exactly isn't working...

EDIT - as per comment:

You can use xlNewSheet.Cells to get a Range and use its Columns/Rows/Item properties change/add any values you want - i.e. in your case from valueArray...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • here i get the Range that i want to merge to another file: – David Michaeli Aug 28 '11 at 22:37
  • i tried to us xlNewSheet.Cells.Insert or xlNewSheet.Cells.set_Item or xlNewSheet.Cells.set_Value but always get errors.. can u explain more? thanks.. – David Michaeli Aug 29 '11 at 06:17
  • when i using "xlNewSheet.Cells.set_Value(valueArray, Type.Missing);" i get Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) – David Michaeli Aug 29 '11 at 21:51
  • ohh, now i understand: for (int row = 1; row < valueArray.GetLength(0)/2 + 1; row++) { for (int col = 1; col < valueArray.GetLength(1)/2 + 1; col++) { object o = valueArray[row, col]; xlNewSheet.Cells.set_Item(row, col, valueArray[row, col]); } } – David Michaeli Aug 29 '11 at 22:15