You are on page 1of 3

2- Lectura y Escritura en EXCEL: http://www.megavideo.com/?v=UTU36NQ3 ////////////////////////////////////////////////////// import java.io.File; import java.io.IOException; import java.util.

Locale; import import import import import import import import import import import import import jxl.CellView; jxl.Workbook; jxl.WorkbookSettings; jxl.format.UnderlineStyle; jxl.write.Formula; jxl.write.Label; jxl.write.Number; jxl.write.WritableCellFormat; jxl.write.WritableFont; jxl.write.WritableSheet; jxl.write.WritableWorkbook; jxl.write.WriteException; jxl.write.biff.RowsExceededException;

public class NewClass { private WritableCellFormat timesBoldUnderline; private WritableCellFormat times; private String inputFile; public void setOutputFile(String inputFile) { this.inputFile = inputFile; } public void crear() throws IOException, WriteException { File file = new File(inputFile); WorkbookSettings configLibro = new WorkbookSettings(); configLibro.setLocale(new Locale("en", "EN"); WritableWorkbook libroExcel = Workbook.createWorkbook(file, configLibro); libroExcel.createSheet("Practica", 0); WritableSheet hojaLibro = libroExcel.getSheet(0); crearEtiqueta(hojaLibro); crearContenido(hojaLibro); libroExcel.write(); libroExcel.close(); } private void crearEtiqueta(WritableSheet sheet) throws WriteException { // Tipo de letra "Times". WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10); // Tamano de texto dentro de la celda. times = new WritableCellFormat(times10pt); // Autoajuste de contenido en celdas 1. times.setWrap(true);

// Letras con efectos en celdas. WritableFont times10ptBoldUnderline = new WritableFont( WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.SINGLE); timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline); // Autoajuste de contenido en celdas 2. timesBoldUnderline.setWrap(true); CellView cv = new CellView(); cv.setFormat(times); cv.setFormat(timesBoldUnderline); cv.setAutosize(true); // Cabeceras. agregarTitulo(sheet, 0, 0, "Cabecera 1"; agregarTitulo(sheet, 1, 0, "Cabecera 2"; } private void crearContenido(WritableSheet sheet) throws WriteException,RowsExceededException { // Contenido numrico. for (int i = 1; i < 10; i++) { // Columna 1 agregarNumero(sheet, 0, i, i + 10); // Columna 2 agregarNumero(sheet, 1, i, i * i); } // Escritura de frmulas. StringBuffer buf = new StringBuffer(); buf.append("SUM(A2:A10)"; Formula f = new Formula(0, 10, buf.toString()); sheet.addCell(f); buf = new StringBuffer(); buf.append("SUM(B2:B10)"; f = new Formula(1, 10, buf.toString()); sheet.addCell(f); // Textos diversos for (int i = 12; i < 20; i++) { // Columna 1 agregarEtiqueta(sheet, 0, i, "Texto columna 1 " + i); // Columna 2 agregarEtiqueta(sheet, 1, i, "Texto columna 2"; } } private void agregarTitulo(WritableSheet sheet, int column, int row, String s) throws RowsExceededException, WriteException { Label label; label = new Label(column, row, s, timesBoldUnderline); sheet.addCell(label); } private void agregarNumero(WritableSheet sheet, int column, int row, Integer integer) throws WriteException, RowsExceededException {

Number number; number = new Number(column, row, integer, times); sheet.addCell(number); } private void agregarEtiqueta(WritableSheet sheet, int column, int row, String s) throws WriteException, RowsExceededException { Label label; label = new Label(column, row, s, times); sheet.addCell(label); } public static void main(String[] args) throws WriteException, IOException { NewClass test = new NewClass(); test.setOutputFile("C:UsersJerryDesktopprueba.xls"; test.crear(); System.out.println("Almacenado!!"; } } /////////////////////////////////////////////////////

You might also like