[英]How to read and write values in excel using java?
- Sample Excel --> Sorry I'm not allowed to attached a image..
Excel示例->对不起,我不允许附加图像。
- TC No. |
TC号| Title |
标题| Result
结果
- 1 |
1 | State and Vin |
州和Vin | Failed
失败的
- 2 |
2 | State and Reg Code |
州和法规代码| Passed
已通过
- 3 |
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 .
我这里的主要问题是如何从excel中读取结果并将值“ Passed”或“ Failed”放置在Result列下 。
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: 从此处下载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:
整个流程是:
There can be multiple type of cells see this for 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");
}
Via library that will be your interface to Excel document. 通过库将成为您与Excel文档的接口。 One option is Apache POI .
一种选择是Apache POI 。 Excel example code can be found from here .
Excel示例代码可从此处找到。
Other option is Java Excel API . 其他选项是Java Excel API 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.