How to copy and paste a sheet from one Excel file to another Excel file under construction?
Background
I am developing a functionality that allows the creation of an Excel file, which must be built with a certain number of sheets.
The Excel file to be built consists of 5 sheets, of which 3 must be created from another Excel file.
For understanding purposes of this question, I have named the files this way:
- New File.xlsx: Excel file a arm.
- Template.xlsx: Excel file containing the sheets to be used to add to the file ( new file.xlsx )
I attach the links of the template files.xlsx and new file.xlsx in case you wish to review your content.
From the template file.xlsx only these sheets will be used:
- H3 Custom
- sales
- Data
Implementation
After searching how I can copy and paste another Excel sheet to another Excel file, I found this answer , which provides a source code that I have modified for my purposes:
#region INTENTO #3.1
// Fuente: http://www.codeproject.com/Questions/695539/How-to-copy-data-from-one-excel-sheet-to-another
bool hasErrors = false;
try
{
string ruta_plantilla = @"C:\Plantilla.xlsx";
string ruta_archivo_final = @"C:\Nuevo Archivo.xlsx";
// Instancia de Excel.
Microsoft.Office.Interop.Excel.Application excel
= new Microsoft.Office.Interop.Excel.Application();
// Abrir el excel-plantilla:
Workbook wbSource = excel.Workbooks.Open(ruta_plantilla, 0, false, 1, "", "", false,
XlPlatform.xlWindows, 9, true, false, 0, true, false, false);
// Abrir el archivo donde se guardará todos los datos.
Workbook wbDestination = excel.Workbooks.Open(ruta_archivo_final, 0, false, 1, "", "", false,
XlPlatform.xlWindows, 9, true, false, 0, true, false, false);
// Detectar cuántas hojas tiene.
int cantHojas = wbDestination.Sheets.Count;
// Si son menores a 5...
if (cantHojas < 5)
{
// Agregar las hojas faltantes.
Sheets hojas = null;
Worksheet wrkSh = null;
for (int i = cantHojas; i < 5; i++)
{
hojas = wbDestination.Sheets;
wrkSh = hojas.Add(Type.Missing, wbDestination.Worksheets[i], Type.Missing, Type.Missing);
releaseObject(hojas);
}
}
// Obtener la plantilla de la hoja #3.
Worksheet wkrSh_src_pl3 = wbSource.Sheets[3];
wkrSh_src_pl3.UsedRange.Copy(Type.Missing);
// Copiar en estas hojas.
// Copiarla en la hoja #4 del archivo final.
Worksheet wrkSh_pl3 = wbDestination.Sheets[3]; // Hoja que contiene la plantilla.
wrkSh_pl3.UsedRange.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationNone, Type.Missing, Type.Missing);
wrkSh_pl3.Name = wkrSh_src_pl3.Name;
wrkSh_pl3.Columns.AutoFit();
// Obtener la plantilla de la hoja #4.
Worksheet wkrSh_src_pl4 = wbSource.Sheets[4];
wkrSh_src_pl4.UsedRange.Copy(Type.Missing);
// Copiarla en la hoja #4 del archivo final.
Worksheet wrkSh_pl4 = wbDestination.Sheets[4]; // Hoja que contiene la plantilla.
wrkSh_pl4.UsedRange.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationNone, Type.Missing, Type.Missing);
wrkSh_pl4.Name = wkrSh_src_pl4.Name;
wrkSh_pl4.Columns.AutoFit();
// Obtener la plantilla de la hoja #5.
Worksheet wkrSh_src_pl5 = wbSource.Sheets[5];
wkrSh_src_pl5.UsedRange.Copy(Type.Missing);
// Copiarla en la hoja #5 del archivo final.
Worksheet wrkSh_pl5 = wbDestination.Sheets[5]; // Hoja que contiene la plantilla.
wrkSh_pl5.UsedRange.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationNone, Type.Missing, Type.Missing);
wrkSh_pl5.Name = wkrSh_src_pl5.Name;
wrkSh_pl5.Columns.AutoFit();
// Establecer la primera hoja como "activa".
wbDestination.Sheets[1].Activate();
wbDestination.Save();
wbSource.Close();
excel.Quit();
hasErrors = false;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + SALTO_DE_LINEA + ex.Message, TITULO, MessageBoxButtons.OK, MessageBoxIcon.Error);
RegistrarEventosDelPrograma(DateTime.Now, "Error al copiar hoja a otro excel: " + ex.ToString());
hasErrors = true;
}
#endregion
The Problem with this code is that, when copying sheet 3 called sales do not paste it the same as shown in the template.
the screenshot #1 shows the sheet sales contained in the template file.xlsx :
screenshot #2 shows the sheet sales generated with the old code in the file new file.xlsx :
The other sheets i.e. h3 Custom yy Data they are generated correctly in the new Excel file; but this is not the case for Sheet sales .
My questions are:
- ¿How to copy and paste a sheet from one Excel file to another Excel file under construction?
- What other way can I employ for by programming to copy and paste an Excel sheet to another Excel file?
1 answers
You can do something like this in batch, the file would be called procesar.bat
.
@echo off
@cd "C:\Archivos Excel"
"C:\Program Files\7-Zip\7z.exe" x "C:\Plantilla.xlsx" -o"Ruta1"
"C:\Program Files\7-Zip\7z.exe" x "C:\Nuevo Archivo.xlsx" -o"Ruta2"
del "Ruta2\xl\calcChain.xml"
del "Ruta2\xl\sharedStrings.xml"
copy "Ruta1\xl\calcChain.xml" "Ruta2\xl\calcChain.xml"
copy "Ruta1\xl\sharedStrings.xml" "Ruta2\xl\sharedStrings.xml"
"C:\Program Files\7-Zip\7z.exe" a -t"zip" "Ruta1"
rename Ruta1.zip Resultado.xlsx