Apache POI在下一个空白行中插入金额

时间:2015-11-01 12:07:47

标签: java android apache-poi

更新 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行写入任何值,依此类推。对不起我的英文自动翻译。

1 个答案:

答案 0 :(得分:0)

你应该可以使用

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

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