如何将excel数据读入向量表

时间:2021-03-07 20:21:17

Sorry, im new to java. I have changed the method from JXL to Apache POI to read excel file. Now im struggling with the java code. i can get the return output from my vector dataholder it listed all the details from the excel file row by row. Please, need a guidance from expert. Any help would be appreciated.

对不起,我是java新手。我已将方法从JXL更改为Apache POI以读取excel文件。现在我正在努力与Java代码。我可以从我的矢量数据持有者那里获得返回输出,它列出了excel文件中的所有细节。请,需要专家的指导。任何帮助,将不胜感激。

My excel file : Banklist.xlsx

我的excel文件:Banklist.xlsx

JSP CODE

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="ReadXLSXFile" scope="page" class="com.rexit.easc.ReadXLSXFile" />

if(sType.equalsIgnoreCase("READ_EXCEL"))
{
    SimpleDateFormat timestampFormat    = new SimpleDateFormat("yyyyMMdd");
    String currDATE                     = timestampFormat.format(new Date());

    mySmartUpload.initialize(pageContext);  
    mySmartUpload.setTotalMaxFileSize(1000000);
    mySmartUpload.upload();

    String upload_path = "";
    FileInputStream is = new FileInputStream("/home/config.prop");
    Properties prop = new Properties();
    prop.load(is);  
    upload_path = prop.getProperty("upload_path");
    String FILE_NAME = "";
    try 
    {
        for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
        {
            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);

            if (!myFile.isMissing()) 
            {
                FILE_NAME = myFile.getFileName();
                FILE_NAME = SESUSERID+"_"+currDATE+"_"+FILE_NAME;
                myFile.saveAs(upload_path +"//"+FILE_NAME,mySmartUpload.SAVE_PHYSICAL);
            }
        }
     } 
     catch (Exception e)
     {
         e.printStackTrace();
     }              
    String ATTACH_FILE = FILE_NAME;

        vTable = new Vector();
        Vector dataHolder=ReadXLSXFile.importExcelSheet(upload_path+"//"+ATTACH_FILE);

        for (int i=0;i<dataHolder.size();i++)
       {
        Vector vTableRow = (Vector)dataHolder.elementAt(i);
        if(vTableRow.size()>=4)
        {

            String sSEQ_NO          = (String) vTableRow.elementAt(0);
            String sUNIT_NO         = (String) vTableRow.elementAt(1);
            String sOWNER_NAME      = (String) vTableRow.elementAt(2);
            String sOWNER_NAME2     = (String) vTableRow.elementAt(3);
            String sMORTGAGEE_NAME  = (String) vTableRow.elementAt(4);
            String sBANK_REF        = (String) vTableRow.elementAt(5);
            intSeqNo                = vTable.size();

            if(!sOWNER_NAME.equals(""))
            {
                intSeqNo   += 1;
                SERIAL      = df.format(intSeqNo);

                Vector vRecord  = new Vector();
                vRecord.addElement(Integer.toString(i+1));
                vRecord.addElement(Integer.toString(i+1));
                vRecord.addElement(sUNIT_NO);
                vRecord.addElement(sOWNER_NAME.toUpperCase());
                vRecord.addElement(sOWNER_NAME2.toUpperCase());
                vRecord.addElement(sMORTGAGEE_NAME.toUpperCase());
                vRecord.addElement(sBANK_REF);
                vRecord.addElement(SERIAL);
                vTable.addElement(vRecord);
            }               
            else
            {
                break;
            }
        }
     }
   %>

I suspect my coding in java file is wrong.

我怀疑我在java文件中的编码是错误的。

ReadXLSXFile.java

