如何将excel工作表复制到另一个excel工作簿中,而无需在c#winforms中打开excel文件? [英] How to copy excel worksheets Into another excel workbook without opening the excel file in c# winforms?

查看:470
本文介绍了如何将excel工作表复制到另一个excel工作簿中,而无需在c#winforms中打开excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#Windows应用程序中,
我有许多excel工作簿,我想要的是将工作表从excel工作簿复制到单个工作簿。
这是可能的,但我必须打开excel工作簿才能这样做。

In C# windows application, I have many excel workbooks, what i want is to copy the worksheets from the excel workbook to a single workbook. This is possible, but i have to open the excel workbooks in order to do so.

        Excel.Application app = new Excel.Application();

        app.Visible = true;

        app.WindowState = XlWindowState.xlMinimized;

        app.Workbooks.Add("");
        app.Workbooks.Add(@"Path\WorkBook1.xlsx");
        app.Workbooks.Add(@"Path\WorkBook2.xlsx");

        for (int i = 2; i <= app.Workbooks.Count; i++)
        {
            int count = app.Workbooks[i].Worksheets.Count;

            app.Workbooks[i].Activate();
            for (int j = 1; j <= count; j++)
            {
                Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j];

                ws.Select(true);
                ws.Cells.Select();

                Excel.Range sel = (Excel.Range)app.Selection;
                sel.Copy(Type.Missing);

                Excel._Worksheet sheet = (Excel._Worksheet)app.Workbooks[1].Worksheets.Add(
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing
                    );

                sheet.Paste(Type.Missing, Type.Missing);

                sheet.Name = app.Workbooks[i].Worksheets[j].Name;
            }


        }

        app.DisplayAlerts = false;
        app.Workbooks[3].Close();
        app.Workbooks[2].Close();
        app.DisplayAlerts = true;

        Cursor = Cursors.Default;

        MessageBox.Show("Successfully Generated Excel...!", "Excel Tool", MessageBoxButtons.OK, MessageBoxIcon.Information);

是否可以在不打开excel表的情况下复制所有的数据和样式?

Is is possible without opening the excel sheets, copy all the data with their styles ?

推荐答案

要复制工作表及其所有内容和格式,而不选择和复制工作表本身的内容,您可以利用 Worksheet.Copy 。您可以这样使用:

To copy a worksheet and all its contents and formatting without selecting and copying the contents of the worksheet itself you can make use of Worksheet.Copy. You'd use it like so:

Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j];
Excel._Worksheet sheet = (Excel._Worksheet)app.Workbooks[1].Worksheets.Add(
                Type.Missing, Type.Missing, Type.Missing, Type.Missing
                );
ws.Copy(Before: sheet);

然而,如果你的问题实际上是你想要复制的内容工作簿成为一个共同的工作簿,而不会打开文件,那么我不相信这是可能的。您需要打开文件才能访问数据。

If, however, what you actually mean by your question is that you want to copy the contents of the workbooks into one common workbook without ever opening the file, then I don't believe that's possible. You need to open the file to access the data.

这篇关于如何将excel工作表复制到另一个excel工作簿中,而无需在c#winforms中打开excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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