Ir al contenido principal

Entradas

Angular2 + Bootstrap3

Para incluir Bootstrap 3 en nuestra aplicación de Angular 2 hacemos lo siguiente: 1) Incluimos bootstrap + jquery en package.json npm install --save bootstrap@3 jquery 2) Incluimos el .css en styles.css @import "../node_modules/bootstrap/dist/css/bootstrap.min.css"; 3) Incluimos los .js en el "scripts" de angular-cli.json "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/bootstrap/dist/js/bootstrap.min.js" ]

Cómo actualizar Node.js

Para actualizar Node.js tenemos que ejecutar lo siguiente: npm install -g n npm cache clean -f sudo n stable

Apache POI: Copiar una tabla en Word

En este post vamos a ver cómo podemos copiar una tabla que tenemos en un documento Word plantilla, e incluirlo en nuestro documento final que queremos crear. En este ejemplo, la tabla tiene una primera fila que es la cabecera y otra fila que sería la que tenemos que ir replicando para crear diferentes filas. try (XWPFDocument document = new XWPFDocument(new FileInputStream(documentFile)); XWPFDocument tableDocument = new XWPFDocument(new FileInputStream(tableFile))) { XWPFTable table = document.createTable(); table.removeRow(0); //This default row is not needed XWPFTable tableDocument = tableDocument.getTables().get(0); //Copy the table header table.addRow(tableDocument.getRow(0)); //Add rows XWPFTableRow row = tableDocument.getRow(1); //Copy the row style addRow(table, row, "col1", "col2", "col3"; } } ///... private static void addRow(XWPFTable table, XWPFTableRow row, String... ...

Jackson: Json + Java

Jackson es una librería Java que nos permite mapear ficheros JSON a clases Java y viceversa. Para este ejemplo yo he utilizado Maven, y he incluido la siguiente dependencia: com.fasterxml.jackson.core jackson-databind 2.8.4 Creamos nuestro fichero JSON: { "name": "Test", "number": 3 } Creamos la clase Java que representa a nuestro fichero JSON: public class MyJson { private String name; private int number; @JsonIgnore private String ignoreMe; @JsonCreator public MyJson(@JsonProperty("name") final String name) throws Exception { this.name = name; this.ignoreMe = name.toUpperCase(); } //... El constructor es opcional, sólo lo he utilizado para tener un ejemplo más completo. Y para mapear el fichero JSON a Java hacemos lo siguiente: MyJson myJson = new ObjectMapper().readValue(new File(jsonFilePath), MyJson.class); Para mapear el objeto a un fichero JSON: ...

Java 8: Borrar los ficheros de un directorio

En Java 8, si queremos borrar todos los ficheros regulares de un directorio, exceptuando por ejemplo los "pom.xml", haríamos lo siguiente: Files.walk(directoryPath) .map(Path::toFile) .filter(f -> f.isFile() && !f.getName().equals("pom.xml")) .forEach(f -> { log("Deleting file " + f); f.delete(); });

Generación con Apache FreeMarker

Apache FreeMarker es una librería java que nos permite generar documentos, emails, código, ... a partir de plantillas. Empezamos creando y configurando nuestra instancia de FreeMarker: Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); cfg.setDirectoryForTemplateLoading(new File(myTemplatesFolder)); cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX); cfg.setDefaultEncoding("UTF-8"); cfg.setNumberFormat("computer"); //To iterate maps in templates with entrySet() (eh. [#list configs as conf]) cfg.setObjectWrapper(new BeansWrapperBuilder(Configuration.VERSION_2_3_23).build()); En el directorio donde tenemos las plantillas (en nuestro ejemplo la variable myTemplatesFolder indica el directorio), podemos crear nuestra primera plantilla (" myTemplate.ftl "): [#-- @ftlvariable name="config" type="com.examples.Config" --] ${config.env} [#list config.params as param] ${param} [/#list] Si queremos ...

Apache POI: Unir documentos docx

Si tenemos un documento docx y queremos añadírselo a otro, sólo tenemos que hacer lo siguiente: private void mergeDocuments(XWPFDocument doc, XWPFDocument docToMerge) { int pos = doc.getParagraphs().size() - 1; for (XWPFParagraph par : docToMerge.getParagraphs()) { doc.createParagraph(); doc.setParagraph(par, pos++); } } Yo he utilizado Maven, y para este ejemplo he tenido que incluir las siguientes dependencias: org.apache.poi poi 3.15 org.apache.poi poi-ooxml 3.15