Using Apache POI 3.16 Eclipse IDE neon 3 Selenium 3.4 (not that it matters in this case)
使用Apache POI 3.16 Eclipse IDE的氖3 * 3(不认为在本例中很重要)
I'm having an issue with writing values to an excel spreadsheet then reading back the value.
我遇到了一个问题,就是将值写到excel电子表格中,然后再读取这些值。
Here's what I want to do at a high level:
以下是我在高水平上想做的:
- Open up an excel file
- 打开一个excel文件
- write to row 1 column 1 (we are using index starting at 0)
- 写入第1行第1列(我们使用从0开始的索引)
- Read back what was written in that cell.
- 读回写在那个单元格中的内容。
The cell contains the value "B2". In a setCellData() function, I write to the cell a "Hello World" and have the function return the contents of the cell. I also have a separate function that reads in the contents of a specified cell.
单元格包含值“B2”。在setCellData()函数中,我向单元格写入“Hello World”,并让函数返回单元格的内容。我还有一个单独的函数,它读取指定单元格的内容。
When I run the following code:
当我运行以下代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Udemy_Excel_Driven {
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static XSSFRow row;
public static XSSFCell cell;
public static FileInputStream fis;
public static void main(String[] args) throws IOException, Exception
{
System.out.println("Before cell edit value is:");
System.out.println(getCellData(1,1));
String value = setCellData(1,1,"Hello World");
System.out.println("What's the value after setting it with setCellData()?");
System.out.println(value);
System.out.println("What's the value using getCellData()?");
System.out.println(getCellData(1,1));
}
public static String getCellData(int rowNum, int colNum) throws IOException
{
/*
* Hierarchy of excel data:
*
* Workbook - take control of this
* Sheet - pick up the sheet of the workbook
* Row - pick the row
* Column - after picking the row, select the column
* Value - grab the value from the cell
*
*/
//0. = identify the path to the excel file in the system.
fis = new FileInputStream("C:\\data.xlsx");
//1. Create a new XSSFWorkbook object. You need to pass in a FileInputStream object into it, which you created earlier.
wb = new XSSFWorkbook(fis);
//2. Get the sheet in the workbook. Create a new XSSFsheet object and set it to the sheet in the workbook
// Access the workbook method "getSheet" and pass in the name of the sheet
sheet = wb.getSheet("script");
//3. Get the row and column. We are going to access the data from row 2 column 2. And remember the indices start at 0.
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
//get the value specified in the row and cell
return cell.getStringCellValue();
}
public static String setCellData(int rowNum, int colNum, String data) throws IOException
{
fis = new FileInputStream("C:\\data.xlsx");
wb = new XSSFWorkbook(fis);
sheet = wb.getSheet("script");
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
cell.setCellValue(data);
String cellData = cell.getStringCellValue();
return cellData;
}
I get the following output:
得到如下输出:
Before cell edit value is:
B2
What's the value after setting it with setCellData()?
Hello World
What's the value using getCellData()?
B2
I don't think the write actually occurred since I opened up the excel file and the "Hello World" string wasn't in the specified cell. Any answers to this issue?
我认为,自从我打开excel文件并在指定的单元格中没有出现“Hello World”字符串之后,实际上并没有发生写操作。对这个问题有什么回答吗?
1 个解决方案
#1
1
I don't see any part of your code that actually writes to your file. It should more or less look something like this:
我看不出你的代码中有哪一部分是写进你的文件的。它或多或少应该是这样的:
FileOutputStream fileOut = new FileOutputStream("C:\\data.xlsx");
wb.write(fileOut);
fileOut.close();
You can also consult this guide for this issue as well as other functionality that you might be interested in implementing.
您还可以参考本指南,了解这个问题以及您可能感兴趣的其他功能。
#1
1
I don't see any part of your code that actually writes to your file. It should more or less look something like this:
我看不出你的代码中有哪一部分是写进你的文件的。它或多或少应该是这样的:
FileOutputStream fileOut = new FileOutputStream("C:\\data.xlsx");
wb.write(fileOut);
fileOut.close();
You can also consult this guide for this issue as well as other functionality that you might be interested in implementing.
您还可以参考本指南,了解这个问题以及您可能感兴趣的其他功能。