Jexcel编辑现有文件

时间:2015-09-13 04:52:03

标签: java file edit jexcelapi

package Mundo;

import java.io.*;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*; 

public class ExcelIO
{
  public static void main(String[] args) throws BiffException
  {
    toExcel("testName", "65", "180", "80", "120", "36");

    leerExcel();

}

public static void toExcel(String name, String weight, String height, String systolic, String diastolic, String temp) throws BiffException
{
    try
    {
        File inp = new File("Trina.xls");
        File out = new File("Trina.xls");

        // Abre una copia del archivo de solo lectura
        Workbook existingWorkbook = Workbook.getWorkbook(inp);
        // Abre un archivo que se puede editar
        WritableWorkbook workbook = Workbook.createWorkbook(out,existingWorkbook);


        //Revisar celda disponible
        int i = leerExcel();

        //Se obtiene la primera hoja
        WritableSheet sheet = workbook.getSheet(0); 
        WritableCell cell = sheet.getWritableCell(0, i);  



        //Creamos celdas de varios tipos
        sheet.addCell(new jxl.write.Label(0, i, name));
        sheet.addCell(new jxl.write.Number(1, i, Double.parseDouble(weight)));
        sheet.addCell(new jxl.write.Number(2, i, Double.parseDouble(height)));
        sheet.addCell(new jxl.write.Number(3, i, Double.parseDouble(systolic)));
        sheet.addCell(new jxl.write.Number(4, i, Double.parseDouble(diastolic)));
        sheet.addCell(new jxl.write.Number(5, i, Double.parseDouble(temp)));



        //Escribimos los resultados al fichero Excel
        // Important: Close it before writing the copy with copy.write();
        existingWorkbook.close();    
        //inp.close();
        workbook.write();
        workbook.close();

    }
    catch (IOException ex)
    {
        System.out.println("Error al crear el fichero.");
    }
    catch (WriteException ex)
    {
        System.out.println("Error al escribir el fichero.");
    }
}


public static int leerExcel()
{

    int i = 0;

    try
    {
        //Se abre el fichero Excel
        Workbook workbook = Workbook.getWorkbook(new File("Trina.xls")); 

        //Se obtiene la primera hoja
        Sheet sheet = workbook.getSheet(0); 

        //Revisa que la primera celda es "Nombre"
        Cell cell = sheet.getCell(0,0);

            while(!cell.getContents().isEmpty())
            {
                //Se obtiene la celda i-esima
                cell = sheet.getCell(i,0);
                i ++;
            }
        }

    catch (IOException | BiffException | IndexOutOfBoundsException ex)
    {
        System.out.println("Error!" + ex);
    }

    return i;
}
}

这是我试图创建的一个类来读取excel文件和编辑。问题是我一直收到错误的错误:

 Error!jxl.read.biff.BiffException: The input file was not found
 Error!java.lang.ArrayIndexOutOfBoundsException: 6

文件在那里,我已经仔细检查过,程序每次都会覆盖以前的文件

1 个答案:

答案 0 :(得分:1)

您的问题是您使用相同的输入和输出路径:

rmarkdown::run

尝试更改为:rmarkdown::run("test.Rmd")并在运行后,实现工作文件夹以注意新创建的Excel(在eclipse(包浏览器视图)中鼠标悬停在项目文件夹上,然后“F5”。