/**//* Copyright 2006-2007 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ecside.view; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import jxl.Cell; import jxl.Workbook; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.CellFormat; import jxl.format.Colour; import jxl.write.Blank; import jxl.write.Label; import jxl.write.Number; import jxl.write.NumberFormat; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ecside.common.log.LogHandler; import org.ecside.core.ECSideContext; import org.ecside.core.TableModel; import org.ecside.core.bean.Column; import org.ecside.preferences.PreferencesConstants; import org.ecside.table.calc.CalcResult; import org.ecside.table.calc.CalcUtils; import org.ecside.util.ECSideUtils; import org.ecside.util.ExportViewUtils; import org.ecside.util.ExtremeUtils; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.nodes.TagNode; import org.htmlparser.tags.TableColumn; import org.htmlparser.tags.TableRow; import org.htmlparser.tags.TableTag; import org.htmlparser.util.NodeList; /** *//** * @author Wei Zijun * @editor by pharaohsprince 法老 * http://blog.youkuaiyun.com/pharaohsprince * 2007.09.25 中秋节 */ /** *//** * com.extremecomp.table.view.XlsView.java - * * @author paul horn */ publicclass XlsView implements View ...{ private Log logger = LogFactory.getLog(XlsView.class); publicstaticfinalint WIDTH_MULT =240; // width per char publicstaticfinalint MIN_CHARS =8; // minimum char width publicstaticfinalshort DEFAULT_FONT_HEIGHT =8; publicstaticfinaldouble NON_NUMERIC =-.99999; publicstaticfinal String DEFAULT_MONEY_FORMAT ="$###,###,##0.00"; publicstaticfinal String DEFAULT_PERCENT_FORMAT ="##0.0%"; publicstaticfinal String NBSP =" "; publicstaticfinalint colWidth=15; private WritableWorkbook wb; private WritableSheet sheet; privateint rownum; privateshort cellnum; private String moneyFormat; private String percentFormat; private ByteArrayOutputStream outputStream ; private ByteArrayOutputStream outputStreamOut; private PrintWriter out =null; private String encoding; public XlsView() ...{ encoding=ECSideContext.ENCODING; } publicvoid beforeBody(TableModel model) ...{ logger.debug("XlsView.init()"); outputStream=new ByteArrayOutputStream(); outputStreamOut=outputStream; out=new PrintWriter(outputStream); moneyFormat = model.getPreferences().getPreference(PreferencesConstants.TABLE_EXPORTABLE +"format.money"); if (StringUtils.isEmpty(moneyFormat)) ...{ moneyFormat = DEFAULT_MONEY_FORMAT; } percentFormat = model.getPreferences().getPreference(PreferencesConstants.TABLE_EXPORTABLE +"format.percent"); if (StringUtils.isEmpty(percentFormat)) ...{ percentFormat = DEFAULT_PERCENT_FORMAT; } // encoding = model.getExportHandler().getCurrentExport().getEncoding(); try...{ wb = Workbook.createWorkbook(outputStream); sheet = wb.createSheet("Export Workbook",0); createHeader(model); // int totalCol=model.getColumnHandler().getColumns().size(); rownum++; String extendRowBefore=(String)(model.getTable().getAttribute("ExtendRowBefore")); rownum+=createRow(sheet, getRows(extendRowBefore,encoding),(CellFormat)WritableWorkbook.NORMAL_STYLE, rownum,0)-1; }catch (Exception e) ...{ LogHandler.errorLog(logger, e); } } publicstatic TableRow[] getRows(String inputHtml,String encode) throws Exception ...{ if (StringUtils.isBlank(inputHtml))...{ returnnull; } inputHtml=inputHtml.trim(); if (!inputHtml.startsWith("<table>")&&!inputHtml.startsWith("<TABLE>") )...{ inputHtml="<table>"+inputHtml+"</table>"; } Parser parser = Parser.createParser(inputHtml, encode); NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() ...{ publicboolean accept(Node node) ...{ return node instanceof TagNode; } }); TagNode node = (TagNode)nodes.elementAt(0); return ((TableTag)node).getRows(); } publicstaticint getColumnNum(TableRow row)...{ int totalCol=0; TableColumn[] columns=row.getColumns(); for (int cn=0;cn<columns.length;cn++)...{ String colspan=columns[cn].getAttribute("colspan"); if (colspan!=null&&colspan.length()>0)...{ try...{ totalCol+=Integer.parseInt(colspan); continue; }catch(Exception e)...{} } totalCol++; } return totalCol; } publicstaticint createRow(WritableSheet sheet,TableRow[] tableRows,CellFormat cellFormat,int startRow,int startCol) throws RowsExceededException, WriteException...{ if (tableRows==null||tableRows.length<1) ...{ return0; } int totalCol=getColumnNum(tableRows[0]); // CellFormat cellFormat=(CellFormat)WritableWorkbook.NORMAL_STYLE; int colWidth=15; List mergeCells=new ArrayList(); for (int rowNo=startRow;rowNo<startRow+tableRows.length;rowNo++ )...{ int idx=0; for (int colNo=startCol;colNo<startCol+totalCol;colNo++)...{ Cell cell=sheet.getCell(colNo,rowNo); if (cell instanceof Blank) ...{ continue; } TableColumn tdBean=((TableRow)tableRows[rowNo-startRow]).getColumns()[idx]; String title=ECSideUtils.specialHTMLToShowTEXT(tdBean.toPlainTextString()); Label label=new Label(colNo,rowNo,title,cellFormat); sheet.addCell(label); sheet.setColumnView(colNo, colWidth); idx++; int ce=Integer.parseInt(ECSideUtils.convertString(tdBean.getAttribute("colspan"),"1"))-1; int re=Integer.parseInt(ECSideUtils.convertString(tdBean.getAttribute("rowspan"),"1"))-1; if (ce>=1|| re>=1)...{ ce=ce<0?0:ce; re=re<0?0:re; mergeCells.add(newint[]...{colNo,rowNo,colNo+ce,rowNo+re}); if (ce<1&& re>=1)...{ for (int srowNo=1;srowNo<=re; srowNo++)...{ sheet.addCell(new Blank(colNo,rowNo+srowNo)); sheet.setColumnView(colNo, colWidth); } continue; }elseif (re<1&& ce>=1)...{ for (int scolNo=1;scolNo<=ce;scolNo++)...{ sheet.addCell(new Blank(colNo+scolNo,rowNo)); sheet.setColumnView(colNo+scolNo, colWidth); } colNo+=ce; continue; }elseif (ce>=1&& re>=1)...{ for (int scolNo=1;scolNo<=ce;scolNo++)...{ for (int srowNo=1;srowNo<=re; srowNo++)...{ sheet.addCell(new Blank(colNo+scolNo,rowNo+srowNo)); sheet.setColumnView(colNo+scolNo, colWidth); } } colNo+=ce; continue; } } } } for (int i=0;i<mergeCells.size();i++)...{ int[] mc=(int[])mergeCells.get(i); sheet.mergeCells(mc[0],mc[1],mc[2],mc[3]); } return tableRows.length; } privatevoid createHeader(TableModel model) throws RowsExceededException, WriteException ...{ rownum =0; cellnum =0; int etr=0; //HSSFRow row = sheet.createRow(rownum); WritableCellFormat cellFormat=new WritableCellFormat(); WritableFont arial10font =new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,WritableFont.BOLD); cellFormat.setBackground(Colour.GRAY_25); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.GRAY_50); cellFormat.setFont(arial10font); 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--; } } } publicvoid 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(); out.flush(); out.close(); }catch (WriteException e) ...{ LogHandler.warnLog(logger, e); }catch (IOException e) ...{ LogHandler.warnLog(logger, e); }catch (Exception e) ...{ LogHandler.warnLog(logger, e); } return outputStreamOut; } privatevoid 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); } privatevoid 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); }elseif (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); } }elseif (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 publicvoid 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++; } } } } } }
/**//* * Copyright 2006-2007 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ecside.view; import java.io.ByteArrayOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.ecside.core.Preferences; /** *//** * @author Wei Zijun * @editor by pharaohsprince 法老 * http://blog.youkuaiyun.com/pharaohsprince * 2007.09.25 中秋节 */ publicclass XlsViewResolver implements ViewResolver ...{ publicvoid resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception ...{ if (viewData!=null)...{ ByteArrayOutputStream out = (ByteArrayOutputStream)viewData; byte[] contents = out.toByteArray(); response.setContentLength(contents.length); response.getOutputStream().write(contents); } } }
/**//* * Copyright 2004 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ecside.view; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.StringUtils; import org.ecside.core.TableModel; import org.ecside.core.bean.Column; import org.ecside.core.bean.Export; import org.ecside.table.calc.CalcResult; import org.ecside.table.calc.CalcUtils; import org.ecside.util.ExportViewUtils; import org.ecside.util.ExtremeUtils; /** *//** * For producing a delimited datafile view of the table. Default delimiter is * comma. * * @author Wei Zijun * @editor by pharaohsprince 法老 * http://blog.youkuaiyun.com/pharaohsprince * 2007.09.25 中秋节 */ publicclass CsvView implements View ...{ publicfinalstatic String DELIMITER ="delimiter"; finalstatic String DEFAULT_DELIMITER =","; // private StringBuffer plainData = new StringBuffer(); private ByteArrayOutputStream outputStream ; private ByteArrayOutputStream outputStreamOut; private StringBuffer rowBuffer =null; private PrintWriter out =null; private String delimiter; publicvoid beforeBody(TableModel model) ...{ outputStream=new ByteArrayOutputStream(); outputStreamOut=outputStream; /**//* outputStream=ContextUtils.getResponseOutputStream(model.getContext()); outputStreamOut=null; if (outputStream==null){ outputStream=new ByteArrayOutputStream(); outputStreamOut=outputStream; }*/ out=new PrintWriter(outputStream); Export export = model.getExportHandler().getCurrentExport(); delimiter = export.getAttributeAsString(DELIMITER); List columns = model.getColumnHandler().getHeaderColumns(); if (StringUtils.isBlank(delimiter)) ...{ delimiter = DEFAULT_DELIMITER; } boolean isFirstColumn=true; rowBuffer =new StringBuffer(); for (Iterator iter = columns.iterator(); iter.hasNext();) ...{ Column column = (Column) iter.next(); String value = ExportViewUtils.parseCSV(column.getCellDisplay()); if (!isFirstColumn)...{ rowBuffer.append(delimiter); } rowBuffer.append(value); isFirstColumn=false; } if (columns.size()>0)...{ rowBuffer.append(ExportViewUtils.BR); writeToOutputStream(rowBuffer.toString()); } } publicvoid body(TableModel model, Column column) ...{ String value = ExportViewUtils.parseCSV(column.getCellDisplay()); rowBuffer.append(value); if (column.isLastColumn()) ...{ rowBuffer.append(ExportViewUtils.BR); writeToOutputStream(rowBuffer.toString()); }else...{ rowBuffer.append(delimiter); } } public Object afterBody(TableModel model) ...{ totals(model); out.flush(); out.close(); return outputStreamOut; } publicvoid totals(TableModel model) ...{ rowBuffer =new StringBuffer(); Column firstCalcColumn = model.getColumnHandler().getFirstCalcColumn(); if (firstCalcColumn !=null) ...{ int rows = firstCalcColumn.getCalc().length; for (int i =0; i < rows; i++) ...{ for (Iterator iter = model.getColumnHandler().getColumns().iterator(); iter.hasNext();) ...{ Column column = (Column) iter.next(); if (column.isFirstColumn()) ...{ String calcTitle = CalcUtils.getFirstCalcColumnTitleByPosition(model, i); rowBuffer.append(ExportViewUtils.parseCSV(calcTitle) ); continue; } rowBuffer.append(delimiter); if (column.isCalculated()) ...{ CalcResult calcResult = CalcUtils.getCalcResultsByPosition(model, column, i); java.lang.Number value = calcResult.getValue(); if (value !=null)...{ if (StringUtils.isNotBlank(column.getFormat()))...{ rowBuffer.append(ExportViewUtils.parseCSV(ExtremeUtils.formatNumber(column.getFormat(), value, model.getLocale()))); }else...{ rowBuffer.append(ExportViewUtils.parseCSV(value.toString()) ); } }else...{ rowBuffer.append(ExportViewUtils.parseCSV("")); } }else...{ rowBuffer.append(ExportViewUtils.parseCSV("")); } } rowBuffer.append(ExportViewUtils.BR); writeToOutputStream(rowBuffer.toString()); } } } publicvoid writeToOutputStream(String rowContent)...{ out.print(rowContent); rowBuffer =new StringBuffer(); } }
/**//* * Copyright 2004 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ecside.view; import java.io.ByteArrayOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.ecside.core.Preferences; /** *//** * @author Jeff Johnston */ /** *//** * @editor by pharaohsprince 法老 * http://blog.youkuaiyun.com/pharaohsprince * 2007.09.25 中秋节 */ publicclass CsvViewResolver implements ViewResolver ...{ publicvoid resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception ...{ if (viewData!=null)...{ ByteArrayOutputStream out = (ByteArrayOutputStream)viewData; byte[] contents = out.toByteArray(); response.setContentLength(contents.length); response.getOutputStream().write(contents); } } }