package com.rexit.easc;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadXLSXFile {

    public Vector importExcelSheet(String fileName)
    {
        Vector cellVectorHolder = new Vector();
        try
        {
            Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
            Sheet sheet = workBook.getSheetAt(0);
            Iterator rowIter = sheet.rowIterator();

            while(rowIter.hasNext())
            {
                XSSFRow row = (XSSFRow) rowIter.next();
                Iterator cellIter = row.cellIterator();
                Vector cellStoreVector=new Vector();

                while(cellIter.hasNext())
                {
                    XSSFCell cell = (XSSFCell) cellIter.next();
                    cellStoreVector.addElement(cell);
                }
                cellVectorHolder.addElement(cellStoreVector);
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        return cellVectorHolder;
    }

}

When i try to read the excel file, it show error message as below : 如何将excel数据读入向量表

当我尝试读取excel文件时,它显示如下错误消息:

3 个解决方案

#1


0  

Do you get the exception in this line cellStoreVector.addElement(cell);? You add a XSSFCell and I think the element must be a String not a Cell.

你在这行中得到了异常cellStoreVector.addElement(cell);?你添加一个XSSFCell,我认为该元素必须是一个字符串而不是一个单元格。

#2


0  

Here is the working example:

这是工作示例:

import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

public class ReadXLSXFile {

    public Vector<Vector<String>> importExcelSheet(String fileName) {
        Vector<Vector<String>> cellVectorHolder = new Vector<>();
        try {
            Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
            Sheet sheet = workBook.getSheetAt(0);
            Iterator rowIter = sheet.rowIterator();

            while (rowIter.hasNext()) {
                XSSFRow row = (XSSFRow) rowIter.next();
                Iterator cellIter = row.cellIterator();
                Vector<String> cellStoreVector = new Vector<>();

                while (cellIter.hasNext()) {
                    XSSFCell cell = (XSSFCell) cellIter.next();
                    String cellTypeDesc = "";
                    String cellValue = "";
                    Integer cellType = cell.getCellType();

                    switch (cellType) {
                        case 0:
                            cellTypeDesc = "NUMERIC";
                            Double doubleValue = cell.getNumericCellValue();

                            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                if (HSSFDateUtil.isValidExcelDate(doubleValue)) {
                                    Date date = HSSFDateUtil.getJavaDate(doubleValue);

                                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                                    cellValue = df.format(date);
                                    System.err.println(cellValue);
                                }
                            } else {
                                Integer intValue = doubleValue.intValue();
                                cellValue = String.valueOf(intValue);
                            }

                            break;
                        case 1:
                            cellTypeDesc = "STRING";
                            cellValue = cell.getStringCellValue();
                            break;
                        case 2:
                            cellTypeDesc = "FORMULA";
                            cellValue = cell.getCellFormula();
                            break;
                        case 3:
                            cellTypeDesc = "BLANK";
                            cellValue = "BLANK";
                            break;
                        case 4:
                            cellTypeDesc = "BOOLEAN";
                            boolean booleanValue = cell.getBooleanCellValue();
                            cellValue = "" + booleanValue;
                            break;
                        case 5:
                            cellTypeDesc = "ERROR";
                            byte byteValue = cell.getErrorCellValue();
                            cellValue = "" + byteValue;
                            break;
                    }

                    cellStoreVector.addElement(cellValue);
                }
                cellVectorHolder.addElement(cellStoreVector);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cellVectorHolder;
    }

}

And the test:

而且测试:

  public static void main(String[] args) {

        ReadXLSXFile xls = new ReadXLSXFile();
        Vector<Vector<String>> vect = xls.importExcelSheet("c:/test_doc.xlsx");

        for (Vector<String> v : vect) {
            for (String s : v) {
                System.out.println(s);
            }
        }
    }

I allso needed to add: xmlbeans-5.1.3.jar dom4j.jar

我还需要添加:xmlbeans-5.1.3.jar dom4j.jar

#3


0  

i found a simple solution, to convert cell to string

我找到了一个简单的解决方案,将单元转换为字符串

cellStoreVector.addElement(cell+"");

#1


0  

Do you get the exception in this line cellStoreVector.addElement(cell);? You add a XSSFCell and I think the element must be a String not a Cell.

你在这行中得到了异常cellStoreVector.addElement(cell);?你添加一个XSSFCell,我认为该元素必须是一个字符串而不是一个单元格。

#2


0  

Here is the working example:

这是工作示例:

import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

public class ReadXLSXFile {

    public Vector<Vector<String>> importExcelSheet(String fileName) {
        Vector<Vector<String>> cellVectorHolder = new Vector<>();
        try {
            Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
            Sheet sheet = workBook.getSheetAt(0);
            Iterator rowIter = sheet.rowIterator();

            while (rowIter.hasNext()) {
                XSSFRow row = (XSSFRow) rowIter.next();
                Iterator cellIter = row.cellIterator();
                Vector<String> cellStoreVector = new Vector<>();

                while (cellIter.hasNext()) {
                    XSSFCell cell = (XSSFCell) cellIter.next();
                    String cellTypeDesc = "";
                    String cellValue = "";
                    Integer cellType = cell.getCellType();

                    switch (cellType) {
                        case 0:
                            cellTypeDesc = "NUMERIC";
                            Double doubleValue = cell.getNumericCellValue();

                            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                if (HSSFDateUtil.isValidExcelDate(doubleValue)) {
                                    Date date = HSSFDateUtil.getJavaDate(doubleValue);

                                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                                    cellValue = df.format(date);
                                    System.err.println(cellValue);
                                }
                            } else {
                                Integer intValue = doubleValue.intValue();
                                cellValue = String.valueOf(intValue);
                            }

                            break;
                        case 1:
                            cellTypeDesc = "STRING";
                            cellValue = cell.getStringCellValue();
                            break;
                        case 2:
                            cellTypeDesc = "FORMULA";
                            cellValue = cell.getCellFormula();
                            break;
                        case 3:
                            cellTypeDesc = "BLANK";
                            cellValue = "BLANK";
                            break;
                        case 4:
                            cellTypeDesc = "BOOLEAN";
                            boolean booleanValue = cell.getBooleanCellValue();
                            cellValue = "" + booleanValue;
                            break;
                        case 5:
                            cellTypeDesc = "ERROR";
                            byte byteValue = cell.getErrorCellValue();
                            cellValue = "" + byteValue;
                            break;
                    }

                    cellStoreVector.addElement(cellValue);
                }
                cellVectorHolder.addElement(cellStoreVector);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cellVectorHolder;
    }

}

And the test:

而且测试:

  public static void main(String[] args) {

        ReadXLSXFile xls = new ReadXLSXFile();
        Vector<Vector<String>> vect = xls.importExcelSheet("c:/test_doc.xlsx");

        for (Vector<String> v : vect) {
            for (String s : v) {
                System.out.println(s);
            }
        }
    }

I allso needed to add: xmlbeans-5.1.3.jar dom4j.jar

我还需要添加:xmlbeans-5.1.3.jar dom4j.jar

#3


0  

i found a simple solution, to convert cell to string

我找到了一个简单的解决方案,将单元转换为字符串

cellStoreVector.addElement(cell+"");