繁体   English   中英

Apache POI在下一个空白行中插入数量

[英]Apache POI insert amount in the next blank line

更新 CENIC的响应起作用了,但是现在当我按下保存按钮时,电子表格正在多次写入数据,而不仅仅是一个,它遵循更新的代码。

//Read the spreadsheet that needs to be updated
        FileInputStream input_document = new FileInputStream(new File("sdcard/AperamApps/DBQ/DBQmestre/historico/historico.xls"));
        //Access the workbook
        HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
        //Access the worksheet, so that we can update / modify it.
        HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 



        {


       Row newRow = my_worksheet.createRow(my_worksheet.getLastRowNum() + 1);
       newRow.createCell(0).setCellValue("Unidade Metálica");
       newRow.createCell(1).setCellValue("Local");
       newRow.createCell(2).setCellValue("Posição");
       newRow.createCell(3).setCellValue("Empilhamento");
       newRow.createCell(4).setCellValue("Hora Recebida");        
       newRow.createCell(5).setCellValue("Hora Realizado");

       Row header = my_worksheet.createRow(my_worksheet.getLastRowNum() + 1);
       header.createCell(0).setCellValue(item1);
       header.createCell(1).setCellValue(local1);
       header.createCell(2).setCellValue(posicao1);
       header.createCell(3).setCellValue(emp1);
       header.createCell(4).setCellValue(hr);
       header.createCell(5).setCellValue(timeStamp); 
        }


        input_document.close();

        //Open FileOutputStream to write updates
        FileOutputStream output_file =new FileOutputStream(new File("sdcard/AperamApps/DBQ/DBQmestre/historico/historico.xls"));
        //write changes
        my_xls_workbook.write(output_file);
        //close the stream
        output_file.close();

打印 https://uploaddeimagens.com.br/images/000/537/859/full/Sem_t%C3%ADtulo.png?1446470667

我正在开发一个将数据写入excel电子表格以保存历史记录的应用程序,但我不会以任何方式导致将数据插入到空白的下一行中,而是遵循您希望完成此任务的代码。

public void salvar1acao (View view) throws IOException, BiffException, WriteException {

    String item1 = ((String) txtcoluna1.getText().toString());
    String local1 = ((String) txtlocal1.getText().toString());
    String posicao1 = ((String) txtposicao1.getText().toString());
    String emp1 = ((String) txtemp1.getText().toString());

    Workbook workbook = Workbook.getWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/p-comando.xls"));
    WritableWorkbook copy = Workbook.createWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/p-comando.xls"), workbook);

    //Trabalhando com a API POI para escrever de forma mais eficiênte no histórico
    String timeStamp = new SimpleDateFormat("dd/MM/yyyy_HH:mm:ss",
            Locale.getDefault()).format(new Date());

    HSSFWorkbook historico = new HSSFWorkbook();
    HSSFSheet sheet = historico.createSheet("Histórico");

    @SuppressWarnings("unused")
    boolean linha = historico.getSheet(null) != null;



   Row dataRow = sheet.createRow(0);
   dataRow.createCell(0).setCellValue("Unidade Metálica");
   dataRow.createCell(1).setCellValue("Local");
   dataRow.createCell(2).setCellValue("Posição");
   dataRow.createCell(3).setCellValue("Empilhamento");
   dataRow.createCell(4).setCellValue("Hora Recebida");        
   dataRow.createCell(5).setCellValue("Hora Realizado");

    Row header = sheet.createRow(1);
    header.createCell(0).setCellValue(item1);
    header.createCell(1).setCellValue(local1);
    header.createCell(2).setCellValue(posicao1);
    header.createCell(3).setCellValue(emp1);
    header.createCell(5).setCellValue(timeStamp);



    try {
        FileOutputStream out = 
                new FileOutputStream(new File("sdcard/AperamApps/DBQ/DBQmestre/historico/historico.xls"));
        historico.write(out);
        out.close();
        System.out.println("Gravado com sucesso..");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

基本原理如下,如果该行具有任何值,则在第2行中写入,第2行在第3行中写入任何值,依此类推。 对不起,我的英语自动翻译。

您应该可以使用

Row newRow = sheet.createRow(sheet.getLastRowNum() + 1);

然后通过此行填充单元格。 这样,您将始终在工作表的末尾添加新行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM