first commit

This commit is contained in:
MaddoScientisto 2026-03-14 20:04:39 +01:00
commit 4d332ef662
27586 changed files with 3281783 additions and 0 deletions

View file

@ -0,0 +1,257 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import jxl.WorkbookSettings;
import jxl.biff.Type;
import jxl.read.biff.BiffException;
import jxl.read.biff.BiffRecordReader;
import jxl.read.biff.Record;
class BiffDump {
private BufferedWriter writer;
private BiffRecordReader reader;
private HashMap recordNames;
private int xfIndex;
private int fontIndex;
private int bofs;
private static final int bytesPerLine = 16;
public BiffDump(File file, OutputStream os) throws IOException, BiffException {
this.writer = new BufferedWriter(new OutputStreamWriter(os));
FileInputStream fis = new FileInputStream(file);
jxl.read.biff.File f = new jxl.read.biff.File(fis, new WorkbookSettings());
this.reader = new BiffRecordReader(f);
buildNameHash();
dump();
this.writer.flush();
this.writer.close();
fis.close();
}
private void buildNameHash() {
this.recordNames = new HashMap(50);
this.recordNames.put(Type.BOF, "BOF");
this.recordNames.put(Type.EOF, "EOF");
this.recordNames.put(Type.FONT, "FONT");
this.recordNames.put(Type.SST, "SST");
this.recordNames.put(Type.LABELSST, "LABELSST");
this.recordNames.put(Type.WRITEACCESS, "WRITEACCESS");
this.recordNames.put(Type.FORMULA, "FORMULA");
this.recordNames.put(Type.FORMULA2, "FORMULA");
this.recordNames.put(Type.XF, "XF");
this.recordNames.put(Type.MULRK, "MULRK");
this.recordNames.put(Type.NUMBER, "NUMBER");
this.recordNames.put(Type.BOUNDSHEET, "BOUNDSHEET");
this.recordNames.put(Type.CONTINUE, "CONTINUE");
this.recordNames.put(Type.FORMAT, "FORMAT");
this.recordNames.put(Type.EXTERNSHEET, "EXTERNSHEET");
this.recordNames.put(Type.INDEX, "INDEX");
this.recordNames.put(Type.DIMENSION, "DIMENSION");
this.recordNames.put(Type.ROW, "ROW");
this.recordNames.put(Type.DBCELL, "DBCELL");
this.recordNames.put(Type.BLANK, "BLANK");
this.recordNames.put(Type.MULBLANK, "MULBLANK");
this.recordNames.put(Type.RK, "RK");
this.recordNames.put(Type.RK2, "RK");
this.recordNames.put(Type.COLINFO, "COLINFO");
this.recordNames.put(Type.LABEL, "LABEL");
this.recordNames.put(Type.SHAREDFORMULA, "SHAREDFORMULA");
this.recordNames.put(Type.CODEPAGE, "CODEPAGE");
this.recordNames.put(Type.WINDOW1, "WINDOW1");
this.recordNames.put(Type.WINDOW2, "WINDOW2");
this.recordNames.put(Type.MERGEDCELLS, "MERGEDCELLS");
this.recordNames.put(Type.HLINK, "HLINK");
this.recordNames.put(Type.HEADER, "HEADER");
this.recordNames.put(Type.FOOTER, "FOOTER");
this.recordNames.put(Type.INTERFACEHDR, "INTERFACEHDR");
this.recordNames.put(Type.MMS, "MMS");
this.recordNames.put(Type.INTERFACEEND, "INTERFACEEND");
this.recordNames.put(Type.DSF, "DSF");
this.recordNames.put(Type.FNGROUPCOUNT, "FNGROUPCOUNT");
this.recordNames.put(Type.COUNTRY, "COUNTRY");
this.recordNames.put(Type.TABID, "TABID");
this.recordNames.put(Type.PROTECT, "PROTECT");
this.recordNames.put(Type.SCENPROTECT, "SCENPROTECT");
this.recordNames.put(Type.OBJPROTECT, "OBJPROTECT");
this.recordNames.put(Type.WINDOWPROTECT, "WINDOWPROTECT");
this.recordNames.put(Type.PASSWORD, "PASSWORD");
this.recordNames.put(Type.PROT4REV, "PROT4REV");
this.recordNames.put(Type.PROT4REVPASS, "PROT4REVPASS");
this.recordNames.put(Type.BACKUP, "BACKUP");
this.recordNames.put(Type.HIDEOBJ, "HIDEOBJ");
this.recordNames.put(Type.NINETEENFOUR, "1904");
this.recordNames.put(Type.PRECISION, "PRECISION");
this.recordNames.put(Type.BOOKBOOL, "BOOKBOOL");
this.recordNames.put(Type.STYLE, "STYLE");
this.recordNames.put(Type.EXTSST, "EXTSST");
this.recordNames.put(Type.REFRESHALL, "REFRESHALL");
this.recordNames.put(Type.CALCMODE, "CALCMODE");
this.recordNames.put(Type.CALCCOUNT, "CALCCOUNT");
this.recordNames.put(Type.NAME, "NAME");
this.recordNames.put(Type.MSODRAWINGGROUP, "MSODRAWINGGROUP");
this.recordNames.put(Type.MSODRAWING, "MSODRAWING");
this.recordNames.put(Type.OBJ, "OBJ");
this.recordNames.put(Type.USESELFS, "USESELFS");
this.recordNames.put(Type.SUPBOOK, "SUPBOOK");
this.recordNames.put(Type.LEFTMARGIN, "LEFTMARGIN");
this.recordNames.put(Type.RIGHTMARGIN, "RIGHTMARGIN");
this.recordNames.put(Type.TOPMARGIN, "TOPMARGIN");
this.recordNames.put(Type.BOTTOMMARGIN, "BOTTOMMARGIN");
this.recordNames.put(Type.HCENTER, "HCENTER");
this.recordNames.put(Type.VCENTER, "VCENTER");
this.recordNames.put(Type.ITERATION, "ITERATION");
this.recordNames.put(Type.DELTA, "DELTA");
this.recordNames.put(Type.SAVERECALC, "SAVERECALC");
this.recordNames.put(Type.PRINTHEADERS, "PRINTHEADERS");
this.recordNames.put(Type.PRINTGRIDLINES, "PRINTGRIDLINES");
this.recordNames.put(Type.SETUP, "SETUP");
this.recordNames.put(Type.SELECTION, "SELECTION");
this.recordNames.put(Type.STRING, "STRING");
this.recordNames.put(Type.FONTX, "FONTX");
this.recordNames.put(Type.IFMT, "IFMT");
this.recordNames.put(Type.WSBOOL, "WSBOOL");
this.recordNames.put(Type.GRIDSET, "GRIDSET");
this.recordNames.put(Type.REFMODE, "REFMODE");
this.recordNames.put(Type.GUTS, "GUTS");
this.recordNames.put(Type.EXTERNNAME, "EXTERNNAME");
this.recordNames.put(Type.FBI, "FBI");
this.recordNames.put(Type.CRN, "CRN");
this.recordNames.put(Type.HORIZONTALPAGEBREAKS, "HORIZONTALPAGEBREAKS");
this.recordNames.put(Type.VERTICALPAGEBREAKS, "VERTICALPAGEBREAKS");
this.recordNames.put(Type.DEFAULTROWHEIGHT, "DEFAULTROWHEIGHT");
this.recordNames.put(Type.TEMPLATE, "TEMPLATE");
this.recordNames.put(Type.PANE, "PANE");
this.recordNames.put(Type.SCL, "SCL");
this.recordNames.put(Type.PALETTE, "PALETTE");
this.recordNames.put(Type.PLS, "PLS");
this.recordNames.put(Type.OBJPROJ, "OBJPROJ");
this.recordNames.put(Type.DEFCOLWIDTH, "DEFCOLWIDTH");
this.recordNames.put(Type.ARRAY, "ARRAY");
this.recordNames.put(Type.WEIRD1, "WEIRD1");
this.recordNames.put(Type.BOOLERR, "BOOLERR");
this.recordNames.put(Type.SORT, "SORT");
this.recordNames.put(Type.BUTTONPROPERTYSET, "BUTTONPROPERTYSET");
this.recordNames.put(Type.NOTE, "NOTE");
this.recordNames.put(Type.TXO, "TXO");
this.recordNames.put(Type.DV, "DV");
this.recordNames.put(Type.DVAL, "DVAL");
this.recordNames.put(Type.SERIES, "SERIES");
this.recordNames.put(Type.SERIESLIST, "SERIESLIST");
this.recordNames.put(Type.SBASEREF, "SBASEREF");
this.recordNames.put(Type.CONDFMT, "CONDFMT");
this.recordNames.put(Type.CF, "CF");
this.recordNames.put(Type.FILTERMODE, "FILTERMODE");
this.recordNames.put(Type.AUTOFILTER, "AUTOFILTER");
this.recordNames.put(Type.AUTOFILTERINFO, "AUTOFILTERINFO");
this.recordNames.put(Type.XCT, "XCT");
this.recordNames.put(Type.UNKNOWN, "???");
}
private void dump() throws IOException {
Record r = null;
boolean cont = true;
while (this.reader.hasNext() && cont) {
r = this.reader.next();
cont = writeRecord(r);
}
}
private boolean writeRecord(Record r) throws IOException {
boolean cont = true;
int pos = this.reader.getPos();
int code = r.getCode();
if (this.bofs == 0)
cont = (r.getType() == Type.BOF);
if (!cont)
return cont;
if (r.getType() == Type.BOF)
this.bofs++;
if (r.getType() == Type.EOF)
this.bofs--;
StringBuffer buf = new StringBuffer();
writeSixDigitValue(pos, buf);
buf.append(" [");
buf.append(this.recordNames.get(r.getType()));
buf.append("]");
buf.append(" (0x");
buf.append(Integer.toHexString(code));
buf.append(")");
if (code == Type.XF.value) {
buf.append(" (0x");
buf.append(Integer.toHexString(this.xfIndex));
buf.append(")");
this.xfIndex++;
}
if (code == Type.FONT.value) {
if (this.fontIndex == 4)
this.fontIndex++;
buf.append(" (0x");
buf.append(Integer.toHexString(this.fontIndex));
buf.append(")");
this.fontIndex++;
}
this.writer.write(buf.toString());
this.writer.newLine();
byte[] standardData = new byte[4];
standardData[0] = (byte)(code & 0xFF);
standardData[1] = (byte)((code & 0xFF00) >> 8);
standardData[2] = (byte)(r.getLength() & 0xFF);
standardData[3] = (byte)((r.getLength() & 0xFF00) >> 8);
byte[] recordData = r.getData();
byte[] data = new byte[standardData.length + recordData.length];
System.arraycopy(standardData, 0, data, 0, standardData.length);
System.arraycopy(recordData, 0, data, standardData.length, recordData.length);
int byteCount = 0;
int lineBytes = 0;
while (byteCount < data.length) {
buf = new StringBuffer();
writeSixDigitValue(pos + byteCount, buf);
buf.append(" ");
lineBytes = Math.min(16, data.length - byteCount);
for (int j = 0; j < lineBytes; j++) {
writeByte(data[j + byteCount], buf);
buf.append(' ');
}
if (lineBytes < 16)
for (int k = 0; k < 16 - lineBytes; k++)
buf.append(" ");
buf.append(" ");
for (int i = 0; i < lineBytes; i++) {
char c = (char)data[i + byteCount];
if (c < ' ' || c > 'z')
c = '.';
buf.append(c);
}
byteCount += lineBytes;
this.writer.write(buf.toString());
this.writer.newLine();
}
return cont;
}
private void writeSixDigitValue(int pos, StringBuffer buf) {
String val = Integer.toHexString(pos);
for (int i = 6; i > val.length(); i--)
buf.append('0');
buf.append(val);
}
private void writeByte(byte val, StringBuffer buf) {
String sv = Integer.toHexString(val & 0xFF);
if (sv.length() == 1)
buf.append('0');
buf.append(sv);
}
}

