/******************************************************** * function : count the number of valid rows in a given sheet * ******************************************************* * parameters: * wb : workbook which contains the sheet we want * indexSheet : the index of the sheet in the xb (start from 0) *************************************************************** */ publicstaticintgetSheetRowNumber(XSSFWorkbook wb,int indexSheet) { XSSFSheet sheet=wb.getSheetAt(indexSheet); int count=0; int begin = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); for (int i = begin; i <= end; i++) { if (null == sheet.getRow(i)|| getCellValue(sheet.getRow(i).getCell(0)) == "" || null==sheet.getRow(i).getCell(0)) { continue; } else count++; } return count; }
/********************************************************** * Excel column index begin 1 * @param colStr * @param length * @return **********************************************************/ publicstaticintexcelColStrToNum(String colStr, int length){ int num = 0; int result = 0; for(int i = 0; i < length; i++) { char ch = colStr.charAt(length - i - 1); num = (int)(ch - 'A' + 1) ; num *= Math.pow(26, i); result += num; } return result; }
/******************************************************** * function : copy a part of cells in different workbooks * ******************************************************* * @param: * xbIn : the resource workbook * xbOut : the destination workbook * indexSheetIn : the index of the sheet in the xbIn (start from 0) * indexSheetOut : the index of the sheet in the xbOut (start from 0) * rowInStart : the number of the first row that we need in the indexSheetIn ( start from 1 ) * rowInEnd : the number of the last row that we need in the indexSheetIn ( start from 1 ) * colInStart : the number of the first column that we need in the indexSheetIn ( start from 1, represent A in the sheet ) * colInEnd : the number of the last column that we need in the indexSheetIn ( start from 1, represent A in the sheet ) * rowOutStart : the number of the first row that we want to put cells in the indexSheetOut ( start from 1 ) * colOutStart : the number of the first column that we want to put cells in the indexSheetOut ( start from 1, represent A in the sheet ) * *******************************************************/ public static boolean copyCells(XSSFWorkbook wbIn,XSSFWorkbook wbOut,int indexSheetIn,int indexSheetOut, int rowInStart,int colInStart,int rowInEnd,int colINEnd,int rowOutStart,int colOutStart ) { XSSFSheet sheetIn=wbIn.getSheetAt(indexSheetIn); XSSFRow rowIn; XSSFCell cellIn; XSSFCellStyle cellStyleIn; String cellInValue; XSSFSheet sheetOut=wbOut.getSheetAt(indexSheetOut); XSSFRow rowOut; XSSFCell cellOut; XSSFCellStyle cellStyleOut; int rowNum=rowInEnd-rowInStart; int colNum=colINEnd-colInStart; for(int i=0;i<rowNum+1;i++) { rowIn=sheetIn.getRow(rowInStart+i-1); rowOut = sheetOut.createRow(rowOutStart+i-1); //set the copied row's height rowOut.setHeight(rowIn.getHeight()); for(int j=0;j<colNum+1;j++) { cellIn=rowIn.getCell(colInStart+j-1); if(cellIn!=null) { cellInValue=getCellValue(cellIn); cellOut=rowOut.createCell(colOutStart+j-1); cellStyleIn=cellIn.getCellStyle(); cellStyleOut=wbOut.createCellStyle(); cellStyleOut.cloneStyleFrom(cellStyleIn); cellOut.setCellStyle(cellStyleOut); cellOut.setCellValue(cellInValue); } } } //in order to deal with merged regions java.util.List<CellRangeAddress> regions=sheetIn.getMergedRegions(); for(CellRangeAddress cellRangeAddress : regions) { if(cellRangeAddress.getFirstColumn()>=colInStart-1&& cellRangeAddress.getLastColumn()<=colINEnd-1&& cellRangeAddress.getFirstRow()>=rowInStart-1&& cellRangeAddress.getLastRow()<=rowInEnd-1) { int diffrow=rowOutStart-rowInStart; int diffcol=colOutStart-colInStart; int firstRow=cellRangeAddress.getFirstRow()+diffrow; int firstCol=cellRangeAddress.getFirstColumn()+diffcol; int lastRow=firstRow+cellRangeAddress.getLastRow()-cellRangeAddress.getFirstRow(); int lastCol=firstCol+cellRangeAddress.getLastColumn()-cellRangeAddress.getFirstColumn(); CellRangeAddress cellRangeAddressNew=new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheetOut.addMergedRegion(cellRangeAddressNew); } } //set the copied column's width for(int columnIndex=colInStart-1;columnIndex<=colINEnd-1;columnIndex++) { int width=sheetIn.getColumnWidth(columnIndex); sheetOut.setColumnWidth(columnIndex, width); } return true; }