简体   繁体   中英

How to read and write values in excel using java?

  • Sample Excel --> Sorry I'm not allowed to attached a image..
  • TC No. | Title | Result
  • 1 | State and Vin | Failed
  • 2 | State and Reg Code | Passed
  • 3 | Booking a Test Drive | Passed
public class sampleTest{
public static void main(String[] args) throws Exception {
         int iTest = 2, iTest2 = 3;
          if (iTest == iTest2){
              //It will pass a value into the excel (e.g. "Passed")
          }
          else{
             //It will pass a value into the excel (e.g. "Failed")
          }
    }

My program's goal is to generate a report by getting the Passed and Failed results from my tests. My main problem here is on how to read the results from the excel and place the value "Passed" or "Failed" under Result column .

Download the apache poi jar from here Go through these examples which demonstrates how to read/write xls data from a java program Sample help code:

public static void main(String[] args) throws IOException {
        Workbook wb = new HSSFWorkbook(); 
        Sheet sheet = wb.createSheet("sheet");
        Row row = sheet.createRow((short) 0);

        row.createCell(0).setCellValue(1.2);
        row.createCell(1).setCellValue(wb.getCreationHelper().createRichTextString("This is a string"));
        row.createCell(2).setCellValue(true);

        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }

This might help you to get started. The whole flow is:

  1. Create a workbook => The main xls file
  2. Then create a sheet
  3. Then create a row.
  4. For each row create as many cells as you want and fill the cells with different values
  5. Write the workbook like a file.

There can be multiple type of cells see this for more info. To know how to read an excel file:

InputStream myxls = new FileInputStream("workbook.xls");
        wb     = new HSSFWorkbook(myxls);


         sheet = wb.getSheetAt(0);       // first sheet
         row     = sheet.getRow(0);        // third row
        HSSFCell cell   = (HSSFCell) row.getCell((short)1);  // fourth cell
        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            System.out.println("The Cell was a String with value \" " + cell.getStringCellValue()+" \" ");
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            System.out.println("The cell was a number " + cell.getNumericCellValue());
        } else {
            System.out.println("The cell was nothing we're interested in");
        }

For more info see this

Via library that will be your interface to Excel document. One option is Apache POI . Excel example code can be found from here .

Other option is Java Excel API .

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