View file

@ -0,0 +1,46 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class CSV {
public CSV(Workbook w, OutputStream out, String encoding, boolean hide) throws IOException {
if (encoding == null || !encoding.equals("UnicodeBig"))
encoding = "UTF8";
try {
OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
BufferedWriter bw = new BufferedWriter(osw);
for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) {
Sheet s = w.getSheet(sheet);
if (!hide || !s.getSettings().isHidden()) {
bw.write("*** " + s.getName() + " ****");
bw.newLine();
Cell[] row = null;
for (int i = 0; i < s.getRows(); i++) {
row = s.getRow(i);
if (row.length > 0) {
if (!hide || !row[0].isHidden())
bw.write(row[0].getContents());
for (int j = 1; j < row.length; j++) {
bw.write(44);
if (!hide || !row[j].isHidden())
bw.write(row[j].getContents());
}
}
bw.newLine();
}
}
}
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}

View file

@ -0,0 +1,198 @@
package jxl.demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import jxl.Cell;
import jxl.Range;
import jxl.Workbook;
import jxl.common.Logger;
public class Demo {
private static final int CSVFormat = 13;
private static final int XMLFormat = 14;
private static Logger logger = Logger.getLogger(Demo.class);
private static void displayHelp() {
System.err.println("Command format: Demo [-unicode] [-csv] [-hide] excelfile");
System.err.println(" Demo -xml [-format] excelfile");
System.err.println(" Demo -readwrite|-rw excelfile output");
System.err.println(" Demo -biffdump | -bd | -wa | -write | -formulas | -features | -escher | -escherdg excelfile");
System.err.println(" Demo -ps excelfile [property] [output]");
System.err.println(" Demo -version | -logtest | -h | -help");
}
public static void main(String[] args) {
if (args.length == 0) {
displayHelp();
System.exit(1);
}
if (args[0].equals("-help") || args[0].equals("-h")) {
displayHelp();
System.exit(1);
}
if (args[0].equals("-version")) {
System.out.println("v" + Workbook.getVersion());
System.exit(0);
}
if (args[0].equals("-logtest")) {
logger.debug("A sample \"debug\" message");
logger.info("A sample \"info\" message");
logger.warn("A sample \"warning\" message");
logger.error("A sample \"error\" message");
logger.fatal("A sample \"fatal\" message");
System.exit(0);
}
boolean write = false;
boolean readwrite = false;
boolean formulas = false;
boolean biffdump = false;
boolean jxlversion = false;
boolean propertysets = false;
boolean features = false;
boolean escher = false;
boolean escherdg = false;
String file = args[0];
String outputFile = null;
String propertySet = null;
if (args[0].equals("-write")) {
write = true;
file = args[1];
} else if (args[0].equals("-formulas")) {
formulas = true;
file = args[1];
} else if (args[0].equals("-features")) {
features = true;
file = args[1];
} else if (args[0].equals("-escher")) {
escher = true;
file = args[1];
} else if (args[0].equals("-escherdg")) {
escherdg = true;
file = args[1];
} else if (args[0].equals("-biffdump") || args[0].equals("-bd")) {
biffdump = true;
file = args[1];
} else if (args[0].equals("-wa")) {
jxlversion = true;
file = args[1];
} else if (args[0].equals("-ps")) {
propertysets = true;
file = args[1];
if (args.length > 2)
propertySet = args[2];
if (args.length == 4)
outputFile = args[3];
} else if (args[0].equals("-readwrite") || args[0].equals("-rw")) {
readwrite = true;
file = args[1];
outputFile = args[2];
} else {
file = args[args.length - 1];
}
String encoding = "UTF8";
int format = 13;
boolean formatInfo = false;
boolean hideCells = false;
if (!write && !readwrite && !formulas && !biffdump && !jxlversion && !propertysets && !features && !escher && !escherdg)
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals("-unicode")) {
encoding = "UnicodeBig";
} else if (args[i].equals("-xml")) {
format = 14;
} else if (args[i].equals("-csv")) {
format = 13;
} else if (args[i].equals("-format")) {
formatInfo = true;
} else if (args[i].equals("-hide")) {
hideCells = true;
} else {
System.err.println("Command format: CSV [-unicode] [-xml|-csv] excelfile");
System.exit(1);
}
}
try {
if (write) {
Write w = new Write(file);
w.write();
} else if (readwrite) {
ReadWrite rw = new ReadWrite(file, outputFile);
rw.readWrite();
} else if (formulas) {
Workbook w = Workbook.getWorkbook(new File(file));
Formulas f = new Formulas(w, System.out, encoding);
w.close();
} else if (features) {
Workbook w = Workbook.getWorkbook(new File(file));
Features f = new Features(w, System.out, encoding);
w.close();
} else if (escher) {
Workbook w = Workbook.getWorkbook(new File(file));
Escher f = new Escher(w, System.out, encoding);
w.close();
} else if (escherdg) {
Workbook w = Workbook.getWorkbook(new File(file));
EscherDrawingGroup f = new EscherDrawingGroup(w, System.out, encoding);
w.close();
} else if (biffdump) {
BiffDump bd = new BiffDump(new File(file), System.out);
} else if (jxlversion) {
WriteAccess bd = new WriteAccess(new File(file));
} else if (propertysets) {
OutputStream os = System.out;
if (outputFile != null)
os = new FileOutputStream(outputFile);
PropertySetsReader psr = new PropertySetsReader(new File(file), propertySet, os);
} else {
Workbook w = Workbook.getWorkbook(new File(file));
if (format == 13) {
CSV csv = new CSV(w, System.out, encoding, hideCells);
} else if (format == 14) {
XML xml = new XML(w, System.out, encoding, formatInfo);
}
w.close();
}
} catch (Throwable t) {
System.out.println(t.toString());
t.printStackTrace();
}
}
private static void findTest(Workbook w) {
logger.info("Find test");
Cell c = w.findCellByName("named1");
if (c != null)
logger.info("named1 contents: " + c.getContents());
c = w.findCellByName("named2");
if (c != null)
logger.info("named2 contents: " + c.getContents());
c = w.findCellByName("namedrange");
if (c != null)
logger.info("named2 contents: " + c.getContents());
Range[] range = w.findByName("namedrange");
if (range != null) {
c = range[0].getTopLeft();
logger.info("namedrange top left contents: " + c.getContents());
c = range[0].getBottomRight();
logger.info("namedrange bottom right contents: " + c.getContents());
}
range = w.findByName("nonadjacentrange");
if (range != null)
for (int i = 0; i < range.length; i++) {
c = range[i].getTopLeft();
logger.info("nonadjacent top left contents: " + c.getContents());
c = range[i].getBottomRight();
logger.info("nonadjacent bottom right contents: " + c.getContents());
}
range = w.findByName("horizontalnonadjacentrange");
if (range != null)
for (int i = 0; i < range.length; i++) {
c = range[i].getTopLeft();
logger.info("horizontalnonadjacent top left contents: " + c.getContents());
c = range[i].getBottomRight();
logger.info("horizontalnonadjacent bottom right contents: " + c.getContents());
}
}
}

