svn-gvsig-desktop / trunk / extensions / extTableExport / src / org / gvsig / tableExport / xls / ExportTableToExcelExtension.java @ 28456
History | View | Annotate | Download (7.04 KB)
1 |
package org.gvsig.tableExport.xls; |
---|---|
2 |
|
3 |
|
4 |
import java.awt.Component; |
5 |
import java.io.File; |
6 |
import java.util.Iterator; |
7 |
import java.util.NoSuchElementException; |
8 |
|
9 |
import javax.swing.JFileChooser; |
10 |
import javax.swing.JOptionPane; |
11 |
|
12 |
import jxl.JXLException; |
13 |
import jxl.Workbook; |
14 |
import jxl.write.Label; |
15 |
import jxl.write.Number; |
16 |
import jxl.write.NumberFormats; |
17 |
import jxl.write.WritableCell; |
18 |
import jxl.write.WritableCellFormat; |
19 |
import jxl.write.WritableSheet; |
20 |
import jxl.write.WritableWorkbook; |
21 |
|
22 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
23 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
24 |
import com.hardcode.gdbms.engine.values.DoubleValue; |
25 |
import com.hardcode.gdbms.engine.values.FloatValue; |
26 |
import com.hardcode.gdbms.engine.values.NullValue; |
27 |
import com.hardcode.gdbms.engine.values.NumericValue; |
28 |
import com.hardcode.gdbms.engine.values.Value; |
29 |
import com.iver.andami.PluginServices; |
30 |
import com.iver.andami.messages.NotificationManager; |
31 |
import com.iver.andami.plugins.Extension; |
32 |
import com.iver.andami.ui.mdiManager.IWindow; |
33 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
34 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
35 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
36 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
37 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
38 |
import com.iver.utiles.GenericFileFilter; |
39 |
|
40 |
public class ExportTableToExcelExtension extends Extension { |
41 |
|
42 |
private WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT); |
43 |
private WritableCellFormat intFormat = new WritableCellFormat (NumberFormats.INTEGER); |
44 |
/**
|
45 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
46 |
*/
|
47 |
public void initialize() { |
48 |
} |
49 |
|
50 |
/**
|
51 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
52 |
*/
|
53 |
public void execute(String actionCommand) { |
54 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
55 |
Table table = (Table) v; |
56 |
try {
|
57 |
exportToXLS(table); |
58 |
} catch (Exception e) { |
59 |
NotificationManager.showMessageError("error_en_el_proceso", e);
|
60 |
} |
61 |
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* DOCUMENT ME!
|
66 |
*/
|
67 |
public void exportToXLS(Table table) throws Exception{ |
68 |
File file = this.askForFileName("xls","Excel"); |
69 |
if (file == null){ |
70 |
return;
|
71 |
} |
72 |
exportToFile(table,file); |
73 |
} |
74 |
|
75 |
public void exportToFile(Table table,File trgfile) throws Exception{ |
76 |
SelectableDataSource source; |
77 |
ITableDefinition orgDef; |
78 |
try {
|
79 |
source = table.getModel().getModelo().getRecordset(); |
80 |
source.start(); |
81 |
orgDef = table.getModel().getModelo().getTableDefinition(); |
82 |
} catch (Exception e) { |
83 |
throw new Exception("Error preparando la fuente", e); // TODO intenacionalizacion?? |
84 |
} |
85 |
|
86 |
|
87 |
File file = new File(trgfile.getAbsoluteFile()+".tmp"); |
88 |
|
89 |
WritableWorkbook workbook = Workbook.createWorkbook(file); |
90 |
WritableSheet sheet = workbook.createSheet("First Sheet", 0); |
91 |
|
92 |
writeHeader(sheet,orgDef); |
93 |
|
94 |
|
95 |
try {
|
96 |
SourceIterator iter = new SourceIterator(source);
|
97 |
Value[] values;
|
98 |
Value value; |
99 |
int row=1; |
100 |
int col;
|
101 |
while (iter.hasNext()){
|
102 |
values = iter.nextValues(); |
103 |
for (col=0;col<values.length;col++){ |
104 |
value = values[col]; |
105 |
if (!(value instanceof NullValue)){ |
106 |
sheet.addCell(getCell(row,col,value)); |
107 |
} |
108 |
} |
109 |
row++; |
110 |
|
111 |
} |
112 |
|
113 |
workbook.write(); |
114 |
|
115 |
workbook.close(); |
116 |
|
117 |
} catch (Exception e) { |
118 |
throw new Exception("Error generando fichero", e); // TODO intenacionalizacion?? |
119 |
|
120 |
} |
121 |
|
122 |
file.renameTo(trgfile); |
123 |
|
124 |
try {
|
125 |
source.stop(); |
126 |
|
127 |
} catch (Exception e) { |
128 |
throw new Exception("Error cerrando ficheros", e); // TODO intenacionalizacion?? |
129 |
} |
130 |
|
131 |
|
132 |
|
133 |
} |
134 |
|
135 |
private WritableCell getCell(int row, int col, Value value) { |
136 |
if (value instanceof NumericValue){ |
137 |
if (value instanceof DoubleValue || value instanceof FloatValue){ |
138 |
return new Number(col,row,((NumericValue)value).doubleValue(),floatFormat); |
139 |
} else {
|
140 |
return new Number(col,row,((NumericValue)value).longValue(),intFormat); |
141 |
} |
142 |
} else{
|
143 |
return new Label(col,row,value.toString()); |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
private void writeHeader(WritableSheet sheet, ITableDefinition orgDef) throws JXLException { |
149 |
FieldDescription[] fields = orgDef.getFieldsDesc();
|
150 |
FieldDescription field; |
151 |
Label cell;
|
152 |
int col;
|
153 |
for (col=0;col<fields.length;col++){ |
154 |
field = fields[col]; |
155 |
cell=new Label(col,0,field.getFieldName()); |
156 |
sheet.addCell(cell); |
157 |
} |
158 |
} |
159 |
|
160 |
private File askForFileName(String ext,String extDescription){ |
161 |
JFileChooser jfc = new JFileChooser(); |
162 |
jfc.addChoosableFileFilter(new GenericFileFilter(ext,
|
163 |
extDescription)); |
164 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
165 |
File file=jfc.getSelectedFile();
|
166 |
if (file == null){ |
167 |
return null; |
168 |
} |
169 |
if (file.exists()){
|
170 |
int resp = JOptionPane.showConfirmDialog( |
171 |
(Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
172 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
173 |
if (resp != JOptionPane.YES_OPTION) { |
174 |
return null; |
175 |
} |
176 |
} |
177 |
String name = file.getAbsolutePath();
|
178 |
if (!name.toLowerCase().endsWith("." +ext.toLowerCase())){ |
179 |
file = new File(name + "."+ext); |
180 |
} |
181 |
return file;
|
182 |
} else{
|
183 |
return null; |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
|
189 |
/**
|
190 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
191 |
*/
|
192 |
public boolean isEnabled() { |
193 |
return true; |
194 |
} |
195 |
|
196 |
/**
|
197 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
198 |
*/
|
199 |
public boolean isVisible() { |
200 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
201 |
|
202 |
if (v == null) { |
203 |
return false; |
204 |
} else if (v instanceof Table) { |
205 |
return true; |
206 |
} else {
|
207 |
return false; |
208 |
} |
209 |
} |
210 |
|
211 |
private class SourceIterator implements Iterator{ |
212 |
private int index; |
213 |
private long count; |
214 |
private FBitSet selection = null; |
215 |
private SelectableDataSource source;
|
216 |
|
217 |
public SourceIterator(SelectableDataSource source) throws DriverException, ReadDriverException{ |
218 |
this.source = source;
|
219 |
if (source.getSelection().cardinality()== 0){ |
220 |
this.selection = null; |
221 |
this.index=0; |
222 |
this.count = source.getRowCount();
|
223 |
} else{
|
224 |
this.selection = source.getSelection();
|
225 |
this.index=selection.nextSetBit(0); |
226 |
this.count = selection.cardinality();
|
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
public void remove() { |
232 |
throw new UnsupportedOperationException(); |
233 |
} |
234 |
|
235 |
public boolean hasNext() { |
236 |
if (this.selection == null) |
237 |
return this.index < this.count; |
238 |
return this.index >= 0; |
239 |
} |
240 |
|
241 |
public Object next() { |
242 |
try {
|
243 |
if (!hasNext()){
|
244 |
throw new NoSuchElementException(); |
245 |
} |
246 |
return this.nextValues(); |
247 |
} catch (DriverException e) {
|
248 |
throw new RuntimeException(e); |
249 |
} catch (ReadDriverException e) {
|
250 |
throw new RuntimeException(e); |
251 |
} |
252 |
} |
253 |
|
254 |
public Value[] nextValues() throws DriverException, ReadDriverException { |
255 |
|
256 |
Value[] values = this.source.getRow(this.index); |
257 |
if (this.selection == null){ |
258 |
this.index++;
|
259 |
} else {
|
260 |
this.index = this.selection.nextSetBit(this.index + 1); |
261 |
} |
262 |
return values;
|
263 |
|
264 |
} |
265 |
|
266 |
public long count(){ |
267 |
return this.count; |
268 |
} |
269 |
|
270 |
} |
271 |
|
272 |
} |