前提
Javaでエクセル操作がしたいと思い、調べると「Apache POI」というライブラリ
出てきたのでダウンロードしたのですが、コンパイルは通るがJavaコマンドで実行すると
エラーメッセージが出てしまう。
ダウンロードしたのバージョンは poi-bin-5.0.0-20210120 です(最新でした)
実現したいこと
◆下記のエラーを解決
◆一般的な環境設定が知りたい
発生している問題・エラーメッセージ
エラー: メイン・クラスTestを初期化できません 原因: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
該当のソースコード
Test.java
1import java.io.IOException; 2import java.io.InputStream; 3import java.io.OutputStream; 4import java.io.File; 5import java.nio.file.Files; 6import java.nio.file.Path; 7import java.nio.file.Paths; 8 9import org.apache.poi.ss.usermodel.Cell; 10import org.apache.poi.ss.usermodel.CellStyle; 11import org.apache.poi.ss.usermodel.FillPatternType; 12import org.apache.poi.ss.usermodel.CellValue; 13import org.apache.poi.ss.usermodel.Workbook; 14import org.apache.poi.ss.usermodel.Sheet; 15import org.apache.poi.ss.usermodel.Row; 16import org.apache.poi.xssf.usermodel.XSSFWorkbook; 17import org.apache.poi.ss.usermodel.IndexedColors; 18 19public class Test { 20 21 public static void main(String[] args){ 22 System.out.print("start"); 23 24 Workbook tempbook = null; 25 try { 26 Path tempPath = Paths.get("C:\\Java\\TestFile\\test.xlsx"); 27 InputStream inSt = Files.newInputStream(tempPath); 28 tempbook = new XSSFWorkbook(inSt); 29 Sheet sheet = tempbook.getSheet("Sheet1"); 30 Row row = sheet.createRow(0); 31 Cell cell = row.createCell(0); 32 cell.setCellValue("AAA"); 33 CellStyle styleA1 = tempbook.createCellStyle(); 34 styleA1.setFillPattern( 35 FillPatternType.SOLID_FOREGROUND); 36 styleA1.setFillForegroundColor( 37 IndexedColors.MAROON.getIndex()); 38 cell.setCellStyle(styleA1); 39 40 row = sheet.createRow(1); 41 cell = row.createCell(0); 42 cell.setCellValue("BBB"); 43 CellStyle styleA2 = tempbook.createCellStyle(); 44 styleA2.setFillPattern( 45 FillPatternType.SOLID_FOREGROUND); 46 styleA2.setFillForegroundColor( 47 IndexedColors.BLUE.getIndex()); 48 cell.setCellStyle(styleA2); 49 50 row = sheet.createRow(2); 51 cell = row.createCell(0); 52 cell.setCellValue("CCC"); 53 CellStyle styleA3 = tempbook.createCellStyle(); 54 styleA3.setFillPattern( 55 FillPatternType.SOLID_FOREGROUND); 56 styleA3.setFillForegroundColor( 57 IndexedColors.GREEN.getIndex()); 58 cell.setCellStyle(styleA3); 59 60 Path outPath = Paths.get("C:\\Java\\TestFile\\out.xlsx"); 61 OutputStream outSt = Files.newOutputStream(outPath); 62 tempbook.write(outSt); 63 } catch (IOException e) { 64 System.out.print("error"); 65 } finally { 66 try { 67 if (tempbook != null) { 68 tempbook.close(); 69 } 70 } catch (IOException e) { 71 System.out.print("error"); 72 } 73 } 74 System.out.print("done"); 75 } 76
試したこと
エラーメッセージを調べると
org.apache.poi.xssf.usermodel.Workbookクラスがみつかっていない?感じだった。
poi-ooxml-schemasーバージョンナンバー.jar
といファイルが必要という記事を見つけたので
(ダウンロードしたフォルダにはありませんでした)
poi-ooxml-schemas-3.8-20120326.jarをダウンロード
クラスパスを通してリトライしましたが全く同じエラーがでます。
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー