<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    love fish大鵬一曰同風起,扶搖直上九萬里

    常用鏈接

    統計

    積分與排名

    friends

    link

    最新評論

    POI 復制excel一行到另外一個sheet頁(轉)

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.Region;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    public class RowCopy {

     
    /**
      * 
    @param args
      * 
    @throws IOException
      * 
    @throws FileNotFoundException
      
    */


     @SuppressWarnings(
    "deprecation")
     
    public static void main(String[] args) {
      
    try {
       POIFSFileSystem fs 
    = new POIFSFileSystem(new FileInputStream(
         
    "exlsample.xls"));
       HSSFWorkbook wb 
    = new HSSFWorkbook(fs);

    //source ,target 為,源sheet 頁和目標sheet頁,
       copyRows(wb, "source""target"3420);
       FileOutputStream fileOut 
    = new FileOutputStream("exlsample.xls");
       wb.write(fileOut);
       fileOut.flush();
       fileOut.close();
       System.out.println(
    "Operation finished");
      }
     catch (Exception e) {
       e.printStackTrace();
      }

     }


     
    public static void copyRows(HSSFWorkbook wb, String pSourceSheetName,
       String pTargetSheetName, 
    int pStartRow, int pEndRow, int pPosition) {
      HSSFRow sourceRow 
    = null;
      HSSFRow targetRow 
    = null;
      HSSFCell sourceCell 
    = null;
      HSSFCell targetCell 
    = null;
      HSSFSheet sourceSheet 
    = null;
      HSSFSheet targetSheet 
    = null;
      Region region 
    = null;
      
    int cType;
      
    int i;
      
    short j;
      
    int targetRowFrom;
      
    int targetRowTo;

      
    if ((pStartRow == -1|| (pEndRow == -1)) {
       
    return;
      }

      sourceSheet 
    = wb.getSheet(pSourceSheetName);
      targetSheet 
    = wb.getSheet(pTargetSheetName);
      
    // 拷貝合并的單元格
      for (i = 0; i < sourceSheet.getNumMergedRegions(); i++{
       region 
    = sourceSheet.getMergedRegionAt(i);
       
    if ((region.getRowFrom() >= pStartRow)
         
    && (region.getRowTo() <= pEndRow)) {
        targetRowFrom 
    = region.getRowFrom() - pStartRow + pPosition;
        targetRowTo 
    = region.getRowTo() - pStartRow + pPosition;
        region.setRowFrom(targetRowFrom);
        region.setRowTo(targetRowTo);
        targetSheet.addMergedRegion(region);
       }

      }

      
    // 設置列寬
      for (i = pStartRow; i <= pEndRow; i++{
       sourceRow 
    = sourceSheet.getRow(i);
       
    if (sourceRow != null{
        
    for (j = sourceRow.getLastCellNum(); j > sourceRow
          .getFirstCellNum(); j
    --{
         targetSheet
           .setColumnWidth(j, sourceSheet.getColumnWidth(j));
         targetSheet.setColumnHidden(j, 
    false);
        }

        
    break;
       }

      }

      
    // 拷貝行并填充數據
      for (; i <= pEndRow; i++{
       sourceRow 
    = sourceSheet.getRow(i);
       
    if (sourceRow == null{
        
    continue;
       }

       targetRow 
    = targetSheet.createRow(i - pStartRow + pPosition);
       targetRow.setHeight(sourceRow.getHeight());
       
    for (j = sourceRow.getFirstCellNum(); j < sourceRow
         .getPhysicalNumberOfCells(); j
    ++{
        sourceCell 
    = sourceRow.getCell(j);
        
    if (sourceCell == null{
         
    continue;
        }

        targetCell 
    = targetRow.createCell(j);
        targetCell.setEncoding(sourceCell.getEncoding());
        targetCell.setCellStyle(sourceCell.getCellStyle());
        cType 
    = sourceCell.getCellType();
        targetCell.setCellType(cType);
        
    switch (cType) {
        
    case HSSFCell.CELL_TYPE_BOOLEAN:
         targetCell.setCellValue(sourceCell.getBooleanCellValue());
         System.out.println(
    "--------TYPE_BOOLEAN:"
           
    + targetCell.getBooleanCellValue());
         
    break;
        
    case HSSFCell.CELL_TYPE_ERROR:
         targetCell
           .setCellErrorValue(sourceCell.getErrorCellValue());
         System.out.println(
    "--------TYPE_ERROR:"
           
    + targetCell.getErrorCellValue());
         
    break;
        
    case HSSFCell.CELL_TYPE_FORMULA:
         
    // parseFormula這個函數的用途在后面說明
         targetCell.setCellFormula(parseFormula(sourceCell
           .getCellFormula()));
         System.out.println(
    "--------TYPE_FORMULA:"
           
    + targetCell.getCellFormula());
         
    break;
        
    case HSSFCell.CELL_TYPE_NUMERIC:
         targetCell.setCellValue(sourceCell.getNumericCellValue());
         System.out.println(
    "--------TYPE_NUMERIC:"
           
    + targetCell.getNumericCellValue());
         
    break;
        
    case HSSFCell.CELL_TYPE_STRING:
         targetCell
           .setCellValue(sourceCell.getRichStringCellValue());
         System.out.println(
    "--------TYPE_STRING:" + i
           
    + targetCell.getRichStringCellValue());
         
    break;
        }


       }


      }


     }


     
    private static String parseFormula(String pPOIFormula) {
      
    final String cstReplaceString = "ATTR(semiVolatile)"//$NON-NLS-1$
      StringBuffer result = null;
      
    int index;

      result 
    = new StringBuffer();
      index 
    = pPOIFormula.indexOf(cstReplaceString);
      
    if (index >= 0{
       result.append(pPOIFormula.substring(
    0, index));
       result.append(pPOIFormula.substring(index
         
    + cstReplaceString.length()));
      }
     else {
       result.append(pPOIFormula);
      }


      
    return result.toString();
     }


    }


    posted on 2007-11-14 16:51 liaojiyong 閱讀(8318) 評論(1)  編輯  收藏 所屬分類: POI

    評論

    # re: POI 復制excel一行到另外一個sheet頁(轉)[未登錄] 2011-12-26 16:02 zz

    請問pPosition參數是什么?  回復  更多評論   

    主站蜘蛛池模板: 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久精品7亚洲午夜a| 一级特级aaaa毛片免费观看 | 成人免费毛片视频| 亚洲av无码久久忘忧草| 免费看成人AA片无码视频羞羞网| 色拍自拍亚洲综合图区| 最近免费中文字幕大全高清大全1| 亚洲精品免费在线观看| 99久久免费观看| 亚洲国产亚洲片在线观看播放| 亚洲一区免费视频| 亚洲av乱码一区二区三区香蕉| 欧美好看的免费电影在线观看 | 亚洲成AV人片在线观看| 日韩电影免费在线观看中文字幕 | 亚洲免费无码在线| 在线成人精品国产区免费| 一区二区三区亚洲| 免费99精品国产自在现线| 亚洲精品国产综合久久久久紧| 国产免费爽爽视频免费可以看| 免费一级毛suv好看的国产网站 | 亚洲美女大bbbbbbbbb| a毛片基地免费全部视频| 亚洲AV日韩AV一区二区三曲| 亚洲人成影院在线观看| 日韩精品免费视频| 最新亚洲春色Av无码专区| 免费一级e一片在线播放| a在线视频免费观看| 91亚洲国产成人久久精品网址| 在线精品免费视频无码的| 一级特黄aaa大片免费看| 久久精品国产亚洲AV香蕉| 成人免费777777| a级毛片免费高清视频| 亚洲视频国产精品| 内射无码专区久久亚洲| 免费人成在线观看网站品爱网| 亚洲AV日韩综合一区尤物|