gistfile1.txt import org.apache.poi.hssf.usermodel.*;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import java.io.*;import java.util.Iterator;/** * @Description: * @author: * @date: 2017/8/16 14:47 * @Compan
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import java.io.*; import java.util.Iterator; /** * @Description: * @author: * @date: 2017/8/16 14:47 * @Company: * @version: V1.0 */ public class Main { public static void main(String[] args) throws IOException { File file = new File("C:\\Users\\linxy\\Downloads\\8月15\\8月15\\人员调动_1502849124421"); File inFile = new File("d:\\调入.xls"); File outFile = new File("d:\\调出.xls"); FileOutputStream instream = new FileOutputStream(inFile); HSSFWorkbook inWorkbook = new HSSFWorkbook(); HSSFSheet inSheet = inWorkbook.createSheet(); int inRow = 0; FileOutputStream outstream = new FileOutputStream(outFile); HSSFWorkbook outWorkbook = new HSSFWorkbook(); HSSFSheet outSheet = outWorkbook.createSheet(); int outRow = 0; for(File f: file.listFiles()) { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(f)); for(int sidx = 0; sidx < workbook.getNumberOfSheets(); sidx++) { HSSFSheet sheet = workbook.getSheetAt(sidx); boolean callIn = false; for (Iterator iterator = sheet.rowIterator(); iterator.hasNext(); ) { Row row = (Row)iterator.next(); boolean header = false; if("调入日期".equals(row.getCell(13).getStringCellValue().trim())) { header = true; callIn = true; } if("调出日期".equals(row.getCell(13).getStringCellValue().trim())) { header = true; callIn = false; } if(!header) { if(callIn) { copyRow(inSheet, inRow++, row); } else { copyRow(outSheet, outRow++, row); } } } } } outWorkbook.write(outstream); inWorkbook.write(instream); outWorkbook.close(); inWorkbook.close(); instream.close(); outstream.close(); } private static void copyRow(HSSFSheet resultSheet, int destinationRowNum, Row sourceRow) { HSSFRow newRow = resultSheet.createRow(destinationRowNum); // Loop through source columns to add to new row for (int i = 0; i < sourceRow.getLastCellNum(); i++) { // Grab a copy of the old/new cell Cell oldCell = sourceRow.getCell(i); HSSFCell newCell = newRow.createCell(i); // If the old cell is null jump to next cell if (oldCell == null) { newCell = null; continue; } // Set the cell data type newCell.setCellType(oldCell.getCellType()); // Set the cell data value switch (oldCell.getCellType()) { case Cell.CELL_TYPE_BLANK: newCell.setCellValue(oldCell.getStringCellValue()); System.out.print(oldCell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); System.out.print(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); System.out.print(oldCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); System.out.print(oldCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); System.out.print(oldCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); System.out.print(oldCell.getRichStringCellValue()); break; } } } }