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 33 38 public class CSV 39 { 40 50 public CSV(Workbook w, OutputStream out, String encoding, boolean hide) 51 throws IOException 52 { 53 if (encoding == null || !encoding.equals("UnicodeBig")) 54 { 55 encoding = "UTF8"; 56 } 57 58 try 59 { 60 OutputStreamWriter osw = new OutputStreamWriter (out, encoding); 61 BufferedWriter bw = new BufferedWriter (osw); 62 63 for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) 64 { 65 Sheet s = w.getSheet(sheet); 66 67 if (!(hide && s.getSettings().isHidden())) 68 { 69 bw.write(s.getName()); 70 bw.newLine(); 71 72 Cell[] row = null; 73 74 for (int i = 0 ; i < s.getRows() ; i++) 75 { 76 row = s.getRow(i); 77 78 if (row.length > 0) 79 { 80 if (!(hide && row[0].isHidden())) 81 { 82 bw.write(row[0].getContents()); 83 } 86 87 for (int j = 1; j < row.length; j++) 88 { 89 bw.write(','); 90 if (!(hide && row[j].isHidden())) 91 { 92 bw.write(row[j].getContents()); 93 } 96 } 97 } 98 bw.newLine(); 99 } 100 } 101 } 102 bw.flush(); 103 bw.close(); 104 } 105 catch (UnsupportedEncodingException e) 106 { 107 System.err.println(e.toString()); 108 } 109 } 110 } 111 112 113 114 | Popular Tags |