-2

私のコードでは、apcheライブラリの欠落エラーに直面しています。

以下にコード全体を貼り付けます。plsは解決策を見つけるのに役立ちます...

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.*;



// Apache POI - HSSF imports
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;




public class ExcelParser {

    HSSFSheet m_sheet;
    int m_iNbRows;
    int m_iCurrentRow = 0;
    private static final String JAVA_TOSTRING =
 "EEE MMM dd HH:mm:ss zzz yyyy";

    public ExcelParser(HSSFSheet sheet)
    {
        m_sheet = sheet;
        m_iNbRows = sheet.getPhysicalNumberOfRows();
    }


    /* Returns the contents of an Excel row in the 
form of a String array.
     * @see com.ibm.ccd.common.parsing.Parser#splitLine()
     */
    public String[] splitLine() throws Exception {
        if (m_iCurrentRow == m_iNbRows)
            return null;

        HSSFRow row = m_sheet.getRow(m_iCurrentRow);
        if(row == null)
        {
            return null;
        }
        else
        {
            int cellIndex = 0; 
            int noOfCells = row.getPhysicalNumberOfCells();
            String[] values = new String[noOfCells];        
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            if (firstCellNum >=0 && lastCellNum >=0) 
            {
                for(short iCurrent = firstCellNum; iCurrent <lastCellNum; iCurrent++) 
            {
                    HSSFCell cell = (HSSFCell)row.getCell(iCurrent);
                    if(cell == null)
                    {
                        values[iCurrent] = "";
                        cellIndex++;                
                        continue;
                    }
                    else
                    {
                        switch(cell.getCellType())
                        {                           
                        case HSSFCell.CELL_TYPE_NUMERIC:
                        double value = cell.getNumericCellValue();
                        if(HSSFDateUtil.isCellDateFormatted(cell)) 

                        {
                            if(HSSFDateUtil.isValidExcelDate(value))
                            {
                                Date date = HSSFDateUtil.getJavaDate(value);
                                SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);  
                                values[iCurrent] = dateFormat.format(date);                             
                            }
                            else
                            {
                                throw new Exception("Invalid Date value found at row number " +
                                        row.getRowNum()+" and column number "+cell.getCellNum());   
                            }
                        }
                        else
                        {
                            values[iCurrent] = value + "";
                        }
                        break;

                        case HSSFCell.CELL_TYPE_STRING:
                            values[iCurrent] = cell.getStringCellValue();
                        break;

                        case HSSFCell.CELL_TYPE_BLANK:
                            values[iCurrent] = null;    
                        break;

                        default:
                            values[iCurrent] = null;    
                        }           
                    }                           
                }        
            }
            m_iCurrentRow++;
            return values;              
        }

    }

   public static void main(String args[])
   {
       HSSFWorkbook workBook = null; 
       File file  = new File("/home/sprasad/austin_api/Book1.xls");
       InputStream excelDocumentStream = null;
       try 
       {
           excelDocumentStream = new FileInputStream(file);
           POIFSFileSystem fsPOI = new POIFSFileSystem(new BufferedInputStream(excelDocumentStream));
           workBook = new HSSFWorkbook(fsPOI);         
           ExcelParser parser = new ExcelParser(workBook.getSheetAt(0));
           String [] res;
            while ((res = parser.splitLine()) != null)
            {
                            for (int i = 0; i < res.length; i++)
                {
                    System.out.println("Token Found [" + res[i] + "]");
                }
            }
            excelDocumentStream.close();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


   }

上記の例では、このコードを使用しているときにエラーが発生します。

同じためのライブラリまたはチュートリアルを私に提案してください..事前に感謝します...

4

2 に答える 2

2

Apache POIをダウンロードし、クラスパスで膨らませて、機能しているかどうかを確認するだけです。

Eclipseを使用している場合は、プロジェクト>プロパティ>Javaビルドパス>ライブラリを右クリックし、外部JARの追加をクリックします。

次のJARを追加する必要があります。

  • poi-3.8-20120326.jar
  • poi-examples-3.8-20120326.jar
  • poi-excelant-3.8-20120326.jar
  • poi-ooxml-3.8-20120326.jar
  • poi-ooxml-schemas-3.8-20120326.jar
  • poi-scratchpad-3.8-20120326.jar

この後、プロジェクトクラスを使用できるようになります。

于 2012-06-11T17:16:53.830 に答える
1

エラーが発生した場合は、使用しているサードパーティのライブラリをJavaクラスパスimportに含めるのを忘れている可能性があります。Apacheライブラリ用のJARファイルがたくさんあると仮定すると、私が提供したWikipediaのリンクに記載されているように、それらをアプリケーションのクラスパスに含める必要があります。

または、現在のライブラリセットに存在しない古いクラスまたは無効なクラスをインポートしようとしている可能性があります。

エラーメッセージを投稿し、使用しているライブラリファイルとアプリケーションの起動方法について言及しない限り、より具体的なヘルプを提供することはできません。

于 2012-06-11T17:12:05.403 に答える