我如何合并2 Excel文件与分隔片1 Excel文件? [英] How do I merge 2 Excel files into one excel file with separated sheets?

查看:131
本文介绍了我如何合并2 Excel文件与分隔片1 Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 Excel文件,我想将它们合并为1个文件有独立的床单。

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

我试图用Microsoft.Office进行合并。 ?Interop.Excel,
,但我不明白如何使用

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

有关叶海亚:

这里得到,我想用不同的文件合并他们的Range方法:

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;
        }
    }



问题在于xlNewSheet不具有任何属性可以得到价值..我怎样才能加入??

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

推荐答案

不能确定的问题是什么?但对于合并Excel文件编程看到一个起点:

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

  • http://www.dotnetspider.com/forum/242869-How-merge-two-excel-sheets-c.aspx
  • http://www.dotnetperls.com/excel

如果你需要更多的信息,然后显示一些代码,请告诉究竟是不是工作..

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

编辑 - 按评论:

EDIT - as per comment:

您可以使用 xlNewSheet.Cells 来获得一定范围内,并使用其 / / 项目属性来更改/添加你想要的任何值 - 即你的情况从 valueArray ...

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...

这篇关于我如何合并2 Excel文件与分隔片1 Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