S
S
Slavik Sharov2015-11-03 15:39:35
C++ / C#
Slavik Sharov, 2015-11-03 15:39:35

How to merge several sheets of excel file into one?

In general, the essence is this in an excel file of about 100 sheets, in each sheet you need to copy a certain piece and paste everything into another excel file in one sheet, I got such a non-working shit code

class Program
    {
        static void Main(string[] args)
        {
            Excel.Application input = new Excel.Application();
            Excel.Application output = new Excel.Application();
            input.Workbooks.Open(@"C:\Users\sharov\Desktop\main.xls");
            output.Workbooks.Open(@"C:\Users\sharov\Desktop\temp.xlsx");
            Excel.Worksheet outSheet = (Excel.Worksheet)output.ActiveWorkbook.Worksheets[1];
            int i = 1;
            foreach (Excel.Worksheet sheet in input.ActiveWorkbook.Worksheets)
            {
                Excel.Range excelcells = sheet.get_Range("J7","DW63");
                foreach(Excel.ListRow row in excelcells)
                    outSheet.Rows[i++] = row;
            }

            input.Quit();
            output.ActiveWorkbook.Saved = true;
            output.Quit();
        }

    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2015-11-04
@spark36

Firstly, there is no need to open two Excel - open spreadsheets in one and work with them. Workbooks.Open just returns you a Workbook object.
From the source, select Range and do Copy
. In the resulting one, go to the end and do Paste or PasteSpetial.

I
Ilya, 2015-11-03
@Gorily

I used to work with documents through Microsoft.Office.Interop.Excel, I didn't like it. For each document, there is an instance of Excel in memory, everything is tossing and turning, incomprehensible errors.
Now I use EPPlus, I have enough with my head. Look here: https://epplus.codeplex.com/ , https://github.com/ylatuya/EPPlus
Examples here: https://epplus.codeplex.com/wikipage?title=FAQ&ref...
The library is intuitive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question