Iniciar Sesión

Ver la Versión Completa : [Duda] Como cambiar de hoja en una hoja de calculo Excel


Roberto
25 de marzo de 2026, 16:44
Estimados coboleros.

Estoy tratando de escribir datos en una tabla Excel que tiene varias hojas pero no logro poder cambiar de una hoja a otra.

Lo que quiero es escribir la información de la provincia 1 en la hoja 1 y los de la provincia 2 en la hoja 2 y así con el resto de las provincias.

Gracias por su ayuda.

Gusaiello
25 de marzo de 2026, 19:51
Ejemplos varios :

IDENTIFICATION DIVISION.
PROGRAM-ID. EXCEL.
*Nota Siempre cerrar el excel desde Archivo Salir
*----------------------------------------------------------------*
*Agregar un comentario
* inquire hoja1 @cells::@item(wrow-n 1) = hrange.
* modify hrange @value = wnum.
* modify hrange AddComment "Comentario ?????????".
*--------------------- OTROS ----------------------------------*
*Salto de Pagina en el Excel
* inquire hoja1 @cells::@item(wrow-n 1) = hrange.
* modify hrange @PageBreak xlPageBreakManual.
*----------------------------------------------------------------*
* Es mejor colocar AutoFit() que ColumnWidth
* modify hoja1 @range("A1")::ColumnWidth = 6.
*----------------------------------------------------------------*
*Centrados
* inquire hoja1 @Range(ColRow) = hrange.
* modify hrange @horizontalalignment=XlRight.
* horizontalalignment=XlCenter
* horizontalalignment=XlLeft
* horizontalalignment=XlRight
*----------------------------------------------------------------*
*Ejemplo Suma en la celda C5 la Fila 4
* modify hexcelapp
* @Range("C5")::Formula = "=SUM(4:4)"
*----------------------------------------------------------------*
*Ejemplo Suma usando variables
* 01 Sumar.
* 02 filler pic x(05) value "=SUM(".
* 02 wdes-l pic xx value spaces.
* 02 wdes-n pic 9(05) value 0.
* 02 filler pic x value ":".
* 02 whas-l pic xx value spaces.
* 02 whas-n pic 9(05) value 0.
* 02 filler pic x(01) value ")".
* 01 ColRow1.
* 02 wcol pic xx value spaces.
* 02 wrow pic 9(05) value 0.
*Resultado de la Suma
* move " C" to wcol.
* add 1 to wrow.
* modify hoja1 @Range(ColRow1)::Formula = Sumar.
*----------------------------------------------------------------*
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
special-names.
copy "D:\clientes\PRUEBAS\ej-act\def\excel.def".
.
INPUT-OUTPUT SECTION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*----------------------------------------------------------------*
copy "..\def\acucobol.def".
copy "..\def\fonts.def".
*----------------------------------------------------------------*
01 excel-objects-hojas.
03 hexcelapp handle of application of excel.
03 hexcelwkb handle of workbook of excel.
03 hrange handle of range of excel.
03 hoja1 handle of worksheet of excel.
03 hoja2 handle of worksheet of excel.
03 hoja3 handle of worksheet of excel.
03 hoja4 handle of worksheet of excel.
03 hoja5 handle of worksheet of excel.
03 hoja6 handle of worksheet of excel.
03 hoja7 handle of worksheet of excel.
03 hoja8 handle of worksheet of excel.
03 hoja9 handle of worksheet of excel.
03 hoja10 handle of worksheet of excel.
03 hoja11 handle of worksheet of excel.
03 hoja12 handle of worksheet of excel.
*----------------------------------------------------------------*
01 ColRow.
02 wcol.
03 wcol-l pic xx value spaces.
03 wcol-n pic 9(05) value 0.
03 filler pic x value ":".
03 wrow-l pic xx value spaces.
03 wrow-n pic 9(05) value 0.
*----------------------------------------------------------------*
01 wLeftMargin pic 9(10)v99 value 0.
01 wRightMargin pic 9(10)v99 value 0.
01 wTopMargin pic 9(10)v99 value 0.
01 wBottomMargin pic 9(10)v99 value 0.
01 wHeaderMargin pic 9(10)v99 value 0.
01 wFooterMargin pic 9(10)v99 value 0.
*----------------------------------------------------------------*
*Formato Fecha luego el excel muestra dd/mm/aaa
01 wfecha.
02 wmes pic 99 value 0.
02 filler pic x value "/".
02 wdia pic 99 value 0.
02 filler pic x value "/".
02 wani pic 9999 value 0.
*----------------------------------------------------------------*
*Formato Hora (hh:mm:ss)
01 whora.
02 whh pic 99 value 0.
02 filler pic x value ":".
02 wmm pic 99 value 0.
02 filler pic x value ":".
02 wss pic 99 value 0.
*----------------------------------------------------------------*
*Formato Hora (hhh:mm:ss)
01 wwhora.
02 wwhh pic 999 value 0.
02 filler pic x value ":".
02 wwmm pic 99 value 0.
02 filler pic x value ":".
02 wwss pic 99 value 0.
*----------------------------------------------------------------*
77 wnum pic 9(10) value zero.
77 wape pic x(60) value spaces.
77 wimp pic 9(15)v99 value zeros.
*----------------------------------------------------------------*
PROCEDURE DIVISION.
INICIO.
*----------------------------------------------------------------*
Create Application of @Excel Handle in Hexcelapp.
*----------------------------------------------------------------*
*Cantidad de Hojas
modify hexcelapp @SheetsInNewWorkBook = 2.
modify hexcelapp workbooks::add() giving hexcelwkb.
*----------------------------------------------------------------*
*Coloco Nombre a la Hoja 1
inquire hexcelwkb worksheets::item(1) in hoja1.
modify hoja1 @Name = "Hoja Uno".
*----------------------------------------------------------------*
*Anulo Valores en Cero Hoja 1
modify hoja1
application::ActiveWindow::DisplayZeros = 0.
*----------------------------------------------------------------*
*Anulo Linea de Division Hoja 1
modify hoja1
application::ActiveWindow::DisplayGridlines = 0.
*----------------------------------------------------------------*
*Coloco Nombre a la Hoja 2
inquire hexcelwkb worksheets::item(2) in hoja2.
modify hoja2 @Name = "Hoja Dos".
*----------------------------------------------------------------*
*Activo Hoja 2
modify hoja2 @Activate = ().
*----------------------------------------------------------------*
*Anulo Valores en Cero Hoja 2
modify hoja2
application::ActiveWindow::DisplayZeros = 0.
*----------------------------------------------------------------*
*Anulo Linea de Division Hoja 2
modify hoja2
application::ActiveWindow::DisplayGridlines = 0.
*----------------------------------------------------------------*
*Activo Hoja 1
modify hoja1 @Activate = ().
*----------------------------------------------------------------*
*Desactivo la Hoja de Calculo mientras se Genera
modify hexcelapp @Visible = 0.
*----------------------------------------------------------------*
* Titulo En hoja 1 *
*----------------------------------------------------------------*
inquire hoja1 @cells::@item(1 1) = hrange.
modify hrange @value = "EJEMPLO ACUCOBOL - EXCEL".
destroy hrange.
*----------------------------------------------------------------*
*Combinar y Centrar el Titulo en Hoja 1
inquire hoja1 @Range("A1:F1") = hrange.
modify hrange @AutoFormat = xlRangeAutoFormatSimple.
modify hrange @Font::Size = 20.
modify hrange @Font::ColorIndex = 5.
destroy hrange.
*----------------------------------------------------------------*
* Subtitulos en Hoja 1 *
*----------------------------------------------------------------*
inquire hoja1 @cells::@item(2 1) = hrange.
modify hrange @value = "Numero".
destroy hrange.
inquire hoja1 @cells::@item(2 2) = hrange.
modify hrange @value = "Apellido y Nombre".
destroy hrange.
inquire hoja1 @cells::@item(2 3) = hrange.
modify hrange @value = "Fecha".
destroy hrange.
inquire hoja1 @cells::@item(2 4) = hrange.
modify hrange @value = "Hora".
destroy hrange.
inquire hoja1 @cells::@item(2 5) = hrange.
modify hrange @value = "Hora".
destroy hrange.
inquire hoja1 @cells::@item(2 6) = hrange.
modify hrange @value = "Importe".
destroy hrange.
*----------------------------------------------------------------*
*Formato subtitulo hoja 1
inquire hoja1 @Range("A2:F2") = hrange.
modify hrange @AutoFormat =xlRangeAutoFormat3DEffects2.
modify hrange @Font::Size = 12.
modify hrange @Font::ColorIndex = 5.
destroy hrange.
*----------------------------------------------------------------*
* Subtitulos en Hoja 2 *
*----------------------------------------------------------------*
inquire hoja2 @cells::@item(2 1) = hrange.
modify hrange @value = "Numero".
destroy hrange.
inquire hoja2 @cells::@item(2 2) = hrange.
modify hrange @value = "Apellido y Nombre".
destroy hrange.
inquire hoja2 @cells::@item(2 3) = hrange.
modify hrange @value = "Fecha".
destroy hrange.
inquire hoja2 @cells::@item(2 4) = hrange.
modify hrange @value = "Hora".
destroy hrange.
inquire hoja2 @cells::@item(2 5) = hrange.
modify hrange @value = "Hora".
destroy hrange.
inquire hoja2 @cells::@item(2 6) = hrange.
modify hrange @value = "Importe".
destroy hrange.
*----------------------------------------------------------------*
*Formato subtitulo hoja 2
inquire hoja2 @Range("A2:F2") = hrange.
modify hrange @AutoFormat =xlRangeAutoFormat3DEffects2.
modify hrange @Font::Size = 10.
modify hrange @Font::ColorIndex = 7.
destroy hrange.
*----------------------------------------------------------------*
*Formato de la Celda Hora en hoja 1
modify hoja1 @Range("D:D")::NumberFormat = ("hh:mm:ss").
*----------------------------------------------------------------*
*Formato de la Celda Hora en hoja 1
modify hoja1 @range("E:E")::NumberFormat = ("[h]:mm:ss").
*----------------------------------------------------------------*
*Formato de la Celda Importe en hoja 1
modify hoja1 @range("F:F")::NumberFormat = ("#.##0,00").
*----------------------------------------------------------------*

