2011-05-03 10 views
5

मैं पीओआई एक्सएसएसएफ एपीआई का उपयोग कर रहा हूं और मैं एक चादर को ट्रांसफर करना चाहता हूं।पीओआई एसएस/एक्सएसएसएफ के साथ शीट कैसे स्थानांतरित करें?

मैं यह कैसे कर सकता हूं?

धन्यवाद।

उत्तर

4

ट्रांसफर करें, जैसा कि सी 1 के साथ बी 1 और ए 3 के साथ स्वैप ए 2 में है (इसलिए कॉलम पंक्तियां बनती हैं)?

यदि हां, तो इसमें कुछ भी नहीं बनाया गया है, इसलिए आपको स्वयं को कोडिंग करने की आवश्यकता होगी। आप शायद कोशिकाओं की एक जोड़ी पकड़ना चाहते हैं, एक (मूल्य और शैली) की सामग्री को सहेजना चाहते हैं, दूसरे को पहले कॉपी करें, फिर दूसरे को ओवरराइट करें।

quick guide देखें यदि आप सभी पढ़ने/लिखने वाले हिस्सों पर सुनिश्चित नहीं हैं।

+0

धन्यवाद, लेकिन मैं के लिए एक अपवाद मिला मूल्यों को बचाने और उन्हें वापस चिपकाने के लिए आने के बाद: सूत्र में अपवाद "मुख्य" java.lang.IllegalArgumentException: शीट सूचकांक (1) रेंज (0 से बाहर है। .0) ..... इसे हल करने के लिए मैं क्या कर सकता हूं? – Yoni

+0

मेरा अनुमान है कि आप एक ही कार्यपुस्तिका का उपयोग नहीं कर रहे हैं, या आप एक नई कार्यपुस्तिका का उपयोग कर रहे हैं जिसमें आपने कोई शीट नहीं बनाई है। इससे पहले कि आप इसे पॉप्युलेट कर सकें, आपको नई कार्यपुस्तिका में एक शीट बनाना होगा! – Gagravarr

2

मैं वही जवाब ढूंढ रहा था और इसे स्वयं कोड करना था। मैं अपने समाधान संलग्न किया है जो काफी सरल है:

  1. पंक्तियों की संख्या
  2. निर्धारित कॉलम की अधिकतम संख्या हर पंक्ति पर इस्तेमाल किया
  3. इटरेटर, और हर स्तंभ
  4. सहेजें सेल से निर्धारित उस पंक्ति/एक साधारण सूची में स्तंभ एक 'CellModel' प्रकार के रूप में
  5. इसके बाद, भर CellModels
  6. स्विच स्तंभ और पंक्ति सूचकांक पुनरावृति और चादर में CellModel बचाने

कोड मैं का उपयोग किया है है:

public static void transpose(Workbook wb, int sheetNum, boolean replaceOriginalSheet) { 
    Sheet sheet = wb.getSheetAt(sheetNum); 

    Pair<Integer, Integer> lastRowColumn = getLastRowAndLastColumn(sheet); 
    int lastRow = lastRowColumn.getFirst(); 
    int lastColumn = lastRowColumn.getSecond(); 

    LOG.debug("Sheet {} has {} rows and {} columns, transposing ...", new Object[] {sheet.getSheetName(), 1+lastRow, lastColumn}); 

    List<CellModel> allCells = new ArrayList<CellModel>(); 
    for (int rowNum = 0; rowNum <= lastRow; rowNum++) { 
     Row row = sheet.getRow(rowNum); 
     if (row == null) { 
      continue; 
     } 
     for (int columnNum = 0; columnNum < lastColumn; columnNum++) { 
      Cell cell = row.getCell(columnNum); 
      allCells.add(new CellModel(cell)); 
     } 
    } 
    LOG.debug("Read {} cells ... transposing them", allCells.size()); 

    Sheet tSheet = wb.createSheet(sheet.getSheetName() + "_transposed"); 
    for (CellModel cm : allCells) { 
     if (cm.isBlank()) { 
      continue; 
     } 

     int tRow = cm.getColNum(); 
     int tColumn = cm.getRowNum(); 

     Row row = tSheet.getRow(tRow); 
     if (row == null) { 
      row = tSheet.createRow(tRow); 
     } 

     Cell cell = row.createCell(tColumn); 
     cm.insertInto(cell); 
    } 

    lastRowColumn = getLastRowAndLastColumn(sheet); 
    lastRow = lastRowColumn.getFirst(); 
    lastColumn = lastRowColumn.getSecond(); 
    LOG.debug("Transposing done. {} now has {} rows and {} columns.", new Object[] {tSheet.getSheetName(), 1+lastRow, lastColumn}); 

    if (replaceOriginalSheet) { 
     int pos = wb.getSheetIndex(sheet); 
     wb.removeSheetAt(pos); 
     wb.setSheetOrder(tSheet.getSheetName(), pos); 
    } 

} 

