=== modified file 'local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponse.java' --- local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponse.java 2011-06-29 07:29:06 +0000 +++ local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponse.java 2011-07-18 05:04:23 +0000 @@ -27,21 +27,26 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.io.File; +import static org.hisp.dhis.reportexcel.utils.ExcelUtils.convertAlignmentString; +import static org.hisp.dhis.reportexcel.utils.ExcelUtils.convertVerticalString; +import static org.hisp.dhis.reportexcel.utils.ExcelUtils.readSpecialValueByPOI; +import static org.hisp.dhis.reportexcel.utils.StringUtils.applyPatternDecimalFormat; + +import java.io.FileInputStream; import java.io.IOException; import java.util.Collection; -import jxl.Cell; -import jxl.CellType; -import jxl.Range; -import jxl.Sheet; -import jxl.Workbook; -import jxl.format.CellFormat; -import jxl.format.Colour; -import jxl.format.Font; -import jxl.format.Pattern; - -import org.hisp.dhis.reportexcel.utils.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFPatternFormatting; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xwpf.usermodel.UnderlinePatterns; /** * Simple demo class which uses the api to present the contents of an excel 97 @@ -85,7 +90,7 @@ // ------------------------------------------------------------------------- // Get & Set methods // ------------------------------------------------------------------------- - + protected String getXml() { return xml.toString(); @@ -112,10 +117,9 @@ throws Exception { this.cleanUpForResponse(); - this.bWRITE_DTD = bWriteDTD; this.bWRITE_VERSION = bWriteVersion; - this.WORKBOOK = Workbook.getWorkbook( new File( pathFileName ) ); + this.WORKBOOK = new HSSFWorkbook( new FileInputStream( pathFileName ) ); if ( bFormat ) { @@ -145,30 +149,36 @@ xml.append( WORKBOOK_OPENTAG ); - for ( Integer sheet : collectSheets ) + int i = 0; + int j = 0; + + for ( Integer sheetNo : collectSheets ) { - Sheet s = WORKBOOK.getSheet( sheet - 1 ); + Sheet sheet = WORKBOOK.getSheetAt( sheetNo - 1 ); xml.append( "" ); - xml.append( "" ); - - Cell[] row = null; - - for ( int i = 0; i < s.getRows(); i++ ) + xml.append( "" ); + + for ( Row row : sheet ) { + j = 0; + xml.append( "" ); - row = s.getRow( i ); - - for ( int j = 0; j < row.length; j++ ) + for ( Cell cell : row ) { - if ( row[j].getType() != CellType.EMPTY ) + if ( cell.getCellType() != Cell.CELL_TYPE_BLANK ) { xml.append( "" ); - xml.append( "" ); + xml.append( "" ); xml.append( "" ); } + + j++; } + + i++; + xml.append( "" ); } xml.append( "" ); @@ -218,34 +228,39 @@ private void writeBySheetNo( int sheetNo, boolean bDetailed ) { - Sheet s = WORKBOOK.getSheet( sheetNo - 1 ); + Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 ); xml.append( "" ); - xml.append( "" ); - - Cell[] cell = null; - - for ( int i = 0; i < s.getRows(); i++ ) + xml.append( "" ); + + int i = 0; + int j = 0; + + for ( Row row : s ) { + j = 0; + xml.append( "" ); - cell = s.getRow( i ); - - for ( int j = 0; j < cell.length; j++ ) + for ( Cell cell : row ) { - // Remember that empty cells can contain format - // information - if ( !cell[j].getType().equals( CellType.EMPTY ) || (cell[j].getCellFormat() != null) ) + // Remember that empty cells can contain format information + if ( (cell.getCellStyle() != null) || cell.getCellType() != Cell.CELL_TYPE_BLANK ) { xml.append( "" ); - xml.append( "" ); - this.readingDetailsFormattedCell( cell[j], bDetailed ); + this.readingDetailsFormattedCell( cell, bDetailed ); xml.append( "" ); } + + j++; } + + i++; + xml.append( "" ); } xml.append( "" ); @@ -254,41 +269,36 @@ private void readingDetailsFormattedCell( Cell objCell, boolean bDetailed ) { // The format information - CellFormat format = objCell.getCellFormat(); - Font font = null; + CellStyle format = objCell.getCellStyle(); if ( format != null ) { - xml.append( "" ); - - // The font information - font = format.getFont(); - - xml.append( "" ); + + xml.append( "" ); // The cell background information - if ( format.getBackgroundColour() != Colour.DEFAULT_BACKGROUND || format.getPattern() != Pattern.NONE ) + if ( format.getFillBackgroundColor() != IndexedColors.WHITE.getIndex() + || format.getFillPattern() != HSSFPatternFormatting.NO_FILL ) { - xml.append( "" ); + xml.append( "" ); } // The cell number/date format - if ( !format.getFormat().getFormatString().equals( "" ) ) + if ( !format.getDataFormatString().equals( "" ) ) { - xml.append( "" ); + xml.append( "" ); } xml.append( "" ); } @@ -325,23 +335,18 @@ private void writeBySheetNo( int sheetNo ) { - Sheet sheet = WORKBOOK.getSheet( sheetNo - 1 ); - Range[] aMergedCell = sheet.getMergedCells(); - - int iColTopLeft = 0; - int iRowTopLeft = 0; - int iColBottomRight = 0; - - for ( int j = 0; j < aMergedCell.length; j++ ) + Sheet sheet = WORKBOOK.getSheetAt( sheetNo - 1 ); + CellRangeAddress cellRangeAddress = null; + + for ( int i = 0; i < sheet.getNumMergedRegions(); i++ ) { - iColTopLeft = aMergedCell[j].getTopLeft().getColumn(); - iRowTopLeft = aMergedCell[j].getTopLeft().getRow(); - iColBottomRight = aMergedCell[j].getBottomRight().getColumn(); + cellRangeAddress = sheet.getMergedRegion( i ); - if ( iColTopLeft != iColBottomRight ) + if ( cellRangeAddress.getFirstColumn() != cellRangeAddress.getLastColumn() ) { - xml.append( "" - + (iColBottomRight - iColTopLeft + 1) + "" ); + xml.append( "" + + (cellRangeAddress.getLastColumn() - cellRangeAddress.getFirstColumn() + 1) + "" ); } } } === modified file 'local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponseImport.java' --- local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponseImport.java 2011-07-12 05:23:15 +0000 +++ local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/preview/action/XMLStructureResponseImport.java 2011-07-18 05:04:23 +0000 @@ -27,7 +27,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.reportexcel.utils.StringUtils.convertAlignmentString; +import static org.hisp.dhis.reportexcel.utils.ExcelUtils.convertAlignmentString; +import static org.hisp.dhis.reportexcel.utils.ExcelUtils.readSpecialValueByPOI; import java.io.File; import java.io.FileInputStream; @@ -35,13 +36,12 @@ import java.util.Collection; import java.util.List; -import jxl.Cell; -import jxl.CellType; -import jxl.Range; -import jxl.Sheet; -import jxl.Workbook; -import jxl.format.CellFormat; - +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.util.CellRangeAddress; import org.hisp.dhis.reportexcel.importitem.ExcelItem; import org.hisp.dhis.reportexcel.importitem.ExcelItemGroup; @@ -61,7 +61,7 @@ /** * The workbook we are reading from a given file */ - private Workbook WORKBOOK; + private org.apache.poi.ss.usermodel.Workbook WORKBOOK; private static final String WORKBOOK_OPENTAG = ""; @@ -102,37 +102,75 @@ FileInputStream inputStream = new FileInputStream( new File( pathFileName ) ); - this.WORKBOOK = Workbook.getWorkbook( inputStream ); + this.WORKBOOK = new HSSFWorkbook( inputStream ); this.writeFormattedXML( collectSheets, importItems, bWriteDescription, type ); } // ------------------------------------------------------------------------- - // Public methods + // Private methods // ------------------------------------------------------------------------- - public void writeData( int sheetNo, List importItems, String TYPE ) - { - Sheet s = WORKBOOK.getSheet( sheetNo - 1 ); + private void cleanUpForResponse() + { + System.gc(); + } + + private void writeFormattedXML( Collection collectSheets, List importItems, + boolean bWriteDescription, String type ) + throws Exception + { + if ( bWriteDescription ) + { + this.writeXMLMergedDescription( collectSheets ); + } + + xml.append( WORKBOOK_OPENTAG ); + + for ( Integer sheet : collectSheets ) + { + this.writeData( sheet, importItems, type ); + } + + xml.append( WORKBOOK_CLOSETAG ); + } + + private void writeXMLMergedDescription( Collection collectSheets ) + throws IOException + { + xml.append( MERGEDCELL_OPENTAG ); + + for ( Integer sheet : collectSheets ) + { + writeBySheetNo( sheet ); + } + + xml.append( MERGEDCELL_CLOSETAG ); + } + + private void writeData( int sheetNo, List importItems, String TYPE ) + { + Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 ); xml.append( "" ); - xml.append( "" ); + xml.append( "" ); - Cell[] cell = null; int run = 0; + int i = 0; + int j = 0; - for ( int i = 0; i < s.getRows(); i++ ) + for ( Row row : s ) { + j = 0; + xml.append( "" ); - cell = s.getRow( i ); - - for ( int j = 0; j < cell.length; j++ ) + for ( Cell cell : row ) { run = 0; - + // Remember that empty cells can contain format information - if ( !cell[j].getType().equals( CellType.EMPTY ) || (cell[j].getCellFormat() != null) ) + if ( (cell.getCellStyle() != null) || cell.getCellType() != Cell.CELL_TYPE_BLANK ) { xml.append( "" ); } @@ -153,94 +192,52 @@ } if ( run == importItems.size() ) - { + { xml.append( ">" ); } // end checking - xml.append( "" ); + xml.append( "" ); - this.readingDetailsFormattedCell( cell[j] ); + this.readingDetailsFormattedCell( cell ); xml.append( "" ); } + + j++; } + + i++; + xml.append( "" ); } xml.append( "" ); } - // ------------------------------------------------------------------------- - // Private methods - // ------------------------------------------------------------------------- - - private void cleanUpForResponse() - { - System.gc(); - } - - private void writeFormattedXML( Collection collectSheets, List importItems, - boolean bWriteDescription, String type ) - throws Exception - { - if ( bWriteDescription ) - { - this.writeXMLMergedDescription( collectSheets ); - } - - xml.append( WORKBOOK_OPENTAG ); - - for ( Integer sheet : collectSheets ) - { - writeData( sheet, importItems, type ); - } - - xml.append( WORKBOOK_CLOSETAG ); - } - - private void writeXMLMergedDescription( Collection collectSheets ) - throws IOException - { - xml.append( MERGEDCELL_OPENTAG ); - - for ( Integer sheet : collectSheets ) - { - writeBySheetNo( sheet ); - } - - xml.append( MERGEDCELL_CLOSETAG ); - } - private void writeBySheetNo( int sheetNo ) { - Sheet sheet = WORKBOOK.getSheet( sheetNo - 1 ); - Range[] aMergedCell = sheet.getMergedCells(); - - int iColTopLeft = 0; - int iRowTopLeft = 0; - int iColBottomRight = 0; - - for ( int j = 0; j < aMergedCell.length; j++ ) + Sheet sheet = WORKBOOK.getSheetAt( sheetNo - 1 ); + CellRangeAddress cellRangeAddress = null; + + for ( int i = 0; i < sheet.getNumMergedRegions(); i++ ) { - iColTopLeft = aMergedCell[j].getTopLeft().getColumn(); - iRowTopLeft = aMergedCell[j].getTopLeft().getRow(); - iColBottomRight = aMergedCell[j].getBottomRight().getColumn(); + cellRangeAddress = sheet.getMergedRegion( i ); - if ( iColTopLeft != iColBottomRight ) + if ( cellRangeAddress.getFirstColumn() != cellRangeAddress.getLastColumn() ) { - xml.append( "" - + (iColBottomRight - iColTopLeft + 1) + "" ); + xml.append( "" + + (cellRangeAddress.getLastColumn() - cellRangeAddress.getFirstColumn() + 1) + "" ); } } } - private void readingDetailsFormattedCell( Cell objCell ) + private void readingDetailsFormattedCell( org.apache.poi.ss.usermodel.Cell objCell ) { - // The format information - CellFormat format = objCell.getCellFormat(); + CellStyle format = objCell.getCellStyle(); if ( format != null ) { - xml.append( "" ); + xml.append( "" ); } } } \ No newline at end of file === modified file 'local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/ExcelUtils.java' --- local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/ExcelUtils.java 2011-07-08 07:52:59 +0000 +++ local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/ExcelUtils.java 2011-07-18 05:04:23 +0000 @@ -45,6 +45,9 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.formula.FormulaParser; import org.apache.poi.ss.formula.FormulaParsingWorkbook; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.ErrorConstants; /** * @author Tran Thanh Tri @@ -83,7 +86,7 @@ public static final String EXTENSION_XLS = ".xls"; // ------------------------------------------------------------------------- - // + // JXL methods // ------------------------------------------------------------------------- /* JXL - Get the specified cell */ @@ -134,6 +137,10 @@ } } + // ------------------------------------------------------------------------- + // POI methods + // ------------------------------------------------------------------------- + /* POI - Get the specified cell */ public static org.apache.poi.ss.usermodel.Cell getCellByPOI( int row, int column, org.apache.poi.ss.usermodel.Sheet sheetPOI ) @@ -168,7 +175,7 @@ break; case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR: - value = String.valueOf( cellPOI.getErrorCellValue() ); + value = ErrorConstants.getText( cellPOI.getErrorCellValue() ); break; case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA: @@ -176,7 +183,14 @@ break; case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC: - value = String.valueOf( cellPOI.getNumericCellValue() ); + if ( DateUtil.isCellDateFormatted( cellPOI ) ) + { + value = String.valueOf( cellPOI.getDateCellValue() ); + } + else + { + value = String.valueOf( cellPOI.getNumericCellValue() ); + } break; default: @@ -189,6 +203,59 @@ } + /* POI - Read the special value of given cell */ + public static String readSpecialValueByPOI( int row, int column, org.apache.poi.ss.usermodel.Sheet sheetPOI ) + { + org.apache.poi.ss.usermodel.Cell cellPOI = getCellByPOI( row, column, sheetPOI ); + + String value = ""; + + if ( cellPOI != null ) + { + switch ( cellPOI.getCellType() ) + { + case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING: + value = cellPOI.getRichStringCellValue().toString(); + break; + + case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN: + value = String.valueOf( cellPOI.getBooleanCellValue() ); + break; + + case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR: + value = ErrorConstants.getText( cellPOI.getErrorCellValue() ); + break; + + case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA: + try + { + value = String.valueOf( cellPOI.getNumericCellValue() ); + } + catch ( IllegalStateException ise ) + { + value = cellPOI.getCellFormula(); + } + break; + case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC: + if ( DateUtil.isCellDateFormatted( cellPOI ) ) + { + value = String.valueOf( cellPOI.getDateCellValue() ); + } + else + { + value = String.valueOf( cellPOI.getNumericCellValue() ); + } + break; + + default: + break; + } + } + + return value; + + } + public static String readValueImportingByPOI( int row, int column, org.apache.poi.ss.usermodel.Sheet sheetPOI ) { org.apache.poi.ss.usermodel.Cell cellPOI = getCellByPOI( row, column, sheetPOI ); @@ -410,6 +477,75 @@ } } + public static String convertAlignmentString( String s ) + { + if ( s.equalsIgnoreCase( "centre" ) ) + { + return "center"; + } + else + { + return s; + } + } + + public static String convertAlignmentString( Short s ) + { + String align = ""; + + switch ( s ) + { + case CellStyle.ALIGN_CENTER: + case CellStyle.ALIGN_CENTER_SELECTION: + align = "center"; + break; + + case CellStyle.ALIGN_JUSTIFY: + align = "justify"; + break; + + case CellStyle.ALIGN_LEFT: + align = "left"; + break; + + case CellStyle.ALIGN_RIGHT: + align = "right"; + break; + + default: + align = "general"; + break; + } + + return align; + } + + public static String convertVerticalString( Short s ) + { + String valign = ""; + + switch ( s ) + { + case CellStyle.VERTICAL_TOP: + valign = "top"; + break; + + case CellStyle.VERTICAL_CENTER: + valign = "center"; + break; + + case CellStyle.VERTICAL_BOTTOM: + valign = "bottom"; + break; + + default: + valign = "justify"; + break; + } + + return valign; + } + public static String checkingExcelFormula( String string_formula, int indexRow, int indexCol ) { Pattern pattern_formula = Pattern.compile( PATTERN_EXCELFORMULA ); === modified file 'local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/StringUtils.java' --- local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/StringUtils.java 2010-04-12 21:23:33 +0000 +++ local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/utils/StringUtils.java 2011-07-18 05:04:23 +0000 @@ -53,22 +53,6 @@ /* */ /* ---------------------------------------------------------------------- */ - public static String convertAlignmentString( String s ) - { - if ( s.equalsIgnoreCase( "centre" ) ) - { - return "center"; - } - else - { - return s; - } - } - - /* ---------------------------------------------------------------------- */ - /* */ - /* ---------------------------------------------------------------------- */ - public static String refiningNumberDecimalFormat( String input ) { try === modified file 'local/vn/dhis-web-excel-reporting/src/main/webapp/dhis-web-excel-reporting/javascript/imports.js' --- local/vn/dhis-web-excel-reporting/src/main/webapp/dhis-web-excel-reporting/javascript/imports.js 2011-06-29 07:29:06 +0000 +++ local/vn/dhis-web-excel-reporting/src/main/webapp/dhis-web-excel-reporting/javascript/imports.js 2011-07-18 05:04:23 +0000 @@ -60,7 +60,7 @@ for ( var i = 0 ; i < importlist.length ; i ++ ) { - importlist[i].className = 'ui-selected'; + importlist[i].className = 'ui-widget-content ui-selected'; idTemp = jQuery(importlist[i]).attr( 'id' ) + "_" + jQuery(importlist[i]).html(); @@ -79,7 +79,7 @@ for ( var i = 0 ; i < importlist.length ; i ++ ) { - importlist[i].className = 'ui-unselected'; + importlist[i].className = 'ui-widget-content ui-unselected'; } importItemIds.length = 0;