*Coloco contador de linea en dos por los titulos
move 2 to wrow-n.

*----------------------------------------------------------------*
* Cargo Variables Hoja 1
move 1234567890 to wnum. move all "A" to wape.
move 159357.25 to wimp. move 12 to wmes.
move 20 to wdia. move 2007 to wani.
move 10 to whh. move 20 to wmm.
move 25 to wss. move 120 to wwhh.
move 30 to wwmm. move 20 to wwss.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*Contador
add 1 to wrow-n.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
inquire hoja1 @cells::@item(wrow-n 1) = hrange.
modify hrange @value = wnum.
modify hrange AddComment "Comentario".
destroy hrange.

inquire hoja1 @cells::@item(wrow-n 2) = hrange.
modify hrange @value = wape.
destroy hrange.

inquire hoja1 @cells::@item(wrow-n 3) = hrange.
modify hrange @value = wfecha.
destroy hrange.

inquire hoja1 @cells::@item(wrow-n 4) = hrange.
modify hrange @value = whora.
destroy hrange.

inquire hoja1 @cells::@item(wrow-n 5) = hrange.
modify hrange @value = wwhora.
destroy hrange.

inquire hoja1 @cells::@item(wrow-n 6) = hrange.
modify hrange @value = wimp.
destroy hrange.
*----------------------------------------------------------------*
* Cargo Variables Hoja 2
move 0987654321 to wnum. move all "B" to wape.
move 124578.55 to wimp. move 10 to wmes.
move 15 to wdia. move 2006 to wani.
move 12 to whh. move 30 to wmm.
move 22 to wss. move 130 to wwhh.
move 22 to wwmm. move 02 to wwss.
*----------------------------------------------------------------*
inquire hoja2 @cells::@item(wrow-n 1) = hrange.
modify hrange @value = wnum.
destroy hrange.