private static Pair<Integer, Integer> getLastRowAndLastColumn(Sheet sheet) { 
    int lastRow = sheet.getLastRowNum(); 
    int lastColumn = 0; 
    for (Row row : sheet) { 
     if (lastColumn < row.getLastCellNum()) { 
      lastColumn = row.getLastCellNum(); 
     } 
    } 
    return new Pair<Integer, Integer>(lastRow, lastColumn); 
} 

जिससे CellModel एक आवरण है जो डेटा एक सेल निहित रखती है (यदि आप और अधिक विशेषताओं को जोड़ने कर सकते हैं यदि आप जैसे, टिप्पणी की तरह, ...) :

static class CellModel { 
    private int rowNum = -1; 
    private int colNum = -1; 
    private CellStyle cellStyle; 
    private int cellType = -1; 
    private Object cellValue; 

    public CellModel(Cell cell) { 
     if (cell != null) { 
      this.rowNum = cell.getRowIndex(); 
      this.colNum = cell.getColumnIndex(); 
      this.cellStyle = cell.getCellStyle(); 
      this.cellType = cell.getCellType(); 
      switch (this.cellType) { 
       case Cell.CELL_TYPE_BLANK: 
        break; 
       case Cell.CELL_TYPE_BOOLEAN: 
        cellValue = cell.getBooleanCellValue(); 
        break; 
       case Cell.CELL_TYPE_ERROR: 
        cellValue = cell.getErrorCellValue(); 
        break; 
       case Cell.CELL_TYPE_FORMULA: 
        cellValue = cell.getCellFormula(); 
        break; 
       case Cell.CELL_TYPE_NUMERIC: 
        cellValue = cell.getNumericCellValue(); 
        break; 
       case Cell.CELL_TYPE_STRING: 
        cellValue = cell.getRichStringCellValue(); 
        break; 
      } 
     } 
    } 

    public boolean isBlank() { 
     return this.cellType == -1 && this.rowNum == -1 && this.colNum == -1; 
    } 

    public void insertInto(Cell cell) { 
     if (isBlank()) { 
      return; 
     } 

     cell.setCellStyle(this.cellStyle); 
     cell.setCellType(this.cellType); 
     switch (this.cellType) { 
      case Cell.CELL_TYPE_BLANK: 
       break; 
      case Cell.CELL_TYPE_BOOLEAN: 
       cell.setCellValue((boolean) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_ERROR: 
       cell.setCellErrorValue((byte) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_FORMULA: 
       cell.setCellFormula((String) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       cell.setCellValue((double) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_STRING: 
       cell.setCellValue((RichTextString) this.cellValue); 
       break; 
     } 
    } 

    public CellStyle getCellStyle() { 
     return cellStyle; 
    } 

    public int getCellType() { 
     return cellType; 
    } 

    public Object getCellValue() { 
     return cellValue; 
    } 

    public int getRowNum() { 
     return rowNum; 
    } 

    public int getColNum() { 
     return colNum; 
    } 

} 
संबंधित मुद्दे