Answer the question
In order to leave comments, you need to log in
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
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 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 questionAsk a Question
731 491 924 answers to any question