?? xlsview.java
字號:
boolean showHeader=model.getTable().isShowHeader();
List columns = model.getColumnHandler().getHeaderColumns();
String extendRowTop=(String)(model.getTable().getAttribute("ExtendRowTop"));
try {
etr=createRow(sheet, getRows(extendRowTop,encoding),cellFormat, rownum,0);
} catch (Exception e) {
LogHandler.warnLog(logger, e);
etr=0;
}
rownum+=etr;
if (showHeader || etr<1){
for (Iterator iter = columns.iterator(); iter.hasNext();) {
Column column = (Column) iter.next();
String title = column.getCellDisplay();
Label label=new Label(cellnum,rownum,title,cellFormat);
sheet.addCell(label);
int valWidth = (title + "").length() ;
valWidth=1;
sheet.setColumnView(cellnum, valWidth*colWidth);
cellnum++;
}
}else{
if (rownum>0){
rownum--;
}
}
}
public void body(TableModel model, Column column) {
if (column.isFirstColumn()) {
rownum++;
cellnum = 0;
}
try {
String value = ExportViewUtils.parseXLS(column.getCellDisplay());
if (column.isEscapeAutoFormat()) {
writeToCellAsText(value, null);
} else {
writeToCellFormatted(value, null);
}
cellnum++;
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
LogHandler.errorLog(logger, e);
} catch (WriteException e) {
// TODO Auto-generated catch block
LogHandler.errorLog(logger, e);
}
}
public Object afterBody(TableModel model) {
if (model.getLimit().getTotalRows() != 0) {
try {
totals(model);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
LogHandler.errorLog(logger, e);
} catch (WriteException e) {
// TODO Auto-generated catch block
LogHandler.errorLog(logger, e);
}
}
try {
// int totalCol=model.getColumnHandler().getColumns().size();
rownum++;
String extendRowAfter=(String)(model.getTable().getAttribute("ExtendRowAfter"));
rownum+=createRow(sheet, getRows(extendRowAfter,encoding),(CellFormat)WritableWorkbook.NORMAL_STYLE, rownum,0);
wb.write();
wb.close();
} catch (WriteException e) {
LogHandler.warnLog(logger, e);
} catch (IOException e) {
LogHandler.warnLog(logger, e);
} catch (Exception e) {
LogHandler.warnLog(logger, e);
}finally{
wb=null;
sheet=null;
outputStream=null;
}
return outputStream;
}
private void writeToCellAsText( String value, WritableCellFormat styleModifier) throws RowsExceededException, WriteException {
// format text
if (value.trim().equals(NBSP)) {
value = "";
}
Label label=new Label(cellnum,rownum,value);
if (styleModifier!=null){
label.setCellFormat(styleModifier);
}
sheet.addCell(label);
}
private void writeToCellFormatted( String value, WritableCellFormat styleModifier) throws RowsExceededException, WriteException {
double numeric = NON_NUMERIC;
try {
numeric = Double.parseDouble(value);
} catch (Exception e) {
numeric = NON_NUMERIC;
}
if (value.startsWith("$") || value.endsWith("%") || value.startsWith("($")) {
boolean moneyFlag = (value.startsWith("$") || value.startsWith("($"));
boolean percentFlag = value.endsWith("%");
value = StringUtils.replace(value, "$", "");
value = StringUtils.replace(value, "%", "");
value = StringUtils.replace(value, ",", "");
value = StringUtils.replace(value, "(", "-");
value = StringUtils.replace(value, ")", "");
try {
numeric = Double.parseDouble(value);
} catch (Exception e) {
numeric = NON_NUMERIC;
}
if (moneyFlag) {
// format money
NumberFormat fivedps = new NumberFormat(moneyFormat);
WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps);
Number number = new Number(cellnum, rownum, numeric, fivedpsFormat);
if (styleModifier!=null){
number.setCellFormat(styleModifier);
}
sheet.addCell(number);
} else if (percentFlag) {
// format percent
numeric = numeric / 100;
NumberFormat fivedps = new NumberFormat(percentFormat);
WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps);
Number number = new Number(cellnum, rownum, numeric, fivedpsFormat);
if (styleModifier!=null){
number.setCellFormat(styleModifier);
}
sheet.addCell(number);
}
} else if (Math.abs(numeric - NON_NUMERIC) >= .0000001) {
// numeric != NON_NUMERIC
// format numeric
Number number = new Number(cellnum, rownum, numeric);
if (styleModifier!=null){
number.setCellFormat(styleModifier);
}
sheet.addCell(number);
} else {
// format text
if (value.trim().equals(NBSP)) {
value = "";
}
Label label=new Label(cellnum,rownum,value);
if (styleModifier!=null){
label.setCellFormat(styleModifier);
}
sheet.addCell(label);
}
}
// Add to export totals
public void totals(TableModel model) throws RowsExceededException, WriteException {
Column firstCalcColumn = model.getColumnHandler().getFirstCalcColumn();
WritableCellFormat cellFormatTotals=new WritableCellFormat();
cellFormatTotals.setBackground(Colour.GRAY_25);
cellFormatTotals.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.GRAY_50);
if (firstCalcColumn != null) {
int rows = firstCalcColumn.getCalc().length;
for (int i = 0; i < rows; i++) {
rownum++;
cellnum = 0;
for (Iterator iter = model.getColumnHandler().getColumns().iterator(); iter.hasNext();) {
Column column = (Column) iter.next();
if (column.isFirstColumn()) {
String calcTitle = CalcUtils.getFirstCalcColumnTitleByPosition(model, i);
if (column.isEscapeAutoFormat()) {
writeToCellAsText(calcTitle, cellFormatTotals);
} else {
writeToCellFormatted(calcTitle, cellFormatTotals);
}
cellnum++;
continue;
}
if (column.isCalculated()) {
CalcResult calcResult = CalcUtils.getCalcResultsByPosition(model, column, i);
java.lang.Number value = calcResult.getValue();
if (value != null){
//if (column.isEscapeAutoFormat()) {
// writeToCellAsText( value.toString(), cellFormatTotals);
//} else {
// writeToCellFormatted( ExtremeUtils.formatNumber(column.getFormat(), value, model.getLocale()), cellFormatTotals);
// }
if (StringUtils.isNotBlank(column.getFormat())){
writeToCellFormatted( ExtremeUtils.formatNumber(column.getFormat(), value, model.getLocale()), cellFormatTotals);
}else{
writeToCellAsText( value.toString(), cellFormatTotals);
}
} else {
Label label=new Label(cellnum,rownum,"n/a");
sheet.addCell(label);
}
cellnum++;
} else {
writeToCellFormatted( "", cellFormatTotals);
cellnum++;
}
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -