Java Code Examples for org.apache.poi.ss.usermodel.Sheet#removeRow()

The following examples show how to use org.apache.poi.ss.usermodel.Sheet#removeRow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpreadsheetDeleteRow.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute( cfSession _session, List<cfData> parameters ) throws cfmRunTimeException {
	cfSpreadSheetData	spreadsheet = null;
	String rows;
	
	/*
	 * Collect up the parameters
	 */
spreadsheet	= (cfSpreadSheetData)parameters.get(1);
rows				= parameters.get(0).getString();

Sheet	sheet = spreadsheet.getActiveSheet();
Set<Integer>	numbers	= tagUtils.getNumberSet( rows );

Iterator<Integer> it	= numbers.iterator();
while ( it.hasNext() ){
	Row row	= sheet.getRow( it.next() - 1 );
	if ( row != null )
		sheet.removeRow( row );
}

	return cfBooleanData.TRUE;
}
 
Example 2
Source File: POIUtils.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
/**
 * 指定した行を削除する。
 * <p>削除した行は上に詰める。
 * @since 0.5
 * @param sheet
 * @param rowIndex 削除する行数
 * @return 削除した行
 */
public static Row removeRow(final Sheet sheet, final int rowIndex) {

    ArgUtils.notNull(sheet, "cell");
    ArgUtils.notMin(rowIndex, 0, "rowIndex");

    final Row row = sheet.getRow(rowIndex);
    if(row == null) {
        // 削除対象の行にデータが何もない場合
        return row;
    }

    sheet.removeRow(row);

    // 上に1つ行をずらす
    int lastRow = sheet.getLastRowNum();
    if(rowIndex +1 > lastRow) {
        return row;
    }

    sheet.shiftRows(rowIndex+1, lastRow, -1);

    return row;
}
 
Example 3
Source File: ExcelDeleteQuery.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private int executeSQL() throws SQLException {
    TExcelConnection excelCon = (TExcelConnection)getConnection();
    //begin transaction,
    excelCon.beginExcelTransaction();
    Sheet currentWorkSheet = excelCon.getWorkbook().getSheet(getTargetTableName());
    for (Integer rowId : this.getResultantRows().keySet()) {
        currentWorkSheet.removeRow(currentWorkSheet.getRow(rowId + 1));
    }
    TDriverUtil.writeRecords(excelCon.getWorkbook(), excelCon.getPath());
    return this.getResultantRows().size();
}
 
Example 4
Source File: ExcelUtil.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
/**
 * 修改Excel,并另存为
 *
 * @Title: WriteExcel
 * @Date : 2014-9-11 下午01:33:59
 * @param wb
 * @param rowList
 * @param xlsPath
 */
public void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {
	
	if (wb == null) {
		out("操作文档不能为空!");
		return;
	}
	
	Sheet sheet = wb.getSheetAt(0);// 修改第一个sheet中的值
	
	// 如果每次重写,那么则从开始读取的位置写,否则果获取源文件最新的行。
	int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;
	int t = 0;// 记录最新添加的行数
	out("要添加的数据总条数为:" + rowList.size());
	for (Row row : rowList) {
		if (row == null) {
			continue;
		}
		// 判断是否已经存在该数据
		int pos = findInExcel(sheet, row);
		
		Row r = null;// 如果数据行已经存在,则获取后重写,否则自动创建新行。
		if (pos >= 0) {
			sheet.removeRow(sheet.getRow(pos));
			r = sheet.createRow(pos);
		} else {
			r = sheet.createRow(lastRowNum + t++);
		}
		
		// 用于设定单元格样式
		CellStyle newstyle = wb.createCellStyle();
		
		// 循环为新行创建单元格
		for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
			Cell cell = r.createCell(i);// 获取数据类型
			cell.setCellValue(getCellValue(row.getCell(i)));// 复制单元格的值到新的单元格
			// cell.setCellStyle(row.getCell(i).getCellStyle());//出错
			if (row.getCell(i) == null) {
				continue;
			}
			copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // 获取原来的单元格样式
			cell.setCellStyle(newstyle);// 设置样式
			// sheet.autoSizeColumn(i);//自动跳转列宽度
		}
	}
	out("其中检测到重复条数为:" + (rowList.size() - t) + " ,追加条数为:" + t);
	
	// 统一设定合并单元格
	setMergedRegion(sheet);
	
	try {
		// 重新将数据写入Excel中
		FileOutputStream outputStream = new FileOutputStream(xlsPath);
		wb.write(outputStream);
		outputStream.flush();
		outputStream.close();
	} catch (Exception e) {
		out("写入Excel时发生错误! ");
		e.printStackTrace();
	}
}