簡體   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