Java 在中读取excel工作表,对其进行处理并将其输出

Java 在中读取excel工作表,对其进行处理并将其输出,java,windows,eclipse,excel,jexcelapi,Java,Windows,Eclipse,Excel,Jexcelapi,我在和你玩 我尝试编写了一个小程序,它可以执行以下操作: 读取xls文件 在工作表上做一些计算,然后把它写到另一个地方 公共类数据处理器{ private String inputFile; private String outputFile; private Sheet sheet; private Workbook w; public void setInputFile(String inputFile) { this.inputFil

我在和你玩

我尝试编写了一个小程序,它可以执行以下操作:

  • 读取
    xls
    文件
  • 在工作表上做一些计算,然后把它写到另一个地方
  • 公共类数据处理器{

        private String inputFile;
        private String outputFile;
    
    
    
    private Sheet sheet;
        private Workbook w;
    
        public void setInputFile(String inputFile) {
            this.inputFile = inputFile;
        }
    
        public void setOutputFile(String outputFile) {
            this.outputFile = outputFile;
        }
    
        public void read() throws IOException {
            File inputWorkbook = new File(inputFile);
            Workbook w;
            try {
                w = Workbook.getWorkbook(inputWorkbook);
                sheet = w.getSheet(0);
            } catch (BiffException e) {
                e.printStackTrace();
            }
        }
    
        @SuppressWarnings("deprecation")
        public void write() throws IOException, WriteException {
            File file = new File(inputFile);
            WorkbookSettings wbSettings = new WorkbookSettings();
    
            wbSettings.setLocale(new Locale("en", "EN"));
    
            WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
            workbook.createSheet("Lolonator", 0);
            workbook.createSheet("Lolonator123", 1);
            workbook.copy(w);
    
            workbook.write();
            workbook.close();
        }
    
        public static void main(String[] args) throws IOException, WriteException {
            ReadExcel test = new ReadExcel();
            test.setInputFile("C:/Users/Desktop/sheet1.xls");
            test.read();
            System.out.println("####################################################");
            System.out.println("File read!");
    
    //      Write
            System.out.println("####################################################");
            System.out.println("Start to write the file!");
            WriteExcel out = new WriteExcel();
            out.setOutputFile("C:/Users/Desktop/sheet2.xls");
            out.write();
            System.out.println("Please check the result file!");
    
        }
    
    }
    

    但是,这不起作用。我的工作表中没有任何输出,即使我的程序运行到最后都没有异常。我非常感谢您的回答!!!

    在写函数中,您使用“inputFile”作为文件构造函数的参数,但在创建out对象后,您没有初始化它

    因此,写函数中的以下行

    File file = new File(inputFile);
    
    应该是

    File file = new File(outputFile);
    
    另外,您确定在运行此代码后没有看到任何错误。它应该引发空指针异常


    希望这有帮助…

    我在你的帖子中看不到ReadExcel和WriteExcel类的主体,但你不应该创建DataProcessor对象吗?还要检查你的文件是否在写函数中正确创建。你能用try-catch块围绕调用写函数的代码并打印出异常。它会给你问题原因(未找到文件、未找到路径等)。