下面是我的程序,我正在尝试更新Birds列,但是值(Ex:Duck)在“parrot”值之后没有更新。我想要输出如下。任何人都可以帮我解决这个问题。 程序:包配置;
import java.io.FileInputStream; import java.io.FileOutputStream; 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; import org.testng.annotations.Test; public class Writing { @Test public void test() throws Exception { FileInputStream f1=new FileInputStream("D://XL4.xlsx"); XSSFWorkbook w1=new XSSFWorkbook(f1); XSSFSheet s1=w1.getSheetAt(0); XSSFCell c = null; XSSFRow r = null; int rows=s1.getPhysicalNumberOfRows(); for(int i=0;i<=rows;i++) { r = s1.getRow(i); if(r == null) { s1.createRow((short) (i)).createCell(1).setCellValue("Duck"); } } FileOutputStream f2=new FileOutputStream("D://XL4.xlsx"); w1.write(f2); f2.close(); } } **Actual result:** Animals Birds Elephant Parrot Lion Tiger Deer Cow Duck **Expected result:** Animals Birds Elephant Parrot Lion Duck Tiger Deer Cow [Output][1] [1]: http://i.stack.imgur.com/xzTTz.pngBelow is my program where i am trying to update the Birds column but the value(Ex:Duck) is not updated after the "parrot" value.I want the output as below.Can any one help me to resolve this. Program: package config;
import java.io.FileInputStream; import java.io.FileOutputStream; 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; import org.testng.annotations.Test; public class Writing { @Test public void test() throws Exception { FileInputStream f1=new FileInputStream("D://XL4.xlsx"); XSSFWorkbook w1=new XSSFWorkbook(f1); XSSFSheet s1=w1.getSheetAt(0); XSSFCell c = null; XSSFRow r = null; int rows=s1.getPhysicalNumberOfRows(); for(int i=0;i<=rows;i++) { r = s1.getRow(i); if(r == null) { s1.createRow((short) (i)).createCell(1).setCellValue("Duck"); } } FileOutputStream f2=new FileOutputStream("D://XL4.xlsx"); w1.write(f2); f2.close(); } } **Actual result:** Animals Birds Elephant Parrot Lion Tiger Deer Cow Duck **Expected result:** Animals Birds Elephant Parrot Lion Duck Tiger Deer Cow [Output][1] [1]: http://i.stack.imgur.com/xzTTz.png最满意答案
if(r == null)是罪魁祸首。 当你在第3行时r = s1.getRow(i)将不为null。 因此,调用不会进入条件块。
将条件更改为row不为null,第二个单元格为null - 创建新单元格并在此处指定值“Duck”。
if(r == null)is the culprit here. When you are in 3rd row r = s1.getRow(i) will not be null. So, call will not come inside the condition block.
Change the condition to row is not null and second cell is null - create new cell and assign value "Duck" here.
无法使用apache poi更新excel中所需的单元格值?(Unable to update the required cell values in the excel using apache poi?) java下面是我的程序,我正在尝试更新Birds列,但是值(Ex:Duck)在“parrot”值之后没有更新。我想要输出如下。任何人都可以帮我解决这个问题。 程序:包配置;
import java.io.FileInputStream; import java.io.FileOutputStream; 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; import org.testng.annotations.Test; public class Writing { @Test public void test() throws Exception { FileInputStream f1=new FileInputStream("D://XL4.xlsx"); XSSFWorkbook w1=new XSSFWorkbook(f1); XSSFSheet s1=w1.getSheetAt(0); XSSFCell c = null; XSSFRow r = null; int rows=s1.getPhysicalNumberOfRows(); for(int i=0;i<=rows;i++) { r = s1.getRow(i); if(r == null) { s1.createRow((short) (i)).createCell(1).setCellValue("Duck"); } } FileOutputStream f2=new FileOutputStream("D://XL4.xlsx"); w1.write(f2); f2.close(); } } **Actual result:** Animals Birds Elephant Parrot Lion Tiger Deer Cow Duck **Expected result:** Animals Birds Elephant Parrot Lion Duck Tiger Deer Cow [Output][1] [1]: http://i.stack.imgur.com/xzTTz.pngBelow is my program where i am trying to update the Birds column but the value(Ex:Duck) is not updated after the "parrot" value.I want the output as below.Can any one help me to resolve this. Program: package config;
import java.io.FileInputStream; import java.io.FileOutputStream; 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; import org.testng.annotations.Test; public class Writing { @Test public void test() throws Exception { FileInputStream f1=new FileInputStream("D://XL4.xlsx"); XSSFWorkbook w1=new XSSFWorkbook(f1); XSSFSheet s1=w1.getSheetAt(0); XSSFCell c = null; XSSFRow r = null; int rows=s1.getPhysicalNumberOfRows(); for(int i=0;i<=rows;i++) { r = s1.getRow(i); if(r == null) { s1.createRow((short) (i)).createCell(1).setCellValue("Duck"); } } FileOutputStream f2=new FileOutputStream("D://XL4.xlsx"); w1.write(f2); f2.close(); } } **Actual result:** Animals Birds Elephant Parrot Lion Tiger Deer Cow Duck **Expected result:** Animals Birds Elephant Parrot Lion Duck Tiger Deer Cow [Output][1] [1]: http://i.stack.imgur.com/xzTTz.png最满意答案
if(r == null)是罪魁祸首。 当你在第3行时r = s1.getRow(i)将不为null。 因此,调用不会进入条件块。
将条件更改为row不为null,第二个单元格为null - 创建新单元格并在此处指定值“Duck”。
if(r == null)is the culprit here. When you are in 3rd row r = s1.getRow(i) will not be null. So, call will not come inside the condition block.
Change the condition to row is not null and second cell is null - create new cell and assign value "Duck" here.
发布评论