Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ExcelSheet.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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         // internally, represent as char array. 
00043         // The COLUMN is cast to a char and the row is converted to a char. 
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                 // Use a printwriter for writing to the stream
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                 // Write the relevant data to the internal buffer
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                 // ----------------------- start parsing --------------------------//
00156                 // first array is the title. 
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                 // Tablename cant have any spaces in it. 
00187                 tableName = tableName.replaceAll(" ","_");              
00188                 items.add( tableName ) ;
00189 
00190                 // Set xy coordinates.  convert the A1 notation to 1,1 notation. 
00191                 //start[COLUMN] = xy[COLUMN] = (int) startxy.charAt( COLUMN ); 
00192                 //start[ROW] = xy[ROW] = Integer.parseInt(startxy.substring(1));  
00193 
00194                 // TODO catch uppercase lowercase
00195                 start[COLUMN] = xy[COLUMN] = ((int) startxy.charAt( COLUMN )) - 64;  // (int) 'A'; 
00196                 start[ROW] = xy[ROW] = Integer.parseInt(startxy.substring(1));  
00197 
00198                 // We need to set the rows and columns for "+sheetName+" table.  
00199                 columns = r.getMetaData().getColumnCount() ; 
00200                 pw.println ("<script language=vbs> \nSub "+tableName +"() ") ; 
00201                 //pw.println (sheetName+".ActiveSheet.Cells.Clear");  // breaks stuff. 
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                 // pw.println(sheetName + ".TitleBar.caption = \"" + tableName + "\""  ); 
00208 
00209                 // First, do column headers. "+sheetName+" could be much nicer. 
00210                 for ( int j = 1; j<=columns; j++) {
00211                         pw.println (sheetName+".ActiveSheet.Cells("+nextColumn()+").Value = \""+r.getMetaData().getColumnName(j)+"\"" ) ;  
00212                 }
00213                 // Now, bold them, to be nice.  We need to kwow where we are though ... 
00214                 pw.println (sheetName+".ActiveSheet.Cells.Range(\""+xy[ROW]+":"+xy[ROW]+"\").Font.Bold = True"); 
00215                 nextRow(); 
00216                 while (r.next())  { // for each row in the result set
00217                         // iterate over the columns, remembering to quote strings, now we should be able to introspect
00218                         // into the columns to get the datatype and the column header. 
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 //              // Final row. 
00226 //              if ( sum ) {    
00227 //                      //nextColumn(); 
00228 //                      int firstrow = start[ROW]; 
00229 //                      for (int i = 1 ; i<columns; i++){
00230 //                              if ( i < 2 ){// = firstNumericRow ) {
00231 //                                      
00232 //                              }
00233 //                              else { 
00234 //                                      pw.println ( sheetName + ".ActiveSheet.Cells("+toRange(xy[ROW],)+").Value = \"=SUM("+
00235 //                                      toRange(firstrow+1, xy[COLUMN]  ) 
00236 //                                      + ':' + 
00237 //                                      toRange(xy[ROW]-1, xy[COLUMN] ) 
00238 //                                      + ")\""); 
00239 //                              }
00240 //                      }
00241 //              }
00242 //              }
00243 //              
00244 //              // Autofit!  this only works for the cells present at the time its called. 
00245                 autofit();
00246                 pw.println (" End Sub\n</script>" ) ; 
00247                 return (int)xy[ROW] ; 
00248         }
00249 
00250         // TODO:  CHANGE "+sheetName+" to reflect "+sheetName+" notation : 
00251         //
00252         //
00253         public String nextRow() {
00254                 xy[COLUMN] = start[COLUMN] ; 
00255                 return  ++xy[ROW] + "," + xy[COLUMN];  
00256         }
00257 
00258         // Todo! columns go from Z to AA, must take care of this.   
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         // somebody write me. 
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 //      public void formatSelection ( String selection, int rows, String format ) throws Exception {
00349 //              // break the selection apart.                                                                   
00350 //              char column = selection.chartAt(0); 
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                 // open manual mode if need be; if it's already open then don't worry about it.
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 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15