1 19 20 package jxl.demo; 21 22 import java.io.File ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.BufferedWriter ; 26 import java.io.UnsupportedEncodingException ; 27 import java.io.IOException ; 28 29 import jxl.Workbook; 30 import jxl.Sheet; 31 import jxl.Cell; 32 import jxl.CellType; 33 import jxl.format.CellFormat; 34 import jxl.format.Format; 35 import jxl.format.Border; 36 import jxl.format.BorderLineStyle; 37 import jxl.format.Colour; 38 import jxl.format.Pattern; 39 import jxl.format.Font; 40 41 46 public class XML 47 { 48 51 private OutputStream out; 52 53 56 private String encoding; 57 58 61 private Workbook workbook; 62 63 74 public XML(Workbook w, OutputStream out, String enc, boolean f) 75 throws IOException 76 { 77 encoding = enc; 78 workbook = w; 79 this.out = out; 80 81 if (encoding == null || !encoding.equals("UnicodeBig")) 82 { 83 encoding = "UTF8"; 84 } 85 86 if (f) 87 { 88 writeFormattedXML(); 89 } 90 else 91 { 92 writeXML(); 93 } 94 95 } 96 97 100 private void writeXML() throws IOException 101 { 102 try 103 { 104 OutputStreamWriter osw = new OutputStreamWriter (out, encoding); 105 BufferedWriter bw = new BufferedWriter (osw); 106 107 bw.write("<?xml version=\"1.0\" ?>"); 108 bw.newLine(); 109 bw.write("<!DOCTYPE workbook SYSTEM \"workbook.dtd\">"); 110 bw.newLine(); 111 bw.newLine(); 112 bw.write("<workbook>"); 113 bw.newLine(); 114 for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++) 115 { 116 Sheet s = workbook.getSheet(sheet); 117 118 bw.write(" <sheet>"); 119 bw.newLine(); 120 bw.write(" <name><![CDATA["+s.getName()+"]]></name>"); 121 bw.newLine(); 122 123 Cell[] row = null; 124 125 for (int i = 0 ; i < s.getRows() ; i++) 126 { 127 bw.write(" <row number=\"" + i + "\">"); 128 bw.newLine(); 129 row = s.getRow(i); 130 131 for (int j = 0 ; j < row.length; j++) 132 { 133 if (row[j].getType() != CellType.EMPTY) 134 { 135 bw.write(" <col number=\"" + j + "\">"); 136 bw.write("<![CDATA["+row[j].getContents()+"]]>"); 137 bw.write("</col>"); 138 bw.newLine(); 139 } 140 } 141 bw.write(" </row>"); 142 bw.newLine(); 143 } 144 bw.write(" </sheet>"); 145 bw.newLine(); 146 } 147 148 bw.write("</workbook>"); 149 bw.newLine(); 150 151 bw.flush(); 152 bw.close(); 153 } 154 catch (UnsupportedEncodingException e) 155 { 156 System.err.println(e.toString()); 157 } 158 } 159 160 163 private void writeFormattedXML() throws IOException 164 { 165 try 166 { 167 OutputStreamWriter osw = new OutputStreamWriter (out, encoding); 168 BufferedWriter bw = new BufferedWriter (osw); 169 170 bw.write("<?xml version=\"1.0\" ?>"); 171 bw.newLine(); 172 bw.write("<!DOCTYPE workbook SYSTEM \"formatworkbook.dtd\">"); 173 bw.newLine(); 174 bw.newLine(); 175 bw.write("<workbook>"); 176 bw.newLine(); 177 for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++) 178 { 179 Sheet s = workbook.getSheet(sheet); 180 181 bw.write(" <sheet>"); 182 bw.newLine(); 183 bw.write(" <name><![CDATA["+s.getName()+"]]></name>"); 184 bw.newLine(); 185 186 Cell[] row = null; 187 CellFormat format = null; 188 Font font = null; 189 190 for (int i = 0 ; i < s.getRows() ; i++) 191 { 192 bw.write(" <row number=\"" + i + "\">"); 193 bw.newLine(); 194 row = s.getRow(i); 195 196 for (int j = 0 ; j < row.length; j++) 197 { 198 if ((row[j].getType() != CellType.EMPTY) || 200 (row[j].getCellFormat() != null)) 201 { 202 format = row[j].getCellFormat(); 203 bw.write(" <col number=\"" + j + "\">"); 204 bw.newLine(); 205 bw.write(" <data>"); 206 bw.write("<![CDATA["+row[j].getContents()+"]]>"); 207 bw.write("</data>"); 208 bw.newLine(); 209 210 if (row[j].getCellFormat() != null) 211 { 212 bw.write(" <format wrap=\"" + format.getWrap() + "\""); 213 bw.newLine(); 214 bw.write(" align=\"" + 215 format.getAlignment().getDescription() + "\""); 216 bw.newLine(); 217 bw.write(" valign=\"" + 218 format.getVerticalAlignment().getDescription() + "\""); 219 bw.newLine(); 220 bw.write(" orientation=\"" + 221 format.getOrientation().getDescription() + "\""); 222 bw.write(">"); 223 bw.newLine(); 224 225 font = format.getFont(); 227 bw.write(" <font name=\"" + font.getName() + "\""); 228 bw.newLine(); 229 bw.write(" point_size=\"" + 230 font.getPointSize() + "\""); 231 bw.newLine(); 232 bw.write(" bold_weight=\"" + 233 font.getBoldWeight() + "\""); 234 bw.newLine(); 235 bw.write(" italic=\"" + font.isItalic() + "\""); 236 bw.newLine(); 237 bw.write(" underline=\"" + 238 font.getUnderlineStyle().getDescription() + "\""); 239 bw.newLine(); 240 bw.write(" colour=\"" + 241 font.getColour().getDescription() + "\""); 242 bw.newLine(); 243 bw.write(" script=\"" + 244 font.getScriptStyle().getDescription() + "\""); 245 bw.write(" />"); 246 bw.newLine(); 247 248 249 if (format.getBackgroundColour() != Colour.DEFAULT_BACKGROUND || 251 format.getPattern() != Pattern.NONE) 252 { 253 bw.write(" <background colour=\"" + 254 format.getBackgroundColour().getDescription() + "\""); 255 bw.newLine(); 256 bw.write(" pattern=\"" + 257 format.getPattern().getDescription() + "\""); 258 bw.write(" />"); 259 bw.newLine(); 260 } 261 262 263 if (format.getBorder(Border.TOP ) != BorderLineStyle.NONE || 265 format.getBorder(Border.BOTTOM) != BorderLineStyle.NONE || 266 format.getBorder(Border.LEFT) != BorderLineStyle.NONE || 267 format.getBorder(Border.RIGHT) != BorderLineStyle.NONE) 268 { 269 270 bw.write(" <border top=\"" + 271 format.getBorder(Border.TOP).getDescription() + "\""); 272 bw.newLine(); 273 bw.write(" bottom=\"" + 274 format.getBorder(Border.BOTTOM).getDescription() + 275 "\""); 276 bw.newLine(); 277 bw.write(" left=\"" + 278 format.getBorder(Border.LEFT).getDescription() + "\""); 279 bw.newLine(); 280 bw.write(" right=\"" + 281 format.getBorder(Border.RIGHT).getDescription() + "\""); 282 bw.write(" />"); 283 bw.newLine(); 284 } 285 286 if (!format.getFormat().getFormatString().equals("")) 288 { 289 bw.write(" <format_string string=\""); 290 bw.write(format.getFormat().getFormatString()); 291 bw.write("\" />"); 292 bw.newLine(); 293 } 294 295 bw.write(" </format>"); 296 bw.newLine(); 297 } 298 299 bw.write(" </col>"); 300 bw.newLine(); 301 } 302 } 303 bw.write(" </row>"); 304 bw.newLine(); 305 } 306 bw.write(" </sheet>"); 307 bw.newLine(); 308 } 309 310 bw.write("</workbook>"); 311 bw.newLine(); 312 313 bw.flush(); 314 bw.close(); 315 } 316 catch (UnsupportedEncodingException e) 317 { 318 System.err.println(e.toString()); 319 } 320 } 321 322 } 323 324 325 326 327 328 329 | Popular Tags |