inquire hoja2 @cells::@item(wrow-n 2) = hrange.
modify hrange @value = wape.
destroy hrange.

inquire hoja2 @cells::@item(wrow-n 3) = hrange.
modify hrange @value = wfecha.
destroy hrange.

inquire hoja2 @cells::@item(wrow-n 4) = hrange.
modify hrange @value = whora.
destroy hrange.

inquire hoja2 @cells::@item(wrow-n 5) = hrange.
modify hrange @value = wwhora.
destroy hrange.

inquire hoja2 @cells::@item(wrow-n 6) = hrange.
modify hrange @value = wimp.
destroy hrange.
*----------------------------------------------------------------*
*Ejemplo Suma en la celda B5 la Columna F
modify hexcelapp @Range("B5")::Formula = "=SUM(F:F)".
*----------------------------------------------------------------*

*----------------------------------------------------------------*
*Borde Hoja 1 ----> (ColRow)
move " A" to wcol-l.
move 6 to wcol-n.
move " F" to wrow-l.

inquire hoja1 @Range(ColRow) = hrange.
modify hrange @Borders::LineStyle = xlContinuous
@Borders::Weight = xlMedium
@Borders::Colorindex = 46

modify hrange @Font::Size = 8.
destroy hrange.
*----------------------------------------------------------------*
* inquire hoja1 @Range("A7:F7") = hrange.
* modify hrange @AutoFormat = xlRangeAutoformatSimple
* modify hrange @Font::Size = 12.
* modify hrange @Font::ColorIndex = 5.
* destroy hrange.
*----------------------------------------------------------------*
*Activo Hoja 1
modify hoja1 @Activate = ().
*----------------------------------------------------------------*
move 0 to wRightMargin.
move 0 to wTopMargin.
move 0 to wHeaderMargin.
move 60 to wBottomMargin.
move 10 to wLeftMargin.
move 0 to wFooterMargin.
*----------------- Hoja 1 ---------------------------------------*
*Parametros Area de Impresion (PrintArea = ColRow)
move " A" to wcol-l.
move 1 to wcol-n.
move " F" to wrow-l.
*----------------------------------------------------------------*
modify hoja1
*Imprime Hoja 1 Orientacion Horinzontal
PageSetup::Orientation = xlLandscape
*Configuro los Margenes
PageSetup::LeftMargin wLeftMargin
PageSetup::RightMargin wRightMargin
PageSetup::TopMargin wTopMargin
PageSetup::BottomMargin wBottomMargin
PageSetup::HeaderMargin wHeaderMargin
PageSetup::FooterMargin wFooterMargin
*Activo Tama¤o del Papel Oficio hoja 1
* PageSetup::PaperSize xlPaperLegal
*Activo Titulos
PageSetup::PrintTitleRows("1:2")
*Activo Fecha y Numero de Pagina
PageSetup::LeftFooter("&F")
PageSetup::RightFooter("&P de &#")
*Ajuste de Escala
PageSetup::Zoom = 100
*Area de Impresion
PageSetup::PrintArea = ColRow.
*----------------------------------------------------------------*
*Activo Imprimir
* modify hoja1 printout().
*----------------------------------------------------------------*

