首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JExcelApi导入图纸

使用JExcelApi导入图纸
EN

Stack Overflow用户
提问于 2012-03-13 04:32:28
回答 1查看 4K关注 0票数 1

首先我要说的是,我对Java和这个站点都是非常陌生的。我读了一两本书,从那时起,我一直在寻找一些小项目来保持自己的娱乐。我试着对此进行研究,但我还没能找到我需要的信息。话虽如此,这是我在这里的第一个问题,所以如果这是非常早期的初学者的东西,并且我遗漏了一些明显的东西,我道歉。

关于这个项目,我的妹夫在工作中遇到了一个问题,他在一个文件夹中有大约90个Excel工作簿,他需要将每个报告的第一个工作表合并到一个主工作簿中。他可以手动完成这项工作,但我认为尝试找出一种用Java完成这项工作的方法会很有趣。

我做了一些研究,下载了JExcelAPI,并将.jar添加到我的类路径中。我在我的计算机上创建了两个目录。

C:\Excel\

C:\Excel\已完成\

在C:\ excel \中,我创建了两个虚拟的excel工作表。出于测试目的,我重命名了每个页面上的第一个工作表。在完成的文件夹中,我已经创建了我的主文档,我打算将这些工作表合并到其中。当工作表为空时,我运行此命令,工作表似乎被复制过来。主文件中有两个工作表,它们的名称与我在它们各自的工作簿中为它们指定的名称相对应,所以我假设这是有效的。但是,当我向其中一个工作表添加信息并尝试运行此操作时,我得到一个空指针异常。我已经在这上面工作了几个小时了,所以也许我只是需要休息一下,但我不知道出了什么问题。我访问了JExcelAPI的网站,尝试了一种看起来过时的方法(在importSheet()出现之前)。这也不起作用,而且还返回了一个空指针异常。

如果有人有时间并且熟悉JExcelAPI,你能让我知道出了什么问题吗?我真的很感激。我已经在下面发布了错误和我的代码。

--错误--

代码语言:javascript
复制
spreadsheet1.xls
Exception in thread "main" java.lang.NullPointerException
at jxl.write.biff.SheetCopier.deepCopyCells(SheetCopier.java:996)
at jxl.write.biff.SheetCopier.importSheet(SheetCopier.java:542)
at jxl.write.biff.WritableSheetImpl.importSheet(WritableSheetImpl.java:2699)
at jxl.write.biff.WritableWorkbookImpl.importSheet(WritableWorkbookImpl.java:1897)
at sheetcopier.SheetCopier.main(SheetCopier.java:32)

--代码--

代码语言:javascript
复制
package sheetcopier;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.nio.file.Paths;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class SheetCopier {

public static void main(String[] args) throws WriteException, BiffException {
    Path inputpath = Paths.get("C:/Excel"); //Directory with excel documents to be copied
    File outputfile = new File("C:/ExcelFinished/finishedbook.xls"); //Master/End file

    //Read all files from directory
    try (DirectoryStream<Path> inputfiles = Files.newDirectoryStream(inputpath)){

        //Get a writable workbook
        WritableWorkbook writableworkbook = Workbook.createWorkbook(outputfile);            

        for(Path path: inputfiles)
        {
            Workbook sourceworkbook = Workbook.getWorkbook(path.toFile()); //Get the source workbook
            System.out.println(path.getFileName()); //Print workbook being processed
            Sheet readablesheet = sourceworkbook.getSheet(0); //Get the first sheet
            writableworkbook.importSheet(readablesheet.getName(), 0, readablesheet); //Import the sheet into the new workbook
            //Sheet names are imported if sheets are empty.  If sheets are populated I get a null pointer error.
        }

        writableworkbook.write();
        writableworkbook.close();
    }
    catch(NotDirectoryException e) {
        System.err.println(inputpath + " is not a directory." + e);
    }
    catch(IOException e) {
        System.err.println("I/O error." + e);
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-11 13:58:19

这是jxl-2.6.12.jar中的错误,请改用jxl-2.6.10.jar。

详细信息:

拼写错误'&&‘为'&’

第493行- WritableSheetCopier.java中的第504行

代码语言:javascript
复制
if (c != null)
          {
            toSheet.addCell(c);

            // Cell.setCellFeatures short circuits when the cell is copied,
            // so make sure the copy logic handles the validated cells        
            if (c.getCellFeatures() != null &
                c.getCellFeatures().hasDataValidation())
            {
              validatedCells.add(c);
            }
          }

第540行- WritableSheetCopier.java中的第551行

代码语言:javascript
复制
if (c != null)
          {
            toSheet.addCell(c);

            // Cell.setCellFeatures short circuits when the cell is copied,
            // so make sure the copy logic handles the validated cells        
            if (c.getCellFeatures() != null &
                c.getCellFeatures().hasDataValidation())
            {
              validatedCells.add(c);
            } 
          }

第990行- SheetCopier.java中的第1001行

代码语言:javascript
复制
if (c != null)
          {
            toSheet.addCell(c);

            // Cell.setCellFeatures short circuits when the cell is copied,
            // so make sure the copy logic handles the validated cells
            if (c.getCellFeatures() != null &
                c.getCellFeatures().hasDataValidation())
            {
              validatedCells.add(c);
            }
          }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9674388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档