gpt4 book ai didi

java - 如何将excel数据读入 vector 表

转载 作者:行者123 更新时间:2023-12-02 22:55:48 25 4
gpt4 key购买 nike

抱歉,我是java新手。我已将读取 excel 文件的方法从 JXL 更改为 Apache POI。现在我正在努力处理java代码。我可以从我的 vector 数据持有者获取返回输出,它逐行列出了 Excel 文件中的所有详细信息。拜托,需要专家指导。任何帮助将不胜感激。

我的 Excel 文件: Banklist.xlsx

JSP 代码

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

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

读取XLSXFile.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;
}

}

当我尝试读取 Excel 文件时,它显示错误消息,如下所示: enter image description here

最佳答案

您在这行cellStoreVector.addElement(cell);中得到异常吗?您添加一个 XSSFCell,我认为 element 必须是 String 而不是 Cell

关于java - 如何将excel数据读入 vector 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com