Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Java(Apache POI):将当前工作簿的变量值存储到另一工作簿的另一个变量_Java_Excel - Fatal编程技术网

Excel Java(Apache POI):将当前工作簿的变量值存储到另一工作簿的另一个变量

Excel Java(Apache POI):将当前工作簿的变量值存储到另一工作簿的另一个变量,java,excel,Java,Excel,伙计们,我的程序是这样运行的: 我有一个工作簿中的特定数据,我想使用JavaExcel(ApachePOI)将该特定数据传输到另一个工作簿。所以我将特定数据的单元格位置存储到一个变量中。现在,我试图将该变量的值设置为另一个工作簿中的另一个变量。但是该程序没有将数据传输到另一个工作簿,它只是读取每一行数据并在eclipse的输出面板中打印出来,你们能帮我吗?你能找出我的错误吗?提前感谢。 // get the workbook Data Workbook wbRES = new HS

伙计们,我的程序是这样运行的:

我有一个工作簿中的特定数据,我想使用JavaExcel(ApachePOI)将该特定数据传输到另一个工作簿。所以我将特定数据的单元格位置存储到一个变量中。现在,我试图将该变量的值设置为另一个工作簿中的另一个变量。但是该程序没有将数据传输到另一个工作簿,它只是读取每一行数据并在eclipse的输出面板中打印出来,你们能帮我吗?你能找出我的错误吗?提前感谢。

   // get the workbook Data
    Workbook wbRES = new HSSFWorkbook(new FileInputStream("C:\\input.xls"));
    Workbook wbML = new HSSFWorkbook(new FileInputStream("C:\\output.xls"));

    // get the sheet from Output
    Sheet sheetRESEARCH = wbRES.getSheet("Data");
    Sheet sheetML = wbML.getSheet("Output");

    try
    {
        // initialize variables
        int iRowCountDataRES = 1;
        Cell cPostDateRES = null;
        Cell cPostDateML = null;
        Row rPostDateRES;
        Row rPostDateML;

        // loop thru every row of data in Data Sheet
        while (sheetRESEARCH.getRow(iRowCountDataRES) != null)
        {
            //set the cell location in Data Sheet
            rPostDateRES = sheetRESEARCH.getRow(iRowCountDataRES);
            cPostDateRES = rPostDateRES.getCell(0);

            //set the cell location in Output Sheet
            rPostDateML = sheetML.createRow(iRowCountDataRES);
            cPostDateML = rPostDateML.createCell(0);

            //transfer data to Output Sheet
            cPostDateML.setCellValue(cPostDateRES.getRichStringCellValue().toString());

            //print cell value
            System.out.println(cPostDateRES.getRichStringCellValue().toString());
            //cPostDateRES.getRichStringCellValue().toString();

            //increment row value
            iRowCountDataRES = iRowCountDataRES + 1;
        }

    }

    catch(Exception e)
    {

        e.printStackTrace();
    }
    wbML.close();
    wbRES.close();

您需要将工作簿写入OutputStream,如果不写入,您希望如何保存更改

代码:


您是否尝试通过调试器运行它以查看发生了什么?一如既往:“但程序不工作。”=>请解释您的确切预期,以及发生了什么。@Seelenvirtuose我想将特定数据传输到另一个工作簿。它只读取每一行数据并在eclipse的输出面板中打印。在while循环之后,您是否将创建的工作簿写入输出流(例如文件)?@ZaidMalhis Nope。我试图将OutputStream放入,但它将所有数据传输到另一个工作簿。我只需要传输具体的数据。谢谢你!谢谢。现在我知道FileOutputStream是如何工作的了。祝你今天愉快。你也是:)。不要忘记在finally块中关闭OutputStream和工作簿,并进行适当的异常处理。
// get the workbook Data
Workbook wbRES = new HSSFWorkbook(new FileInputStream("C:\\input.xls"));
Workbook wbML = new HSSFWorkbook(new FileInputStream("C:\\output.xls"));
// get the sheet from Output
Sheet sheetRESEARCH = wbRES.getSheet("Data");
Sheet sheetML = wbML.getSheet("Output");

try
{
    // initialize variables
    int iRowCountDataRES = 1;
    Cell cPostDateRES = null;
    Cell cPostDateML = null;
    Row rPostDateRES;
    Row rPostDateML;

    // loop thru every row of data in Data Sheet
    while (sheetRESEARCH.getRow(iRowCountDataRES) != null)
    {
        //set the cell location in Data Sheet
        rPostDateRES = sheetRESEARCH.getRow(iRowCountDataRES);
        cPostDateRES = rPostDateRES.getCell(0);

        //set the cell location in Output Sheet
        rPostDateML = sheetML.createRow(iRowCountDataRES);
        cPostDateML = rPostDateML.createCell(0);

        //transfer data to Output Sheet
        cPostDateML.setCellValue(cPostDateRES.getRichStringCellValue().toString());

        //print cell value
        System.out.println(cPostDateRES.getRichStringCellValue().toString());
        //cPostDateRES.getRichStringCellValue().toString();

        //increment row value
        iRowCountDataRES = iRowCountDataRES + 1;
    }
    //add this line and do a proper exception handling and Close the OutputStream and workbooks in a finally block
    wbML.write( new FileOutputStream("C:\\output.xls"));
}

catch(Exception e)
{

    e.printStackTrace();
}
wbML.close();
wbRES.close();