View file

@ -0,0 +1,40 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import jxl.Workbook;
import jxl.biff.drawing.DrawingData;
import jxl.biff.drawing.EscherDisplay;
import jxl.read.biff.SheetImpl;
public class Escher {
public Escher(Workbook w, OutputStream out, String encoding) throws IOException {
if (encoding == null || !encoding.equals("UnicodeBig"))
encoding = "UTF8";
try {
OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
BufferedWriter bw = new BufferedWriter(osw);
for (int i = 0; i < w.getNumberOfSheets(); i++) {
SheetImpl s = (SheetImpl)w.getSheet(i);
bw.write(s.getName());
bw.newLine();
bw.newLine();
DrawingData dd = s.getDrawingData();
if (dd != null) {
EscherDisplay ed = new EscherDisplay(dd, bw);
ed.display();
}
bw.newLine();
bw.newLine();
bw.flush();
}
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}

View file

@ -0,0 +1,34 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import jxl.Workbook;
import jxl.biff.drawing.DrawingGroup;
import jxl.biff.drawing.EscherDisplay;
import jxl.read.biff.WorkbookParser;
public class EscherDrawingGroup {
public EscherDrawingGroup(Workbook w, OutputStream out, String encoding) throws IOException {
if (encoding == null || !encoding.equals("UnicodeBig"))
encoding = "UTF8";
try {
OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
BufferedWriter bw = new BufferedWriter(osw);
WorkbookParser wp = (WorkbookParser)w;
DrawingGroup dg = wp.getDrawingGroup();
if (dg != null) {
EscherDisplay ed = new EscherDisplay(dg, bw);
ed.display();
}
bw.newLine();
bw.newLine();
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}

View file

@ -0,0 +1,50 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import jxl.Cell;
import jxl.CellFeatures;
import jxl.CellReferenceHelper;
import jxl.Sheet;
import jxl.Workbook;
public class Features {
public Features(Workbook w, OutputStream out, String encoding) throws IOException {
if (encoding == null || !encoding.equals("UnicodeBig"))
encoding = "UTF8";
try {
OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
BufferedWriter bw = new BufferedWriter(osw);
for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) {
Sheet s = w.getSheet(sheet);
bw.write(s.getName());
bw.newLine();
Cell[] row = null;
Cell c = null;
for (int i = 0; i < s.getRows(); i++) {
row = s.getRow(i);
for (int j = 0; j < row.length; j++) {
c = row[j];
if (c.getCellFeatures() != null) {
CellFeatures features = c.getCellFeatures();
StringBuffer sb = new StringBuffer();
CellReferenceHelper.getCellReference(c.getColumn(), c.getRow(), sb);
bw.write("Cell " + sb.toString() + " contents: " + c.getContents());
bw.flush();
bw.write(" comment: " + features.getComment());
bw.flush();
bw.newLine();
}
}
}
}
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}

View file

@ -0,0 +1,67 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import jxl.Cell;
import jxl.CellType;
import jxl.FormulaCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.biff.CellReferenceHelper;
import jxl.biff.formula.FormulaException;
public class Formulas {
public Formulas(Workbook w, OutputStream out, String encoding) throws IOException {
if (encoding == null || !encoding.equals("UnicodeBig"))
encoding = "UTF8";
try {
OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
BufferedWriter bw = new BufferedWriter(osw);
ArrayList<String> parseErrors = new ArrayList();
for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) {
Sheet s = w.getSheet(sheet);
bw.write(s.getName());
bw.newLine();
Cell[] row = null;
Cell c = null;
for (int i = 0; i < s.getRows(); i++) {
row = s.getRow(i);
for (int j = 0; j < row.length; j++) {
c = row[j];
if (c.getType() == CellType.NUMBER_FORMULA || c.getType() == CellType.STRING_FORMULA || c.getType() == CellType.BOOLEAN_FORMULA || c.getType() == CellType.DATE_FORMULA || c.getType() == CellType.FORMULA_ERROR) {
FormulaCell nfc = (FormulaCell)c;
StringBuffer sb = new StringBuffer();
CellReferenceHelper.getCellReference(c.getColumn(), c.getRow(), sb);
try {
bw.write("Formula in " + sb.toString() + " value: " + c.getContents());
bw.flush();
bw.write(" formula: " + nfc.getFormula());
bw.flush();
bw.newLine();
} catch (FormulaException e) {
bw.newLine();
parseErrors.add(s.getName() + '!' + sb.toString() + ": " + e.getMessage());
}
}
}
}
}
bw.flush();
bw.close();
if (parseErrors.size() > 0) {
System.err.println();
System.err.println("There were " + parseErrors.size() + " errors");
Iterator<String> i = parseErrors.iterator();
while (i.hasNext())
System.err.println((Object)i.next());
}
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}

View file

@ -0,0 +1,83 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import jxl.WorkbookSettings;
import jxl.biff.BaseCompoundFile;
import jxl.read.biff.BiffException;
import jxl.read.biff.CompoundFile;
class PropertySetsReader {
private BufferedWriter writer;
private CompoundFile compoundFile;
public PropertySetsReader(File file, String propertySet, OutputStream os) throws IOException, BiffException {
this.writer = new BufferedWriter(new OutputStreamWriter(os));
FileInputStream fis = new FileInputStream(file);
int initialFileSize = 1048576;
int arrayGrowSize = 1048576;
byte[] d = new byte[initialFileSize];
int bytesRead = fis.read(d);
int pos = bytesRead;
while (bytesRead != -1) {
if (pos >= d.length) {
byte[] newArray = new byte[d.length + arrayGrowSize];
System.arraycopy(d, 0, newArray, 0, d.length);
d = newArray;
}
bytesRead = fis.read(d, pos, d.length - pos);
pos += bytesRead;
}
bytesRead = pos + 1;
this.compoundFile = new CompoundFile(d, new WorkbookSettings());
fis.close();
if (propertySet == null) {
displaySets();
} else {
displayPropertySet(propertySet, os);
}
}
void displaySets() throws IOException {
int numSets = this.compoundFile.getNumberOfPropertySets();
for (int i = 0; i < numSets; i++) {
BaseCompoundFile.PropertyStorage ps = this.compoundFile.getPropertySet(i);
this.writer.write(Integer.toString(i));
this.writer.write(") ");
this.writer.write(ps.name);
this.writer.write("(type ");
this.writer.write(Integer.toString(ps.type));
this.writer.write(" size ");
this.writer.write(Integer.toString(ps.size));
this.writer.write(" prev ");
this.writer.write(Integer.toString(ps.previous));
this.writer.write(" next ");
this.writer.write(Integer.toString(ps.next));
this.writer.write(" child ");
this.writer.write(Integer.toString(ps.child));
this.writer.write(" start block ");
this.writer.write(Integer.toString(ps.startBlock));
this.writer.write(")");
this.writer.newLine();
}
this.writer.flush();
this.writer.close();
}
void displayPropertySet(String ps, OutputStream os) throws IOException, BiffException {
if (ps.equalsIgnoreCase("SummaryInformation")) {
ps = "\005SummaryInformation";
} else if (ps.equalsIgnoreCase("DocumentSummaryInformation")) {
ps = "\005DocumentSummaryInformation";
} else if (ps.equalsIgnoreCase("CompObj")) {
ps = "\001CompObj";
}
byte[] stream = this.compoundFile.getStream(ps);
os.write(stream);
}
}

View file

@ -0,0 +1,260 @@
package jxl.demo;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import jxl.Cell;
import jxl.CellType;
import jxl.Range;
import jxl.Workbook;
import jxl.common.Logger;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Blank;
import jxl.write.DateFormat;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCell;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableHyperlink;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class ReadWrite {
private static Logger logger = Logger.getLogger(ReadWrite.class);
private File inputWorkbook;
private File outputWorkbook;
public ReadWrite(String input, String output) {
this.inputWorkbook = new File(input);
this.outputWorkbook = new File(output);
logger.setSuppressWarnings(Boolean.getBoolean("jxl.nowarnings"));
logger.info("Input file: " + input);
logger.info("Output file: " + output);
}
public void readWrite() throws IOException, BiffException, WriteException {
logger.info("Reading...");
Workbook w1 = Workbook.getWorkbook(this.inputWorkbook);
logger.info("Copying...");
WritableWorkbook w2 = Workbook.createWorkbook(this.outputWorkbook, w1);
if (this.inputWorkbook.getName().equals("jxlrwtest.xls"))
modify(w2);
w2.write();
w2.close();
logger.info("Done");
}
private void modify(WritableWorkbook w) throws WriteException {
logger.info("Modifying...");
WritableSheet sheet = w.getSheet("modified");
WritableCell cell = null;
CellFormat cf = null;
Label l = null;
WritableCellFeatures wcf = null;
cell = sheet.getWritableCell(1, 3);
WritableFont bold = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
cf = new WritableCellFormat(bold);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 4);
WritableFont underline = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.SINGLE);
cf = new WritableCellFormat(underline);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 5);
WritableFont tenpoint = new WritableFont(WritableFont.ARIAL, 10);
cf = new WritableCellFormat(tenpoint);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 6);
if (cell.getType() == CellType.LABEL) {
Label lc = (Label)cell;
lc.setString(lc.getString() + " - mod");
}
cell = sheet.getWritableCell(1, 9);
NumberFormat sevendps = new NumberFormat("#.0000000");
cf = new WritableCellFormat(sevendps);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 10);
NumberFormat exp4 = new NumberFormat("0.####E0");
cf = new WritableCellFormat(exp4);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 11);
cell.setCellFormat(WritableWorkbook.NORMAL_STYLE);
cell = sheet.getWritableCell(1, 12);
if (cell.getType() == CellType.NUMBER) {
Number number = (Number)cell;
number.setValue(42.0D);
}
cell = sheet.getWritableCell(1, 13);
if (cell.getType() == CellType.NUMBER) {
Number number = (Number)cell;
number.setValue(number.getValue() + 0.1D);
}
cell = sheet.getWritableCell(1, 16);
DateFormat df = new DateFormat("dd MMM yyyy HH:mm:ss");
cf = new WritableCellFormat(df);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 17);
cf = new WritableCellFormat(DateFormats.FORMAT9);
cell.setCellFormat(cf);
cell = sheet.getWritableCell(1, 18);
if (cell.getType() == CellType.DATE) {
DateTime dt = (DateTime)cell;
Calendar cal = Calendar.getInstance();
cal.set(1998, 1, 18, 11, 23, 28);
Date d = cal.getTime();
dt.setDate(d);
}
cell = sheet.getWritableCell(1, 22);
if (cell.getType() == CellType.NUMBER) {
Number number = (Number)cell;
number.setValue(6.8D);
}
cell = sheet.getWritableCell(1, 29);
if (cell.getType() == CellType.LABEL) {
l = (Label)cell;
l.setString("Modified string contents");
}
sheet.insertRow(34);
sheet.removeRow(38);
sheet.insertColumn(9);
sheet.removeColumn(11);
sheet.removeRow(43);
sheet.insertRow(43);
WritableHyperlink[] hyperlinks = sheet.getWritableHyperlinks();
for (int i = 0; i < hyperlinks.length; i++) {
WritableHyperlink wh = hyperlinks[i];
if (wh.getColumn() == 1 && wh.getRow() == 39) {
try {
wh.setURL(new URL("http://www.andykhan.com/jexcelapi/index.html"));
} catch (MalformedURLException e) {
logger.warn(e.toString());
}
} else if (wh.getColumn() == 1 && wh.getRow() == 40) {
wh.setFile(new File("../jexcelapi/docs/overview-summary.html"));
} else if (wh.getColumn() == 1 && wh.getRow() == 41) {
wh.setFile(new File("d:/home/jexcelapi/docs/jxl/package-summary.html"));
} else if (wh.getColumn() == 1 && wh.getRow() == 44) {
sheet.removeHyperlink(wh);
}
}
WritableCell c = sheet.getWritableCell(5, 30);
WritableCellFormat newFormat = new WritableCellFormat(c.getCellFormat());
newFormat.setBackground(Colour.RED);
c.setCellFormat(newFormat);
l = new Label(0, 49, "Modified merged cells");
sheet.addCell(l);
Number n = (Number)sheet.getWritableCell(0, 70);
n.setValue(9.0D);
n = (Number)sheet.getWritableCell(0, 71);
n.setValue(10.0D);
n = (Number)sheet.getWritableCell(0, 73);
n.setValue(4.0D);
Formula f = new Formula(1, 80, "ROUND(COS(original!B10),2)");
sheet.addCell(f);
f = new Formula(1, 83, "value1+value2");
sheet.addCell(f);
f = new Formula(1, 84, "AVERAGE(value1,value1*4,value2)");
sheet.addCell(f);
Label label = new Label(0, 88, "Some copied cells", cf);
sheet.addCell(label);
label = new Label(0, 89, "Number from B9");
sheet.addCell(label);
WritableCell wc = sheet.getWritableCell(1, 9).copyTo(1, 89);
sheet.addCell(wc);
label = new Label(0, 90, "Label from B4 (modified format)");
sheet.addCell(label);
wc = sheet.getWritableCell(1, 3).copyTo(1, 90);
sheet.addCell(wc);
label = new Label(0, 91, "Date from B17");
sheet.addCell(label);
wc = sheet.getWritableCell(1, 16).copyTo(1, 91);
sheet.addCell(wc);
label = new Label(0, 92, "Boolean from E16");
sheet.addCell(label);
wc = sheet.getWritableCell(4, 15).copyTo(1, 92);
sheet.addCell(wc);
label = new Label(0, 93, "URL from B40");
sheet.addCell(label);
wc = sheet.getWritableCell(1, 39).copyTo(1, 93);
sheet.addCell(wc);
for (int j = 0; j < 6; j++) {
Number number = new Number(1, 94 + j, (double)(j + 1) + (double)j / 8.0D);
sheet.addCell(number);
}
label = new Label(0, 100, "Formula from B27");
sheet.addCell(label);
wc = sheet.getWritableCell(1, 26).copyTo(1, 100);
sheet.addCell(wc);
label = new Label(0, 101, "A brand new formula");
sheet.addCell(label);
Formula formula = new Formula(1, 101, "SUM(B94:B96)");
sheet.addCell(formula);
label = new Label(0, 102, "A copy of it");
sheet.addCell(label);
wc = sheet.getWritableCell(1, 101).copyTo(1, 102);
sheet.addCell(wc);
WritableImage wi = sheet.getImage(1);
sheet.removeImage(wi);
wi = new WritableImage(1.0D, 116.0D, 2.0D, 9.0D, new File("resources/littlemoretonhall.png"));
sheet.addImage(wi);
label = new Label(0, 151, "Added drop down validation");
sheet.addCell(label);
Blank b = new Blank(1, 151);
wcf = new WritableCellFeatures();
ArrayList<String> al = new ArrayList();
al.add("The Fellowship of the Ring");
al.add("The Two Towers");
al.add("The Return of the King");
wcf.setDataValidationList(al);
b.setCellFeatures(wcf);
sheet.addCell(b);
label = new Label(0, 152, "Added number validation 2.718 < x < 3.142");
sheet.addCell(label);
b = new Blank(1, 152);
wcf = new WritableCellFeatures();
wcf.setNumberValidation(2.718D, 3.142D, WritableCellFeatures.BETWEEN);
b.setCellFeatures(wcf);
sheet.addCell(b);
cell = sheet.getWritableCell(0, 156);
l = (Label)cell;
l.setString("Label text modified");
cell = sheet.getWritableCell(0, 157);
wcf = cell.getWritableCellFeatures();
wcf.setComment("modified comment text");
cell = sheet.getWritableCell(0, 158);
wcf = cell.getWritableCellFeatures();
wcf.removeComment();
cell = sheet.getWritableCell(0, 172);
wcf = cell.getWritableCellFeatures();
Range r = wcf.getSharedDataValidationRange();
Cell botright = r.getBottomRight();
sheet.removeSharedDataValidation(cell);
al = new ArrayList<String>();
al.add("Stanley Featherstonehaugh Ukridge");
al.add("Major Plank");
al.add("Earl of Ickenham");
al.add("Sir Gregory Parsloe-Parsloe");
al.add("Honoria Glossop");
al.add("Stiffy Byng");
al.add("Bingo Little");
wcf.setDataValidationList(al);
cell.setCellFeatures(wcf);
sheet.applySharedDataValidation(cell, botright.getColumn() - cell.getColumn(), 1);
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,42 @@
package jxl.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.WorkbookSettings;
import jxl.biff.StringHelper;
import jxl.biff.Type;
import jxl.read.biff.BiffException;
import jxl.read.biff.BiffRecordReader;
import jxl.read.biff.Record;
class WriteAccess {
private BiffRecordReader reader;
public WriteAccess(File file) throws IOException, BiffException {
WorkbookSettings ws = new WorkbookSettings();
FileInputStream fis = new FileInputStream(file);
jxl.read.biff.File f = new jxl.read.biff.File(fis, ws);
this.reader = new BiffRecordReader(f);
display(ws);
fis.close();
}
private void display(WorkbookSettings ws) throws IOException {
Record r = null;
boolean found = false;
while (this.reader.hasNext() && !found) {
r = this.reader.next();
if (r.getType() == Type.WRITEACCESS)
found = true;
}
if (!found) {
System.err.println("Warning: could not find write access record");
return;
}
byte[] data = r.getData();
String s = null;
s = StringHelper.getString(data, data.length, 0, ws);
System.out.println(s);
}
}

View file

@ -0,0 +1,188 @@
package jxl.demo;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.format.Font;
import jxl.format.Pattern;
public class XML {
private OutputStream out;
private String encoding;
private Workbook workbook;
public XML(Workbook w, OutputStream out, String enc, boolean f) throws IOException {
this.encoding = enc;
this.workbook = w;
this.out = out;
if (this.encoding == null || !this.encoding.equals("UnicodeBig"))
this.encoding = "UTF8";
if (f) {
writeFormattedXML();
} else {
writeXML();
}
}
private void writeXML() throws IOException {
try {
OutputStreamWriter osw = new OutputStreamWriter(this.out, this.encoding);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("<?xml version=\"1.0\" ?>");
bw.newLine();
bw.write("<!DOCTYPE workbook SYSTEM \"workbook.dtd\">");
bw.newLine();
bw.newLine();
bw.write("<workbook>");
bw.newLine();
for (int sheet = 0; sheet < this.workbook.getNumberOfSheets(); sheet++) {
Sheet s = this.workbook.getSheet(sheet);
bw.write(" <sheet>");
bw.newLine();
bw.write(" <name><![CDATA[" + s.getName() + "]]></name>");
bw.newLine();
Cell[] row = null;
for (int i = 0; i < s.getRows(); i++) {
bw.write(" <row number=\"" + i + "\">");
bw.newLine();
row = s.getRow(i);
for (int j = 0; j < row.length; j++) {
if (row[j].getType() != CellType.EMPTY) {
bw.write(" <col number=\"" + j + "\">");
bw.write("<![CDATA[" + row[j].getContents() + "]]>");
bw.write("</col>");
bw.newLine();
}
}
bw.write(" </row>");
bw.newLine();
}
bw.write(" </sheet>");
bw.newLine();
}
bw.write("</workbook>");
bw.newLine();
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
private void writeFormattedXML() throws IOException {
try {
OutputStreamWriter osw = new OutputStreamWriter(this.out, this.encoding);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("<?xml version=\"1.0\" ?>");
bw.newLine();
bw.write("<!DOCTYPE workbook SYSTEM \"formatworkbook.dtd\">");
bw.newLine();
bw.newLine();
bw.write("<workbook>");
bw.newLine();
for (int sheet = 0; sheet < this.workbook.getNumberOfSheets(); sheet++) {
Sheet s = this.workbook.getSheet(sheet);
bw.write(" <sheet>");
bw.newLine();
bw.write(" <name><![CDATA[" + s.getName() + "]]></name>");
bw.newLine();
Cell[] row = null;
CellFormat format = null;
Font font = null;
for (int i = 0; i < s.getRows(); i++) {
bw.write(" <row number=\"" + i + "\">");
bw.newLine();
row = s.getRow(i);
for (int j = 0; j < row.length; j++) {
if (row[j].getType() != CellType.EMPTY || row[j].getCellFormat() != null) {
format = row[j].getCellFormat();
bw.write(" <col number=\"" + j + "\">");
bw.newLine();
bw.write(" <data>");
bw.write("<![CDATA[" + row[j].getContents() + "]]>");
bw.write("</data>");
bw.newLine();
if (row[j].getCellFormat() != null) {
bw.write(" <format wrap=\"" + format.getWrap() + "\"");
bw.newLine();
bw.write(" align=\"" + format.getAlignment().getDescription() + "\"");
bw.newLine();
bw.write(" valign=\"" + format.getVerticalAlignment().getDescription() + "\"");
bw.newLine();
bw.write(" orientation=\"" + format.getOrientation().getDescription() + "\"");
bw.write(">");
bw.newLine();
font = format.getFont();
bw.write(" <font name=\"" + font.getName() + "\"");
bw.newLine();
bw.write(" point_size=\"" + font.getPointSize() + "\"");
bw.newLine();
bw.write(" bold_weight=\"" + font.getBoldWeight() + "\"");
bw.newLine();
bw.write(" italic=\"" + font.isItalic() + "\"");
bw.newLine();
bw.write(" underline=\"" + font.getUnderlineStyle().getDescription() + "\"");
bw.newLine();
bw.write(" colour=\"" + font.getColour().getDescription() + "\"");
bw.newLine();
bw.write(" script=\"" + font.getScriptStyle().getDescription() + "\"");
bw.write(" />");
bw.newLine();
if (format.getBackgroundColour() != Colour.DEFAULT_BACKGROUND || format.getPattern() != Pattern.NONE) {
bw.write(" <background colour=\"" + format.getBackgroundColour().getDescription() + "\"");
bw.newLine();
bw.write(" pattern=\"" + format.getPattern().getDescription() + "\"");
bw.write(" />");
bw.newLine();
}
if (format.getBorder(Border.TOP) != BorderLineStyle.NONE || format.getBorder(Border.BOTTOM) != BorderLineStyle.NONE || format.getBorder(Border.LEFT) != BorderLineStyle.NONE || format.getBorder(Border.RIGHT) != BorderLineStyle.NONE) {
bw.write(" <border top=\"" + format.getBorder(Border.TOP).getDescription() + "\"");
bw.newLine();
bw.write(" bottom=\"" + format.getBorder(Border.BOTTOM).getDescription() + "\"");
bw.newLine();
bw.write(" left=\"" + format.getBorder(Border.LEFT).getDescription() + "\"");
bw.newLine();
bw.write(" right=\"" + format.getBorder(Border.RIGHT).getDescription() + "\"");
bw.write(" />");
bw.newLine();
}
if (!format.getFormat().getFormatString().equals("")) {
bw.write(" <format_string string=\"");
bw.write(format.getFormat().getFormatString());
bw.write("\" />");
bw.newLine();
}
bw.write(" </format>");
bw.newLine();
}
bw.write(" </col>");
bw.newLine();
}
}
bw.write(" </row>");
bw.newLine();
}
bw.write(" </sheet>");
bw.newLine();
}
bw.write("</workbook>");
bw.newLine();
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
}
}