*----------------------------------------------------------------*
*Activo Hoja 2
modify hoja2 @Activate = ().
*----------------------------------------------------------------*
*Borde Hoja 2 ----> (ColRow)
move " A" to wcol-l.
move 2 to wcol-n.
move " F" to wrow-l.

inquire hoja2 @Range(ColRow) = hrange.
modify hrange @Borders::LineStyle = xlEdgeLeft.
modify hrange @Font::Size = 8.
destroy hrange.
*----------------- Hoja 2 ---------------------------------------*
*Parametros Area de Impresion (PrintArea = ColRow)
move " A" to wcol-l.
move 1 to wcol-n.
move " F" to wrow-l.
*----------------------------------------------------------------*
modify hoja2
PageSetup::Orientation = xlLandscape
PageSetup::LeftMargin wLeftMargin
PageSetup::RightMargin wRightMargin
PageSetup::TopMargin wTopMargin
PageSetup::BottomMargin wBottomMargin
PageSetup::HeaderMargin wHeaderMargin
PageSetup::FooterMargin wFooterMargin
* PageSetup::PaperSize xlPaperLegal
PageSetup::PrintTitleRows("1:2")
PageSetup::LeftFooter("&F")
PageSetup::RightFooter("&P")
PageSetup::Zoom = 90
PageSetup::PrintArea = ColRow.
*----------------------------------------------------------------*
* modify hoja2 printout().
*----------------------------------------------------------------*
*Ajustar a la seleccion Hoja 1
inquire hoja1 @Range("A:AF") = hrange.
modify hrange Columns::AutoFit().
*----------------------------------------------------------------*
*Ajustar a la seleccion Hoja 2
inquire hoja2 @Range("A:AF") = hrange.
modify hrange Columns::AutoFit().
*----------------------------------------------------------------*
*Activo Hoja 1
modify hoja1 @Activate = ().
*----------------------------------------------------------------*
*Muestro Excel
modify hexcelapp @Visible = 1.
*----------------------------------------------------------------*
*FIN PROGRAMA,
destroy hoja2.
destroy hoja1.
* modify hexcelwkb @close(0).
destroy hexcelwkb.
* modify hexcelapp @quit().
destroy hexcelapp.
*----------------------------------------------------------------*
STOP RUN.



