查找最后一列单元格值。如果单元格值为“无效”或“不适用”,则删除整行。
到目前为止我写的代码如下:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
public class Column
{
public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException
{
try
{
Workbook wb = WorkbookFactory.create(new FileInputStream("C:/Users/Excel1.xlsx"));
Sheet sheet = wb.getSheet("Retail-All");
Workbook wb2 = new HSSFWorkbook();
wb2 = wb;
Row row;
row = sheet.getRow(0);
int getLastCell=row.getLastCellNum()-1;
int lastIndex = sheet.getLastRowNum();
for (int i=0; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if(row.getCell(getLastCell)!=null && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid) || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
sheet.removeRow(row);
//sheet.shiftRows(i, lastIndex, -1);
}
}
FileOutputStream fileOut = new FileOutputStream("C:/Users/shiftRows.xlsx");
wb2.write(fileOut);
fileOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
使用上面的代码,使用row.setZeroHeight(true);方法我可以隐藏行。
使用上面的代码,使用sheet.removeRow(row);方法我可以清空该特定行中的所有单元格。
我在网上找到了一些代码,但没有一个是永久删除行。我的要求是永久删除行。如何编码以满足我的要求?
答案 0 :(得分:0)
在这种情况下,使用VBA宏可能会更有帮助吗? VBA宏可以嵌入到您的电子表格中,因此运行它们会更容易。
这是开始使用VBA的好地方:
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx
答案 1 :(得分:0)
试试这个...
SVTableModel model = new SVTableModel(sheet);
int lastIndex = model.getRowCount();
for (int i=1; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if((row.getCell(getLastCell)!=null) && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid") || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
row.setZeroHeight(true);
sheet.removeRow(row);
}
答案 2 :(得分:0)
您可以使用sheet.removeRow(row);
指令,而不是shiftRows(int startRow, int endRow, int n)
,这可能会在startRow和endRow n行之间移动行。
在你的情况下,它将是
// ... code before ...
for (int i=0; i<=lastIndex; i++)
{
row=sheet.getRow(i);
if(row.getCell(getLastCell)!=null && (row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid") || row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable")))
{
// This should shift up by 1 row all the rows below
sheet.shiftRows(i+1, lastIndex, -1);
i--; // Since you move row at i+1 to i you have to recompute the i-th row
}
}
// ... code after ...
检查org.apache.poi.hssf.usermodel.HSSFSheet Documentation以获取更多信息
答案 3 :(得分:0)
请尝试以下代码。我的最后一个单元格值是ot不匹配 匹配。根据您的excel进行设置。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DeleteAllRecord {
public static void main(String[] args) {
try{
File src= new File("testdata\\OSBC.xlsx");
FileInputStream fis= new FileInputStream(src);
XSSFWorkbook workbook= new XSSFWorkbook(fis);
XSSFSheet data= workbook.getSheet("TestData");
int length=data.getLastRowNum();
XSSFSheet data1;
Row row1;
for(int i=1;i<=length;i++){
String testCaseNo=data.getRow(i).getCell(4).toString();
System.out.println("The Testcase no "+testCaseNo);
data1= workbook.getSheet(testCaseNo);
row1 = data1.getRow(2);
int getLastCell=row1.getLastCellNum()-1;
System.out.println("The last cell is "+getLastCell);
int lastIndex = data1.getLastRowNum();
System.out.println("The last row is "+lastIndex);
String str1= row1.getCell(getLastCell).toString();
System.out.println("The valu1 is "+ str1);
for (int j=0; j<lastIndex-1; j++)
{
String str=data1.getRow(j+2).getCell(getLastCell).toString();
row1=data1.getRow(j+2);
System.out.println("print " + (j+2) +str);
if(row1.getCell(getLastCell)!=null &&
(row1.getCell(getLastCell).toString().equalsIgnoreCase("MATCHED")||
row1.getCell(getLastCell).toString().equalsIgnoreCase("MISMATCHED")||
row1.getCell(getLastCell).toString().equalsIgnoreCase(""))){
data1.removeRow(row1);
System.out.println("print " + (j+2)+ " " +str);
}
else{
System.out.println("Null");
}
}
FileOutputStream fileOut = new FileOutputStream("testdata\\delete.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("File deleted");
}
}
catch(Exception e){
System.out.println("No Data Exists to delete");
}
}
}