import java.util.Calendar; import java.util.Date; import org.simpleods.BorderStyle; import org.simpleods.DateStyle; import org.simpleods.OdsFile; import org.simpleods.SimpleOdsException; import org.simpleods.TableStyle; import org.simpleods.TextStyle; import org.simpleods.Util; /* * SimpleODS - A lightweight java library to create simple OpenOffice spreadsheets * Copyright (C) 2008 Martin Schulz * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ public class Test2 { /** * Create a simple OpenDocument spreadsheet * @param args */ public static void main(String[] args) { System.out.println("Starting file creation"); // Create the document OdsFile odsFile = new OdsFile("/tmp/Test2.ods"); try { Date dt1 = new Date(); // Add one table odsFile.addTable("MyTable"); // Create a border style to be used BorderStyle bsBottom = new BorderStyle("0.06cm","#000000",BorderStyle.BORDER_SOLID,BorderStyle.POSITION_BOTTOM); // Create a TableStyle to be used for the header // Always make sure that for a new TableStyle the sName is unique TableStyle ceColor01 = new TableStyle(TableStyle.STYLE_TABLECELL,"color1",odsFile); // Add the background color ceColor01.setBackgroundColor(Util.COLOR_SkyBlue); // Add a border style to this TableStyle ceColor01.addBorderStyle(bsBottom); // Add a text style for the footer TextStyle tsRedText = new TextStyle("TSRED",odsFile); tsRedText.setFontColor(Util.COLOR_Red); // Add a cell style for the merged cells TableStyle tsMerged = new TableStyle(TableStyle.STYLE_TABLECELL,"MERGED",odsFile); tsMerged.setVerticalAlign(TableStyle.VERTICALALIGN_TOP); tsMerged.setFontWrap(true); // Create a TableStyle to be used for the date cells DateStyle.DATEFORMAT_DDMMYY TableStyle ceDate02 = new TableStyle(TableStyle.STYLE_TABLECELL,"color2",odsFile); ceDate02.setFontColor(Util.COLOR_Chocolate); ceDate02.setBackgroundColor(Util.COLOR_GRAY96); // Add a date style for date cells in this table DateStyle ds01 = new DateStyle("DS01",odsFile); ds01.setDateFormat(DateStyle.DATEFORMAT_DDMMYY); ceDate02.setDataStyle(ds01); // Create a TableStyle to be used for the date cells DateStyle.DATEFORMAT_MMYY TableStyle ceDate03 = new TableStyle(TableStyle.STYLE_TABLECELL,"color3",odsFile); ceDate03.setFontColor(Util.COLOR_Chocolate); ceDate03.setBackgroundColor(Util.COLOR_GRAY96); // Add a date style for date cells in this table DateStyle ds02 = new DateStyle("DS02",odsFile); ds02.setDateFormat(DateStyle.DATEFORMAT_MMYY); ceDate03.setDataStyle(ds02); // Create a TableStyle to be used for the date cells DateStyle.DATEFORMAT_WW TableStyle ceDate04 = new TableStyle(TableStyle.STYLE_TABLECELL,"color4",odsFile); ceDate04.setFontColor(Util.COLOR_Chocolate); ceDate04.setBackgroundColor(Util.COLOR_GRAY96); // Add a date style for date cells in this table DateStyle ds03 = new DateStyle("DS03",odsFile); ds03.setDateFormat(DateStyle.DATEFORMAT_WW); ceDate04.setDataStyle(ds03); // Set some text to the table by using the cell name , 0 is the first table ('MyTable') odsFile.setCell(0, "A1", "Hello world!",ceColor01); // use the TableStyle ceColor for this cell odsFile.setCell(0, "B1", "Cell A1",ceColor01); // use the TableStyle ceColor for this cell odsFile.setCell(0, "C1", "Cell C1",ceColor01); // use the TableStyle ceColor for this cell odsFile.setCell(0, "A2", "Cell A2"); odsFile.setCell(0, "B2", "Cell B2"); odsFile.setCell(0, "C2", "Cell C2"); odsFile.setCell(0, "E1", Calendar.getInstance(),ceDate02); odsFile.setCell(0, "E2", Calendar.getInstance(),ceDate03); odsFile.setCell(0, "E3", Calendar.getInstance(),ceDate04); // Set some text to the table by using the cell coordinate odsFile.setCell(0, 2, 0, "Cell A3"); odsFile.setCell(0, 2, 1, "Cell B3"); odsFile.setCell(0, 2, 2, "Cell C3 Merge two rows and two columns",tsMerged); odsFile.setCellMerge(0, "C3",2,2); Date dt2 = new Date(); System.out.println("Time in msec:["+ (dt2.getTime()-dt1.getTime()) +"]"); } catch (SimpleOdsException e) { e.printStackTrace(); } // Set the table number 0 as active table odsFile.setActiveTable(0); // Save all data odsFile.save(); System.out.println("Finish"); } }