00001 package net.threebit.utils.sosc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import javax.xml.transform.*;
00023 import javax.xml.transform.stream.*;
00024 import java.sql.*;
00025 import java.io.*;
00026 import java.util.*;
00027 import javax.servlet.http.*;
00028 import javax.servlet.jsp.*;
00029
00030 public class ExcelSheet {
00031
00032 public static final int COLUMN = 0;
00033 public static final int ROW = 1;
00034
00035 private int columns = 0 ;
00036 private StringWriter sw = new StringWriter();
00037 private PrintWriter pw = new PrintWriter(sw);
00038 private String sheetName = "";
00039 private Random random = new Random();
00040 private List items = new ArrayList();
00041
00042
00043
00044 private int xy[] = new int [2];
00045 private int start[] = new int [2];
00046 private int end[] = new int [2];
00047
00048 public String width = "99%";
00049 public String height = "500px";
00050 public String fontName = "Verdana";
00051 public String fontSize = "8";
00052
00053 private boolean rawWrite = false;
00054
00058 public ExcelSheet (String sheetName, String title) throws Exception {
00059 sheetName = sheetName.replaceAll(" ","_");
00060 this.sheetName = sheetName;
00061 setTitle(title);
00062 }
00067 public void setXY ( int width, int height ) {
00068 this.width = width+"%";
00069 this.height = height+"px";
00070 }
00071
00075 public void render (OutputStream out) throws Exception {
00076
00077 PrintWriter ow = new PrintWriter(new OutputStreamWriter(out));
00078 ow.print("<object ");
00079 ow.print(" id=\""+sheetName+"\" ");
00080 ow.print(" classid=\"CLSID:0002E510-0000-0000-C000-000000000046\" ");
00081 ow.print(" style=\"width: "+width+"; height: "+height+";\">");
00082 ow.println("</object>");
00083 ow.println("");
00084 ow.println(sw.getBuffer().toString());
00085 ow.println("");
00086 ow.println("<script language=\"vbs\">");
00087 for (Iterator i = items.iterator(); i.hasNext(); ) {
00088 ow.println(" " + i.next() + "()");
00089 }
00090 ow.println("</script>");
00091 ow.println("");
00092 ow.flush();
00093 }
00094
00095 public void setTitle ( String title ) throws Exception {
00096
00097 title = title.replaceAll(" ","_");
00098 rawWrite(".TitleBar.caption = \"" + title + "\"");
00099 }
00100
00101 public void autofit() throws Exception {
00102 pw.println(sheetName+".ActiveSheet.Range(\"A1:AZ2000\").AutoFitColumns" );
00103 }
00104
00108 public void bold() throws Exception {
00109 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+xy[ROW]+":"+xy[ROW]+"\").Font.Bold = True");
00110 }
00111
00115 public void bold(String range) throws Exception {
00116 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+range+"\").Font.Bold = True");
00117 }
00118
00122 public void font(String font, int size, String range) throws Exception {
00123 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+range+"\").Font.Name = \""+font+"\"");
00124 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+range+"\").Font.Size = "+size+"");
00125 }
00126
00131 public void printCell ( Object o ) throws Exception {
00132 pw.println (sheetName+".ActiveSheet.Cells("+nextColumn()+").Value = \""+o.toString()+"\"" ) ;
00133 }
00134
00135
00139 public int drawTable(String tableName, List rows, String startxy ) throws Exception {
00140 return drawTable(tableName, rows, startxy, true);
00141
00142 }
00143 public int drawTable(String tableName, List rows, String startxy, boolean first ) throws Exception {
00144 if ( first ) items.add( tableName ) ;
00145 tableName = tableName.replace(' ','_');
00146 start[COLUMN] = xy[COLUMN] = ((int) startxy.charAt( COLUMN )) - 64;
00147 start[ROW] = xy[ROW] = Integer.parseInt(startxy.substring(1));
00148
00149 pw.println("<script language=vbs> \nSub "+tableName +"() ") ;
00150 pw.println("With " +sheetName + ".ActiveSheet");
00151 pw.println(".Columns.Font.Name = \"" + fontName + "\"");
00152 pw.println(".Columns.Font.Size = "+ fontSize);
00153 pw.println("End With" ) ;
00154
00155
00156
00157 boolean title = true;
00158 for ( Iterator i = rows.iterator(); i.hasNext(); ) {
00159 ArrayList row = (ArrayList) i.next();
00160 for ( Iterator j = row.iterator(); j.hasNext(); ) {
00161 printCell( j.next() );
00162 nextColumn();
00163 }
00164 if (title) { bold(); title = false; }
00165 nextRow();
00166 }
00167 autofit();
00168 close();
00169 return xy[ROW] ;
00170 }
00171
00181 public int drawTable (String tableName, ResultSet r, String startxy ) throws SQLException,Exception {
00182 return drawTable(tableName, r, startxy, false, 1);
00183 }
00184 public int drawTable(String tableName, ResultSet r, String startxy, boolean sum, int firstNumericRow) throws SQLException,Exception {
00185
00186
00187 tableName = tableName.replaceAll(" ","_");
00188 items.add( tableName ) ;
00189
00190
00191
00192
00193
00194
00195 start[COLUMN] = xy[COLUMN] = ((int) startxy.charAt( COLUMN )) - 64;
00196 start[ROW] = xy[ROW] = Integer.parseInt(startxy.substring(1));
00197
00198
00199 columns = r.getMetaData().getColumnCount() ;
00200 pw.println ("<script language=vbs> \nSub "+tableName +"() ") ;
00201
00202 pw.println("With " +sheetName + ".ActiveSheet");
00203 pw.println(".Columns.Font.Name = \"" + fontName + "\"");
00204 pw.println(".Columns.Font.Size = " + fontSize + " ");
00205 pw.println("End With " ) ;
00206
00207
00208
00209
00210 for ( int j = 1; j<=columns; j++) {
00211 pw.println (sheetName+".ActiveSheet.Cells("+nextColumn()+").Value = \""+r.getMetaData().getColumnName(j)+"\"" ) ;
00212 }
00213
00214 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+xy[ROW]+":"+xy[ROW]+"\").Font.Bold = True");
00215 nextRow();
00216 while (r.next()) {
00217
00218
00219 for (int i = 1 ; i<=columns; i++){
00220 pw.println (" "+sheetName+".ActiveSheet.Cells("+nextColumn()+").Value = \""+r.getString(i)+"\"" ) ;
00221 }
00222 nextRow();
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 autofit();
00246 pw.println (" End Sub\n</script>" ) ;
00247 return (int)xy[ROW] ;
00248 }
00249
00250
00251
00252
00253 public String nextRow() {
00254 xy[COLUMN] = start[COLUMN] ;
00255 return ++xy[ROW] + "," + xy[COLUMN];
00256 }
00257
00258
00259 public String nextColumn() {
00260 return xy[ROW] + "," + xy[COLUMN]++;
00261 }
00262
00266 public void close () throws Exception {
00267 pw.println("End Sub");
00268 pw.println("</script>");
00269 }
00270
00274 public void openManual( String name ) throws Exception {
00275 openManual();
00276 }
00277
00281 public void openManual() throws Exception {
00282 rawWrite = true;
00283 String ran = ""+random.nextLong();
00284 if (ran.charAt(0) == '-') { ran = ran.substring(1,ran.length()); }
00285 String name = "manual" + ran;
00286 items.add( name ) ;
00287 pw.println("<script language=\"vbs\">");
00288 pw.println("Sub "+name +"()");
00289 pw.println("");
00290 }
00291
00295 public void closeManual( ) throws Exception {
00296 pw.println("End Sub");
00297 pw.println("</script>");
00298 rawWrite = false;
00299 }
00300
00308
00309
00313 public void select ( String range ) throws Exception {
00314 rawWrite(".Range(\""+range+"\").Select");
00315 }
00316
00320 public void format( String range, String format ) throws Exception {
00321 rawWrite(".Range(\""+range+"\").Select");
00322 rawWrite(".Selection.NumberFormat = \""+format+"\"");
00323 }
00324
00332 public void writeCell( String cell, String data ) throws Exception {
00333 boolean inRaw = rawWrite;
00334 if (! inRaw) { openManual(); }
00335 pw.println ( sheetName+".ActiveSheet.Cells("+cell+").Value = \"" + data + "\"" ) ;
00336 if (! inRaw) { closeManual(); }
00337 }
00338
00348
00349
00350
00351
00352
00360 public void writeRange( String cell, String data ) throws Exception {
00361 pw.println ( sheetName+".ActiveSheet.Range(\""+cell+"\").Value = \"" + data + "\"" ) ;
00362 }
00363 public String toRange( int row, int column ) {
00364 return ((char) (column+'A'-1)) + (row +"");
00365 }
00366
00370 public void rawWrite (String command ) throws Exception {
00371
00372 boolean inRaw = rawWrite;
00373 if (! inRaw) { openManual(); }
00374 pw.println(sheetName + command);
00375 if (! inRaw) { closeManual(); }
00376 }
00377
00381 public int[] getStart() { return start; }
00382
00386 public int[] getEnd() { return end; }
00387
00388 }