poi手动删除指定的列

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class DelCellUtil {


    
     //删除列
    public static void delCell(Sheet sheet, int cellValue) {
      
        for (int r = 0; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            for (int c = cellValue; c <= row.getLastCellNum(); c++) {
                Cell cOld = row.getCell(c);
                if (cOld != null) {
                    //删除列值
                    row.removeCell(cOld);
                }
                    //获取删除值列的 下一列
                Cell cellNext = row.getCell(c + 1);
                if (cellNext != null) {
                    Cell cellNew = row.createCell(c, cNext.getCellType());
                    copyCell(cellNew, cellNext);
                    if (r == 0) {
                        sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1));
                    }
                }
            }
        }
    }

   
   // 列 左移 复制样式和值
    private static void copyCell(Cell cellNew, Cell cellNext) {
        cNew.setCellStyle(cOld.getCellStyle());
		//不同版本的poi此处判断不一样
        
		if (0 == cellNext.getCellType()) {
            cNew.setCellValue(cellNext.getNumericCellValue());
        } 
		if (1 == cellNext.getCellType()) {
            cellNew.setCellValue(cellNext.getStringCellValue());
        }
		if (2 == cellNext.getCellType()) {
            cellNew.setCellValue(cellNext.getCellFormula());
        }
		 
		if (4 == cellNext.getCellType()) {
            cellNew.setCellValue(cellNext.getBooleanCellValue());
        } 
		if (5 == cellNext.getCellType()) {
            cellNew.setCellValue(cellNext.getErrorCellValue());
        } 
		
    }

}

你可能感兴趣的:(开发语言,java)