Package org.jsoup.nodes

Examples of org.jsoup.nodes.Element


        int startRow = rangeAddress.getFirstRow();
        int startColumn = rangeAddress.getFirstColumn();
        int rowspan = rangeAddress.getLastRow()-startRow+1;
        int colspan = rangeAddress.getLastColumn()-startColumn+1;
        if(startRow<trs.size()){
          Element tr = trs.get(startRow);
          Elements tds = tr.children();
          if(startColumn<tds.size()){
            Element td = tds.get(startColumn);
            if(rowspan>1){
              td.attr("rowspan",String.valueOf(rowspan));
            }
            if(colspan>1){
              for(int j=0;j<rowspan;j++){
                if(startRow+j<trs.size()){
                  Element spanTr = trs.get(startRow+j);
                  Elements spanTds = spanTr.children();
                  if(startColumn<spanTds.size()){
                    Element spanTd = spanTds.get(startColumn);
                    spanTd.attr("colspan",String.valueOf(colspan));
                  }
                }
              }
            }
            if(rowspan>1||colspan>1){
View Full Code Here


  private static void mergeCells(HSSFSheet sheet, Element table) {
    Elements trs = table.select("tr");
    // 首先设置列宽
    if (trs.size() > 0) {
      for (int i = 0; i < trs.size(); i++) {
        Element tr = trs.get(i);
        Elements tds = tr.children();
        HSSFRow row = sheet.getRow(i);
        if (tds != null) {
          if (row == null) {
            row = sheet.createRow(i);
          }
          for (int j = 0; j < tds.size(); j++) {
            Element td = tds.get(j);
            int rowspan = 1;
            if (td.hasAttr("rowspan")) {
              try {
                rowspan = Integer.parseInt(td.attr("rowspan"));
              } catch (NumberFormatException e) {
                e.printStackTrace();
              }
            }
            int colspan = 1;
            if (td.hasAttr("colspan")) {
              try {
                colspan = Integer.parseInt(td.attr("colspan"));
              } catch (NumberFormatException e) {
                e.printStackTrace();
              }
            }
            if (rowspan > 1) {
View Full Code Here

    Elements trs = table.select("tr");
    int firstRow = sheet.getFirstRowNum();
    int lastRow = sheet.getLastRowNum();
    for(int i=0;i<=lastRow;i++){
      if(i<trs.size()){
        Element tr = trs.get(i);
        if(i<firstRow){//代表是空Cell,直接添加一个空的td
          for(int j=0;j<totalColumns;j++){
            tr.appendElement("td");
          }
        }else{
          Row row = sheet.getRow(i);
          if(row==null){
            for(int j=0;j<totalColumns;j++){
              tr.appendElement("td");
            }
          }else{
            int firstColumn = row.getFirstCellNum();
            int lastColumn = row.getLastCellNum();
            for(int j=0;j<totalColumns;j++){
              if(j<firstColumn){
                tr.appendElement("td");
              }else{
                Cell cell = row.getCell(j);
                Element td = tr.appendElement("td");
                if(cell!=null){
                  if(cell.getCellType()==Cell.CELL_TYPE_FORMULA){
                    td.attr("formula","="+cell.getCellFormula());
                  }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK){
                   
                  }else if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
                    td.text(String.valueOf(cell.getBooleanCellValue()));
                  }else if(cell.getCellType()==Cell.CELL_TYPE_ERROR){
                    td.text("Error:"+String.valueOf(cell.getErrorCellValue()));
                  }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
                    String strValue = null;
                    if(cell instanceof HSSFCell){
                      HSSFCell hssfCell = (HSSFCell)cell;
                      if(HSSFDateUtil.isCellDateFormatted(hssfCell)){
//                        Date date = HSSFDateUtil.getJavaDate(dv);
                        HSSFCellStyle cellStyle = hssfCell.getCellStyle();
                        if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
                           String dateFmt = cell.getCellStyle().getDataFormatString();
                              /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
                              Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
                              will always be 00 for date since "m" is minutes of the hour.*/
                           strValue = new CellDateFormatter(dateFmt).format(hssfCell.getDateCellValue());
                        }else{
                            strValue = WebVariable.sdf.format(hssfCell.getDateCellValue());
                        }
//                        td.text(strValue);
                      }else{
                        HSSFCellStyle cellStyle = hssfCell.getCellStyle();
                        if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
//                          String dataFmt = cellStyle.getDataFormatString();
                          strValue = new HSSFDataFormatter().formatRawCellContents(hssfCell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString());
                        }else{
                          strValue = String.valueOf(hssfCell.getNumericCellValue());
                        }
                      }
                    }else{
                      XSSFCell xssfCell = (XSSFCell)cell;
                      if(HSSFDateUtil.isCellDateFormatted(cell)){
                          String dateFmt = cell.getCellStyle().getDataFormatString();
                          /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
                          Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
                          will always be 00 for date since "m" is minutes of the hour.*/
                          strValue = new CellDateFormatter(dateFmt).format(cell.getDateCellValue());
//                        td.text(strValue);
                      }else{
                        XSSFCellStyle cellStyle = xssfCell.getCellStyle();
                        if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
//                           String dateFmt = cell.getCellStyle().getDataFormatString();
                              /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
                              Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
                              will always be 00 for date since "m" is minutes of the hour.*/
                           strValue = new HSSFDataFormatter().formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString());
                        }else{
                            strValue = String.valueOf(xssfCell.getNumericCellValue());
                        }
//                        xssfCell.getCellStyle().getDataFormatString();
//                        td.text(String.valueOf(cell.getNumericCellValue()));
                      }
                    }
                    td.text(strValue);
                  }else if(cell.getCellType()==Cell.CELL_TYPE_STRING){
                    td.text(cell.getStringCellValue());
                  }
                  if(sheet instanceof HSSFSheet){
                    HSSFSheet hssfSheet = (HSSFSheet)sheet;
                    HSSFCell hssfCell = (HSSFCell)cell;
                    HSSFCellStyle style = hssfCell.getCellStyle();
View Full Code Here

  private static void setCellStyleAndValue(WorkbookStyleContainer styleContainer,HSSFSheet sheet, Element table) {
    Elements trs = table.select("tr");
    // 首先设置列宽
    if (trs.size() > 0) {
      for (int i = 0; i < trs.size(); i++) {
        Element tr = trs.get(i);
        Elements tds = tr.children();
        HSSFRow row = sheet.getRow(i);
        if (tds != null) {
          if (row == null) {
            row = sheet.createRow(i);
          }
          for (int j = 0; j < tds.size(); j++) {
            Element td = tds.get(j);
            // System.out.println(td.text());
            HSSFCell cell = null;
            if (td.hasAttr("formula")) {
              String formula = td.attr("formula");
              String excelFormula = formula;
              if (formula.startsWith("=")) {
                excelFormula = formula.substring(1);
              }
              if (StringUtils.isBlank(excelFormula)) {
                String value = td.text();
                if(StringUtils.isEmpty(value)){
                  cell = row.createCell(j,
                      HSSFCell.CELL_TYPE_BLANK);
                }else{
                  if(NumberUtils.isNumber(value)){
                    cell = row.createCell(j,
                        HSSFCell.CELL_TYPE_NUMERIC);
                    cell.setCellValue(NumberUtils.toDouble(value));
                  }else{
                    cell = row.createCell(j,
                        HSSFCell.CELL_TYPE_STRING);
                    cell.setCellValue(value);
                  }
                }
              } else {
                cell = row.createCell(j,
                    HSSFCell.CELL_TYPE_FORMULA);
                try {
                  cell.setCellFormula(excelFormula);
                } catch (Exception e) {
                  e.printStackTrace();
                  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                  cell.setCellValue(excelFormula);
                }
               
              }
            } else {
              String value = td.text();
              if(StringUtils.isEmpty(value)){
                cell = row.createCell(j,
                    HSSFCell.CELL_TYPE_BLANK);
              }else{
                if(NumberUtils.isNumber(value)){
View Full Code Here

    if(html!=null){
      Document doc = Jsoup.parseBodyFragment(html);
      Elements tables = doc.select("table");
      List<Workbook> books = new ArrayList<Workbook>();
      for(int i=0;i<tables.size();i++){
        Element table = tables.get(i);
        Workbook book = HTMLAdjustMent4J.convertHTML2Excel(table.toString());
        if(book!=null){
          books.add(book);
        }
      }
      Workbook resultBook = ExcelUtility.mergeAllWorkbooks(books);
View Full Code Here

    if(element!=null&&element.tagName().equalsIgnoreCase("table")){
      Elements trs = element.select("tr");
      int totalMaxColumns = 0;
      final int[] colspans = new int[trs.size()];
      for(int tri=0;tri<trs.size();tri++){
        Element tr = trs.get(tri);
//        Elements tds = tr.select("td");
        Elements tds = tr.children();
        int totalColumns = 0;
        for(int tdi=0;tdi<tds.size();tdi++){
          Element td = tds.get(tdi);
          int colspan = 1;
          if(td.hasAttr("colspan")){
            try{
              colspan = Integer.parseInt(td.attr("colspan"));
            }catch(NumberFormatException e){
              e.printStackTrace();
            }
          }
          totalColumns += colspan;
        }
        if(totalColumns>totalMaxColumns){
          totalMaxColumns = totalColumns;
        }
      }
      for(int tri=0;tri<trs.size();tri++){
        Element tr = trs.get(tri);
        Elements tds = tr.select("td");
//        if(tds.size()<totalMaxColumns){
          int totalColumns = 0;
          for(int tdi=0;tdi<tds.size();tdi++){
            Element td = tds.get(tdi);
            int colspan = 1;
            if(td.hasAttr("colspan")){
              try{
                colspan = Integer.parseInt(td.attr("colspan"));
              }catch(NumberFormatException e){
                e.printStackTrace();
              }
              for(int coli=0;coli<colspan-1;coli++){
                td.after("<td style=\"display: none;\"/>");
              }
            }
            int rowspan = 1;
            if(td.hasAttr("rowspan")){
              try{
                rowspan = Integer.parseInt(td.attr("rowspan"));
              }catch(NumberFormatException e){
                e.printStackTrace();
              }
              for(int rowi=1;rowi<rowspan;rowi++){
                if(tri+rowi<trs.size()){
                  Element nextRow = trs.get(tri+rowi);
                  if(nextRow!=null){
                    Elements nextRowCells = nextRow.select("td");
                    for(int coli=0;coli<colspan;coli++){
                      if(nextRowCells.size()>tdi){
                        nextRowCells.get(tdi).before("<td style=\"display: none;\"/>");
                      }
                    }
                  }
                }else{
                  Element newTr = element.child(0).append("<tr/>");
                  for(int newtdi=0;newtdi<tdi;newtdi++){
                    newTr.append("<td/>");
                  }
                  Elements nextRowCells = newTr.select("td");
                  for(int coli=0;coli<colspan;coli++){
                    if(nextRowCells.size()>tdi){
                      nextRowCells.get(tdi).before("<td style=\"display: none;\"/>");
                    }
                  }
View Full Code Here

 
  public void addRows(int sheetIndex,int index,int count){
    if(sheetIndex<getNumberOfSheets()){
      int colNumber = getNumberOfColumns(sheetIndex);
      int rowNumber = getNumberOfRows(sheetIndex);
      Element table = tables.get(sheetIndex);
      Elements trs = table.select("tr");
      if(index<rowNumber){
        if(index<0){
          Element tr = trs.get(0);
          for(int i=0;i<count;i++){
            Element newTr = getDefaultTr();
            tr.before(newTr);
            for(int j=0;j<colNumber;j++){
              newTr.appendChild(getDefaultTd());
            }
          }
        }else{
          Element tr = trs.get(index);
          for(int i=0;i<count;i++){
            Element newTr = getDefaultTr();
            tr.before(newTr);
            for(int j=0;j<colNumber;j++){
              newTr.appendChild(getDefaultTd());
            }
          }
        }
      }else{
        Element tr = trs.last();
        for(int i=0;i<count;i++){
          Element newTr = getDefaultTr();
          tr.after(newTr);
          for(int j=0;j<colNumber;j++){
            newTr.appendChild(getDefaultTd());
          }
        }
      }
    }
  }
View Full Code Here

  public void addColumns(int sheetIndex,int index,int count){
    if(sheetIndex<getNumberOfSheets()){
      int colNumber = getNumberOfColumns(sheetIndex);
      int rowNumber = getNumberOfRows(sheetIndex);
      Element table = tables.get(sheetIndex);
      Elements trs = table.select("tr");
      Elements colgroup = table.select("colgroup");
      if(index<colNumber){
        if(index<0){
          for(int i=0;i<rowNumber;i++){
            Element tr = trs.get(i);
            for(int j=0;j<count;j++){
              tr.prependChild(getDefaultTd());
            }
          }
          if(colgroup.size()>0){
            for(int i=0;i<count;i++){
              colgroup.get(0).prependChild(getDefaultCol());
            }
          }
        }else{
          for(int i=0;i<rowNumber;i++){
            Element tr = trs.get(i);
            Element td = tr.children().get(index);
            for(int j=0;j<count;j++){
              td.before(getDefaultTd());
            }
          }
          if(colgroup.size()>0){
            Elements cols = colgroup.get(0).children();
            if(cols.size()>index){
              for(int i=0;i<count;i++){
                cols.get(index).before(getDefaultCol());
              }
            }
          }
        }
      }else{
        for(int i=0;i<rowNumber;i++){
          Element tr = trs.get(i);
          Element td = tr.children().last();
          for(int j=0;j<count;j++){
            td.after(getDefaultTd());
          }
        }
        if(colgroup.size()>0){
          for(int i=0;i<count;i++){
            colgroup.get(0).appendChild(getDefaultCol());
View Full Code Here

    return 0;
  }
 
  public int getNumberOfRows(int sheet){
    if(sheet<getNumberOfSheets()){
      Element table = tables.get(sheet);
      return table.select("tr").size();
    }
    return 0;
  }
View Full Code Here

    return 0;
  }
 
  public int getNumberOfColumns(int sheet){
    if(sheet<getNumberOfSheets()){
      Element table = tables.get(sheet);
      Elements trs = table.select("tr");
      if(trs.size()>0){
        Element tr = trs.get(0);
        return tr.select("td").size();
      }
    }
    return 0;
  }
View Full Code Here

TOP

Related Classes of org.jsoup.nodes.Element

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.