使用java-excel在excel中填充值具有主键值

使用java-excel在excel中填充值具有主键值,java,excel,Java,Excel,我正在尝试填充一个有名称的excel文件。我正在查询数据库,需要根据名称更新excel值。我有问题,因为我认为这些值会被覆盖,并且只有最后一个值存储在文本中 我正在迭代结果集,并在excel中检查名称。如果它们匹配,我将填充这些值 我的代码如下-为很多SOP道歉 public class ExcelWriteDB { /** * @param args */ public static void main(String[] args) { try { Cla

我正在尝试填充一个有名称的excel文件。我正在查询数据库,需要根据名称更新excel值。我有问题,因为我认为这些值会被覆盖,并且只有最后一个值存储在文本中

我正在迭代结果集,并在excel中检查名称。如果它们匹配,我将填充这些值

我的代码如下-为很多SOP道歉

public class ExcelWriteDB {

/**
 * @param args
 */
public static void main(String[] args) {
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection2 = null;
        // Create a connection to the database
        connection2 = DriverManager.getConnection("");
        Statement mystat2 = connection2.createStatement();

        String query = "";

        ResultSet result = mystat2.executeQuery(query); 

        ResultSetMetaData rsmd2=result.getMetaData();

        FileInputStream file = new FileInputStream(new File("source.xlsx"));

        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(2);


        while(result.next()){

        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            Cell cell = row.getCell(0);
            Cell cell3 = row.createCell(2);

            if (cell != null) {

            switch (cell.getCellType())
            {
                case Cell.CELL_TYPE_NUMERIC:
                    break;
                case Cell.CELL_TYPE_STRING:
                    if(cell.getStringCellValue().equals(result.getString("NAME"))){
                        double UPB = Double.parseDouble(result.getString("UPB"));

                        cell3.setCellValue(UPB);
                    }
                    break;
            }
            }
        }



        }// while(result.next()){

        FileOutputStream out = new FileOutputStream(new File("ZXC.xlsx"));
        workbook.write(out);
        out.close();

        file.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
公共类ExcelWriteDB{
/**
*@param args
*/
公共静态void main(字符串[]args){
尝试
{
类forName(“oracle.jdbc.driver.OracleDriver”);
连接connection2=null;
//创建到数据库的连接
connection2=DriverManager.getConnection(“”);
语句mystat2=connection2.createStatement();
字符串查询=”;
ResultSet result=mystat2.executeQuery(查询);
ResultSetMetaData rsmd2=result.getMetaData();
FileInputStream文件=新的FileInputStream(新文件(“source.xlsx”);
XSSF工作簿=新XSSF工作簿(文件);
XSSFSheet sheet=workbook.getSheetAt(2);
while(result.next()){
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext())
{
行=行迭代器。下一步();
//对于每一行,遍历所有列
迭代器cellIterator=row.cellIterator();
Cell Cell=row.getCell(0);
Cell cell3=row.createCell(2);
如果(单元格!=null){
开关(cell.getCellType())
{
case Cell.Cell\u类型\u数值:
打破
case Cell.Cell\u类型\u字符串:
if(cell.getStringCellValue().equals(result.getString(“NAME”)){
double-UPB=double.parseDouble(result.getString(“UPB”));
单元3.设置单元值(UPB);
}
打破
}
}
}
}//while(result.next()){
FileOutputStream out=新的FileOutputStream(新文件(“ZXC.xlsx”);
练习册。写(出);
out.close();
file.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}


我肯定我遗漏了什么,但我不确定是什么。感谢您的帮助。

首先,在
开关的
语句的第一个
案例中,您没有做任何事情。对吗?

其次,你根本没有在你的
单元格迭代器上迭代。你总是得到第0个单元格。而且,你根本没有向文件中写入内容。

谢谢你的回复。-这里的开关只是占位符,我稍后会添加更多逻辑。-逻辑是检查每行第一列中的名称,这就是我迭代每个ro的原因w并获取第一列。同样基于名称,我需要在结果集中找到名称的值。因此每次我都需要获取第一列并更新第三列(稍后会有更多列)-在遍历所有DB resultset后,我在下面的一个文件中进行了写入。问题是结果正常,只更新了最后一行。再次感谢。我完成了这项工作。这是一个逻辑错误,添加了以下逻辑单元格cell3=row.getCell(2);if(cell3==null){cell3=row.createCell(2);}而不是每次迭代都创建单元格。再次感谢您的帮助。