1

まず、私はJavaとこのサイトに非常に慣れていないということから始めましょう。私は今1、2冊の本を読んでいて、それ以来、自分を楽しませ続けるための小さなプロジェクトを探しています。これを調べてみましたが、必要な情報が見つかりませんでした。そうは言っても、これが私の最初の質問なので、これが非常に初期の初心者のものであり、明らかな何かが欠けている場合は、お詫び申し上げます。

プロジェクトに関して、私の義理の兄弟は、フォルダーに90ほどのExcelワークブックがあり、各レポートの最初のワークシートを1つのマスターワークブックにマージする必要があるという問題を抱えています。彼は手動でそれを行うことができますが、Javaでそれを行う方法を試してみることは興味深いと思いました。

調査を行い、JExcelAPIをダウンロードして、クラスパスに.jarを追加しました。コンピューターに2つのディレクトリを作成しました。

C:\ Excel \

C:\ Excel \ Finished \

C:\Excel\の内部に2枚のダミーのExcelシートを作成しました。テストのために、それぞれの最初のシートの名前を変更しました。完成したフォルダに、これらのシートをマージする予定のマスタードキュメントを作成しました。シートが空でこれを実行すると、シートがコピーされたように見えます。マスターファイルには2枚のシートがあり、それらの名前はそれぞれのワークブックで付けた名前に対応しているので、これは機能していると思います。ただし、これらのシートの1つに情報を追加してこれを実行しようとすると、nullポインター例外が発生します。私はこれに何時間も取り組んできたので、たぶん休憩が必要なのかもしれませんが、何が悪いのか理解できません。私はJExcelAPIのWebサイトにアクセスし、これを行うための古いメソッドのように見えるものを試しました(importSheet()が存在する前)。それはしませんでした

時間に余裕があり、JExcelAPIに精通している場合は、何が問題になっているのか教えていただけますか?とても感謝しております。エラーとコードを以下に投稿しました。

- エラー -

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)

- コード -

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);
    }
}
}
4

1 に答える 1

5

これはjxl-2.6.12.jarのバグです。代わりに、jxl-2.6.10.jarを使用してください。

詳細:

typo'&&' into'&'

493行目-WritableSheetCopier.javaの504行目

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);
            }
          }

WritableSheetCopier.javaの540行目-551行目

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行目

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);
            }
          }
于 2012-04-11T05:58:19.230 に答える