Post añadido a las 15:51. Post anterior a las 15:50

@Roberto, Espero que sirva.

Roberto
1 de abril de 2026, 15:12
Gusaiello.

Ante todo pedirte disculpas por no haberte respondido antes pero es que solo tengo internet cuando vengo los miércoles al trabajo.

Esto que tu me mandas esta hecho en POWERCOBOL?.

Gusaiello
1 de abril de 2026, 19:07
@Roberto, No, es AcuCobol.
Y no es necesario disculparse, todos tenemos nuestras ocupaciones y nuestros problemas.
Lamento no tener el procedimiento en Power, pero sospecho que la base debe ser mas o menos lo mismo.

Fito
1 de abril de 2026, 20:55
Hola:

Te paso un manuel hecho por Josber. Ahí tiene cosas muy interesantes.

Saludos

Fito...

Kuk
1 de abril de 2026, 21:04
@Roberto, si utilizas el control CmExcel que viene con PowerCOBOL, hasta donde yo sé, no es posible. Solo trata la primera hoja (o lo mismo la que se ve al abrir el fichero, no me acuerdo).

Tenes que pasar por COM/OLE, que te da control absoluto:

Exportación a Excel en OLE2 - Cobol Foro (https://www.cobolforo.es/showthread.php?t=202-Exportaci%F3n-a-Excel-en-OLE2)
Exportar CmListview en Excel - Cobol Foro (https://www.cobolforo.es/showthread.php?t=50-Exportar-CmListview-en-Excel)

Roberto
1 de abril de 2026, 21:15
@Kuk si lo estoy haciendo CmExcel y logre cambiar de hoja pero la forma no me gusta pues para cambiar de hoja tengo que hacer lo siguiente.

INVOKE CmExcel1 "SaveBook" USING RETURNING ReturnValue
INVOKE CmExcel1 "CloseBook"
add 1 to ExcelSheetIndx
INVOKE CmExcel1 "OpenBook" USING ExcelBookName ExcelSheetIndx
Gracias