简体   繁体   中英

How can i write into specific column and read specific column in java(Excel)?

So i'm writing an application, but im struggled now. In this application you can scan the Barcode and when it scanned then a customdialog will pop up. There is a Texview which will show the scanned barcode product name(Datas are in excel "BARCODE" "PRODUCTNAME"). On the dialog there is a Edittext where the user can enter the product quantity and finally there is button which is execute the datas into the selected excel file("BARCODE" "PRODUCTNAME" "QUANTITY").

My questions are: 1. How can i read all cell only in the first columns("A") from excel file? 2. How can i write into specified column "C" next empty cell (Still into an excel file)?

Scanner Codes

public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

private static final String LOG_TAG = "";
private ZXingScannerView zXingScannerView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set the main content layout of the Activity
    setContentView(R.layout.activity_barcode_scanner);

    ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
    zXingScannerView = new ZXingScannerView(this);
    contentFrame.addView(zXingScannerView);

}

@Override
public void onResume() {
    super.onResume();
    zXingScannerView.setResultHandler(this);
    zXingScannerView.startCamera();
}

@Override
public void onPause() {
    super.onPause();
    zXingScannerView.stopCamera();
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void handleResult(Result rawResult) {
    zXingScannerView.stopCamera();
    Toast.makeText(this, "Contents = " + rawResult.getText() +
            ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();

    ViewGroup viewGroup = findViewById(android.R.id.content);
    View dialogView = LayoutInflater.from(this).inflate(R.layout.result_dialog, viewGroup, false);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialogView);
    final AlertDialog dialog = builder.create();
    Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    TextView myAwesomeTextView = (TextView) dialogView.findViewById(R.id.resultname);
    myAwesomeTextView.setText(rawResult.getText());
    dialog.show();

    Button resultbutton = (Button) dialog.findViewById(R.id.resultbutton);
    resultbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
            zXingScannerView.startCamera();
        }
    });

    // Note:
    // * Wait 2 seconds to resume the preview.
    // * On older devices continuously stopping and resuming camera preview can result in freezing the app.
    // * I don't know why this is the case but I don't have the time to figure out.
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            zXingScannerView.resumeCameraPreview(ScanActivity.this);
        }
    }, 2000);
}

}

Reader Code

public class ReadActivity extends AppCompatActivity {

private static final String LOG_TAG = "";
public static String cellValue;
public static AssetManager assetManager;
public static HSSFWorkbook myWorkBook;
public static HSSFSheet mySheet;
public static HSSFRow myRow;

public void LoadingDatas(InputStream inputStream) throws IOException {
    assetManager = getAssets();
    inputStream = (FileInputStream) assetManager.open(SecondActivity.filePath);
    myWorkBook = new HSSFWorkbook(inputStream);
    mySheet = myWorkBook.getSheetAt(0);
    Iterator<Row> rowIter = mySheet.rowIterator();
    while (rowIter.hasNext()) {
        myRow = (HSSFRow) rowIter.next();
        Iterator<Cell> cellIter = myRow.cellIterator();
        while (cellIter.hasNext()) {

            HSSFCell myCell = (HSSFCell) cellIter.next();

            if (myCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                cellValue = myCell.getStringCellValue();
            } else {
                cellValue = String.valueOf(myCell.getNumericCellValue());
            }

            Log.v(LOG_TAG, cellValue);
        }
    }

}

}

Writer code

public class WriteActivity extends AppCompatActivity {

private static final String LOG_TAG = "";
public static String cellValue;
public static AssetManager assetManager;
public static HSSFWorkbook myWorkBook;
public static HSSFSheet mySheet;
public static HSSFRow myRow;
public static int rowstart;
public static int rowend;

public void WritingDatas(InputStream inputStream) throws IOException {
    assetManager = getAssets();
    inputStream = (FileInputStream) assetManager.open(SecondActivity.filePath);
    myWorkBook = new HSSFWorkbook(inputStream);
    mySheet = myWorkBook.getSheetAt(0);

    for (int rowNum = rowstart; rowNum < rowend; rowNum++) {
        Row r = mySheet.getRow(rowNum);
        if (r == null) {
            continue;
        }

        int lastColumn = Math.max(r.getLastCellNum(), 0);

        for (int cn = 0; cn < lastColumn; cn++) {
            Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
            if (c == null) {

            } else {

            }
        }
    }
}

}

Is it a requirement for your project that you are using an actual Excel file (.xlss)? If all you're trying to do is essentially use Excel as a database then you would be better off using a.csv format that you can open in Excel if you require (you'd actually be better off using an actual database, although that might not be a satisfying answer).

There are numerous guides to writing between Java and CSV, here is one: https://www.baeldung.com/java-csv

If you are adamant that you need to use Excel then the following library exists that will simply your IO http://jxls.sourceforge.net/getting_started.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM