如果第1列的值相同或不同,我想查看每一行我是否尝试将.getContents()添加到单元格和工作表的末尾,但它并没有更改结果并尝试将它们转换为字符串,但结果仍然相同。每次我尝试它都会一直回来"做动作2"
我也使用JExcelAPI
w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
for(int i = 1;i<sheet.getRows(); i++){
Cell cell = sheet.getCell(0,i);
if(cell == sheet.getCell(0, (i+1))){ //If cell is the same value as the one in the row below it
//do action 1
}
else if(cell != sheet.getCell(0,(i+1))){//If cell has a different value as the one in the row below it
//do action 2
}
}
答案 0 :(得分:1)
使用Apache POI:
首先:您正在比较两个不同的单元格,不是其内容,这就是为什么总是去做 action 2 。要获得他们的内容,你要么说:
DataFormatter df = new DataFormatter();
String content = df.formatCellValue(cell);
或
String content = cell.getStringCellValue();
第一个代码片段的优点是,单元格的内容不必是字符串,它们也可以是数字,而不会抛出异常。
第二:您必须使用.equals(Object)方法而不是==运算符,因为您将要比较的两个字符串永远不会是字面上相同的对象。你的第二个也是不受欢迎的。所以你的代码看起来像这样:
DataFormatter df = new DataFormatter();
for (int i = 1; i < sheet.getLastRowNum() + 1; i++)
{
Cell cell = sheet.getRow(i).getCell(i);
if (df.formatCellValue(cell).equals(df.formatCellValue(sheet.getRow(i).getCell(0))))
{ //If cell is the same value as the one in the row below it
//do action 1
} else
{//If cell has a different value as the one in the row below it
//do action 2
}
}
答案 1 :(得分:0)
为了让它工作,我必须让cell.getcontents()返回字符串值,然后使用.equals()来比较其他cell2.getContents。
package com.example.myapplication;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
//landscape,fullscreen,no action bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//I want those 3 lines above to be one void function
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}