java 如何使用java在excel中读取和写入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11734954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:11:20  来源:igfitidea点击:

How to read and write values in excel using java?

javaexcelreportapache-poi

提问by mary hazelyn valencia

  • 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
  • 示例 Excel --> 对不起,我不允许附加图像..
  • TC 号 | 标题 | 结果
  • 1 | 状态和 Vin | 失败的
  • 2 | 州和注册代码 | 通过
  • 3 | 预订试驾 | 通过
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.

我的程序的目标是通过从我的测试中获取通过和失败的结果来生成报告。我的主要问题是如何从 excel 读取结果并将值“通过”“失败”放在Result column 下

采纳答案by Mikko Maunu

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

通过将成为 Excel 文档界面的库。一种选择是Apache POI。Excel 示例代码可以从这里找到。

Other option is Java Excel API.

其他选项是Java Excel API

回答by Shankhoneer Chakrovarty

Download the apache poi jar from hereGo through these exampleswhich demonstrates how to read/write xls data from a java program Sample help code:

这里下载 apache poi jar 通过这些示例演示如何从 Java 程序读取/写入 xls 数据示例帮助代码:

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.
  1. 创建工作簿 => 主 xls 文件
  2. 然后创建一张表
  3. 然后创建一行。
  4. 对于每一行,创建任意数量的单元格,并用不同的值填充单元格
  5. 像文件一样编写工作簿。

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

可以有多种类型的细胞中看到获取更多信息。要了解如何读取 Excel 文件:

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

有关更多信息,请参阅