需求如下:
将多个excel文件合并成一个文件,合成后的excel文件只能是一个工作表。使用poi3.7实现。在此请教,会的说一下
注意:仅限于poi3.7 其他的请绕过

解决方案 »

  1.   

    你用POI先读取多个excel文件,分析出所有sheet的行数和列数,再构造成一个新的excel,填充数据不就可以了吗
      

  2.   

    数据能获取,样式不能获取吗?
    POI中不是有个Cell类,如果是一个cell整个的copy出来放入新的sheet中,会把样式丢了?
      

  3.   


    边框是可以设置的。。
    楼主,可以参见我的博客,上面关于POI操作Excel的,写的比较详细功能,样式管理等
      

  4.   

    做了示例,可以参考下:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.DateUtil;
    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.usermodel.WorkbookFactory;
    public class POIDemo { public static void main(String[] args) throws InvalidFormatException, IOException {
    Workbook wb = new HSSFWorkbook();
        wb.createSheet();
        
        InputStream input = new FileInputStream("filePath1");
        tranferValue(input, wb);
        input = new FileInputStream("filePath2");
        tranferValue(input, wb);
        
        FileOutputStream fileOut = new FileOutputStream("yourExcelName.xls");
        try {
         wb.write(fileOut);
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
     fileOut.close();
    } } private static void tranferValue(InputStream input, Workbook outwb) throws InvalidFormatException, IOException{
        Sheet outSheet = outwb.getSheetAt(0);
        int outLastRowNum = outSheet.getLastRowNum();
        if(outLastRowNum>0)outLastRowNum++;
        
    Workbook wb = WorkbookFactory.create(input);
    int sheetNums = wb.getNumberOfSheets();
    for(int n = 0; n < sheetNums; n++){
    Sheet sheet = wb.getSheetAt(n);
        int firstRowNum = sheet.getFirstRowNum();
        int lastRowNum = sheet.getLastRowNum();
        
        for(int i = firstRowNum; i <= lastRowNum; i++){
         Row row = sheet.getRow(i);
         if(row != null){
         Row outRow = outSheet.createRow(outLastRowNum++);
        
         int firstCellNum = row.getFirstCellNum();
         int lastCellNum = row.getLastCellNum();
        
         for(int j = firstCellNum; j < lastCellNum; j++){
         Cell cell = row.getCell(j);
         Cell outCell = outRow.createCell(j);
         if(cell != null){
         copyValue(cell, outCell);
         copyCellStyle(cell, outCell, outwb);
         }
         }
         }
        }
    }
        
    }

    private static void copyValue(Cell formCell, Cell toCell) {
    switch(formCell.getCellType()) {
          case Cell.CELL_TYPE_STRING:
           toCell.setCellValue(formCell.getRichStringCellValue());
           break;
          case Cell.CELL_TYPE_NUMERIC:
            if(DateUtil.isCellDateFormatted(formCell)) {
             toCell.setCellValue(formCell.getDateCellValue());
            } else {
             toCell.setCellValue(formCell.getNumericCellValue());
            }
            break;
          case Cell.CELL_TYPE_BOOLEAN:
           toCell.setCellValue(formCell.getBooleanCellValue());
            break;
          case Cell.CELL_TYPE_FORMULA:
           toCell.setCellValue(formCell.getCellFormula());
            break;
          default:
        
    }
    }

    private static void copyCellStyle(Cell cell, Cell cellout, Workbook wbout) {
    CellStyle cellStyleout = wbout.createCellStyle();
    cellStyleout.cloneStyleFrom(cell.getCellStyle());
    cellout.setCellStyle(cellStyleout);
    }
    }
      

  5.   

    8楼,我的做法和你的是一样的。
    这样做出来的有一个bug,就是打开文件会提示部分数据格式丢失
      

  6.   

    POI的两个workbook中的CellStyle是不能共享的,所以出现了上面的这个cloneStyleFrom();
    如果是poi生成的excel,应该不会丢失;
    但exccel中有些信息恐怕poi也不是100%的支持;