Révision 2343

tmp/org.txm.libs.office/src/org/txm/libs/office/WriteODS.java (revision 2343)
1
package org.txm.libs.office;
2

  
3
import java.io.File;
4
import java.util.ArrayList;
5

  
6
import org.odftoolkit.simple.SpreadsheetDocument;
7
import org.odftoolkit.simple.table.Cell;
8
import org.odftoolkit.simple.table.Row;
9
import org.odftoolkit.simple.table.Table;
10

  
11
public class WriteODS {
12
	
13
	SpreadsheetDocument spreadsheet;
14
	Table table;
15
	File newODSFile;
16
	
17
	public WriteODS(File newODSFile) throws Exception {
18
		this.newODSFile = newODSFile;
19
		this.spreadsheet = SpreadsheetDocument.newSpreadsheetDocument();
20
	}
21
	
22
	public void newTable(String name) {
23
		if (name != null && name.length() > 0) {
24
			this.table = spreadsheet.appendSheet(name);
25
		} else {
26
			this.table = spreadsheet.addTable();
27
		}
28
	}
29
	
30
	/**
31
	 * 
32
	 * @param nRows rows are inserted if nRows > 0
33
	 * @param nColumns columns are inserted if nColumns > 0
34
	 */
35
	public void declareRowsAndColumns(int nRows, int nColumns) {
36
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
37
		if (nRows > 0) {
38
			table.appendRows(nRows);
39
		}
40
		
41
		if (nColumns > 0) {
42
			table.appendColumns(nColumns);
43
		}
44
	}
45
	
46
	protected void writeLine(Object[] line, Row row) {
47
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
48
		for (int iCol = 0; iCol < line.length; iCol++) {
49
			Cell cell = row.getCellByIndex(iCol);
50
			if (cell != null) {
51
				writeCell(cell, line[iCol]);
52
			} else {
53
				throw new IllegalStateException("cannot get col at "+iCol);
54
			}
55
		}
56
	}
57
	
58
	public void writeLine(Object[] line, int iRow) {
59
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
60
		Row row = table.getRowByIndex(iRow);
61
		if (row != null) {
62
			writeLine(line, row);
63
		} else {
64
			throw new IllegalStateException("cannot get row at "+iRow);
65
		}
66
	}
67
	
68
	public void writeLine(ArrayList<?> line, int iRow) {
69
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
70
		Row row = table.getRowByIndex(iRow);
71
		if (row != null) {
72
			writeLine(line.toArray(), row);
73
		} else {
74
			throw new IllegalStateException("cannot get row at "+iRow);
75
		}
76
	}
77
	
78
	public void writeLine(Object[] line) {
79
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
80
		Row row = table.appendRow();
81
		writeLine(line, row);
82
	}
83
	
84
	public void writeLine(ArrayList<?> line) {
85
		if (table == null) throw new IllegalStateException("no table set with 'newtTable(...)'");
86
		Row row = table.appendRow();
87
		writeLine(line.toArray(), row);
88
	}
89
	
90
	public void save() throws Exception {
91
		spreadsheet.save(newODSFile);
92
	}
93
	
94
	public static void write(File newODFFile, Object[][] data) throws Exception {
95
		
96
		if (data.length == 0) throw new IllegalArgumentException("no data to write in " + newODFFile);
97
		
98
		SpreadsheetDocument spreadsheet = SpreadsheetDocument.newSpreadsheetDocument();
99
		Object[] firstLine = data[0];
100
		Table table = spreadsheet.addTable();
101
		table.appendRows(data.length);
102
		table.appendColumns(firstLine.length);
103
		
104
		for (int iRow = 0; iRow < data.length; iRow++) {
105
			Row row = table.appendRow();
106
			Object[] line = data[iRow];
107
			for (int iCol = 0; iCol < line.length; iCol++) {
108
				Cell cell = row.getCellByIndex(iCol);
109
				writeCell(cell, line[iCol]);
110
			}
111
		}
112
		
113
		spreadsheet.save(newODFFile);
114
	}
115
	
116
	public static void write(File newODFFile, ArrayList<ArrayList<?>> data) throws Exception {
117
		
118
		if (data.size() == 0) throw new IllegalArgumentException("no data to write in " + newODFFile);
119
		
120
		SpreadsheetDocument spreadsheet = SpreadsheetDocument.newSpreadsheetDocument();
121
		ArrayList<?> firstLine = data.get(0);
122
		Table table = spreadsheet.addTable();
123
		table.appendRows(data.size());
124
		table.appendColumns(firstLine.size());
125
		
126
		for (int iRow = 0; iRow < data.size(); iRow++) {
127
			Row row = table.appendRow();
128
			ArrayList<?> line = data.get(iRow);
129
			for (int iCol = 0; iCol < line.size(); iCol++) {
130
				Cell cell = row.getCellByIndex(iCol);
131
				writeCell(cell, line.get(iCol));
132
			}
133
		}
134
		
135
		spreadsheet.save(newODFFile);
136
	}
137
	
138
	private static void writeCell(Cell cell, Object object) {
139
		
140
		if (object == null) cell.removeContent();
141
		
142
		if (object instanceof String) {
143
			cell.setStringValue(object.toString());
144
		}
145
		else if (object instanceof Boolean) {
146
			cell.setBooleanValue((Boolean) object);
147
		}
148
		else if (object instanceof Double) {
149
			cell.setDoubleValue((Double) object);
150
		}
151
		else {
152
			cell.setStringValue(object.toString());
153
		}
154
	}
155
	
156
	public static void main(String[] args) throws Exception {
157
		
158
		File file = new File("/home/mdecorde/TEMP/test_writing.ods");
159
		ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
160
		
161
	}
162
}
0 163

  

Formats disponibles : Unified diff