How to Read Worksheet Cells Values in Multiple Threads in Android Apps
Join the DZone community and get the full member experience.
Join For FreeThis technical tip shows how to read Excel worksheet cells values in multiple threads simultaneously inside android applications. Often you need to read worksheet cells values in multiple threads simultaneously. To do so, set Worksheet.getCells().setMultiThreadReading() to true. If you do not set this property you might get the wrong cell values. Setting it to true, you always get the correct values. To achieve this task first create a workbook and adds a worksheet. Populates the worksheet with some string values then create two threads that simultaneously read values from random cells. If the values read are correct, then nothing happens. But if the values read are incorrect, then a message box shows up in the LogCat window. If you comment this line:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
the following message will show up in LogCat window:
if (s.equals("R" + row + "C" + col)!=true)
{
System.out.println("This message box will show up when cells read values are incorrect.");
}
Otherwise, the program run without showing any message which means all values read from cells are correct.
public class ThreadProc implements Runnable {
boolean isRunning = true;
Workbook testWorkbook;
Random r = new Random();
public ThreadProc(Workbook workbook)
{
this.testWorkbook = workbook;
}
public int randomNext(int Low, int High)
{
int R = r.nextInt(High-Low) + Low;
return R;
}
public void kill()
{
this.isRunning = false;
}
public void run(){
while(this.isRunning)
{
int row = randomNext(0, 100);
int col = randomNext(0, 10);
String s = testWorkbook.getWorksheets().get(0).getCells().get(row, col).getStringValue();
if (s.equals("R" + row + "C" + col)!=true)
{
System.out.println("This message box will show up when cells read values are incorrect.");
}
}
}
}
//........MainActivity.java........
//.................................
import java.io.File;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.IWarningCallback;
import com.aspose.cells.WarningInfo;
import com.aspose.cells.WarningType;
import com.aspose.cells.Workbook;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
Workbook testWorkbook = new Workbook();
testWorkbook.getWorksheets().clear();
testWorkbook.getWorksheets().add("Sheet1");
for (int row = 0; row < 100; row++)
for (int col = 0; col < 10; col++)
testWorkbook.getWorksheets().get(0).getCells().get(row, col).setValue("R" + row + "C" + col);
//Commenting this line will show a pop-up message
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
ThreadProc tp = new ThreadProc(testWorkbook);
Thread myThread1 = new Thread(tp);
myThread1.start();
Thread myThread2 = new Thread(tp);
myThread2.start();
Thread.currentThread().sleep(5*1000);
tp.kill();
}
catch (Exception e)
{
e.printStackTrace();
}
}
.........
}
Opinions expressed by DZone contributors are their own.
Comments