1 2 17 18 19 20 package org.apache.poi.hssf.contrib.view; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.net.*; 25 import java.applet.*; 26 import java.io.*; 27 import javax.swing.*; 28 29 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 30 import org.apache.poi.hssf.usermodel.HSSFSheet; 31 import org.apache.poi.hssf.usermodel.HSSFCell; 32 33 43 public class SViewer extends JApplet { 44 private SViewerPanel panel; 45 boolean isStandalone = false; 46 String filename = null; 47 48 49 public String getParameter(String key, String def) { 50 return isStandalone ? System.getProperty(key, def) : 51 (getParameter(key) != null ? getParameter(key) : def); 52 } 53 54 55 public SViewer() { 56 } 57 58 59 public void init() { 60 try { 61 jbInit(); 62 } 63 catch(Exception e) { 64 e.printStackTrace(); 65 System.exit(1); 66 } 67 } 68 69 70 private void jbInit() throws Exception { 71 InputStream i = null; 72 boolean isurl = false; 73 if (filename == null) filename = getParameter("filename"); 74 75 if (filename == null || filename.substring(0,7).equals("http://")) { 76 isurl = true; 77 if (filename == null) filename = getParameter("url"); 78 i = getXLSFromURL(filename); 79 } 80 81 HSSFWorkbook wb = null; 82 if (isurl) { 83 wb = constructWorkbook(i); 84 } else { 85 wb = constructWorkbook(filename); 86 } 87 panel = new SViewerPanel(wb, false); 88 getContentPane().setLayout(new BorderLayout()); 89 getContentPane().add(panel, BorderLayout.CENTER); 90 } 91 92 private HSSFWorkbook constructWorkbook(String filename) throws FileNotFoundException, IOException { 93 HSSFWorkbook wb = null; 94 FileInputStream in = new FileInputStream(filename); 95 wb = new HSSFWorkbook(in); 96 in.close(); 97 return wb; 98 } 99 100 private HSSFWorkbook constructWorkbook(InputStream in) throws IOException { 101 HSSFWorkbook wb = null; 102 103 wb = new HSSFWorkbook(in); 104 in.close(); 105 return wb; 106 } 107 108 109 public void start() { 110 } 111 112 public void stop() { 113 } 114 115 public void destroy() { 116 } 117 118 public String getAppletInfo() { 119 return "Applet Information"; 120 } 121 122 public String [][] getParameterInfo() { 123 return null; 124 } 125 126 130 private InputStream getXLSFromURL(String urlstring) throws MalformedURLException, IOException { 131 URL url = new URL(urlstring); 132 URLConnection uc = url.openConnection(); 133 String field = uc.getHeaderField(0); 134 for (int i=0;field != null; i++) { 135 System.out.println(field); 136 field = uc.getHeaderField(i); 137 } 138 BufferedInputStream is = new BufferedInputStream(uc.getInputStream()); 139 return is; 140 } 141 142 143 144 public static void main(String [] args) { 145 SViewer applet = new SViewer(); 146 applet.isStandalone = true; 147 applet.filename = args[0]; 148 Frame frame; 149 frame = new Frame() { 150 protected void processWindowEvent(WindowEvent e) { 151 super.processWindowEvent(e); 152 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 153 System.exit(0); 154 } 155 } 156 public synchronized void setTitle(String title) { 157 super.setTitle(title); 158 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 159 } 160 }; 161 frame.setTitle("Applet Frame"); 162 frame.add(applet, BorderLayout.CENTER); 163 applet.init(); 164 applet.start(); 165 frame.setSize(400,320); 166 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 167 frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); 168 frame.setVisible(true); 169 } 170 } 171 | Popular Tags |