在apache POI中删除单元格中字符串的整个行基本步骤如下:
下面是一个示例代码,演示如何在apache POI中删除单元格中字符串的整个行:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DeleteRowExample {
public static void main(String[] args) {
String filePath = "path/to/your/excel/file.xlsx";
String sheetName = "Sheet1";
String searchString = "要删除的字符串";
try (FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheet(sheetName);
for (int i = sheet.getLastRowNum(); i >= 0; i--) {
Row row = sheet.getRow(i);
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (cell != null && cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
if (cellValue.contains(searchString)) {
row.removeCell(cell);
break;
}
}
}
}
}
try (FileOutputStream fos = new FileOutputStream(filePath)) {
workbook.write(fos);
}
System.out.println("删除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
请注意,上述示例代码中的"path/to/your/excel/file.xlsx"需要替换为你实际的Excel文件路径,"Sheet1"需要替换为你要操作的Sheet名称,"要删除的字符串"需要替换为你要删除的字符串。
推荐的腾讯云相关产品:腾讯云对象存储(COS)- 一个安全、稳定、高效、低成本的云端对象存储服务,适用于存储和处理大规模非结构化数据。产品介绍链接地址:https://cloud.tencent.com/product/cos
领取专属 10元无门槛券
手把手带您无忧上云