对不起,我是java的新手。我已经将方法从JXL更改为Apache POI以读取excel文件。现在我正在努力与Java代码。我可以从我的vector dataholder
获得返回输出,它会逐行列出excel文件中的所有详细信息。请,需要专家的指导。任何帮助将不胜感激。
我的Excel文件: Banklist.xlsx
<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文件中的编码是错误的。
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文件时,它会显示如下错误消息:
答案 0 :(得分:0)
您是否获得此行exception
中的cellStoreVector.addElement(cell);
?
您添加了XSSFCell
,我认为element
必须是String
而不是Cell
。
答案 1 :(得分:0)
以下是工作示例:
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;
}
}
测试:
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);
}
}
}
我还需要添加: XMLBeans的-5.1.3.jar dom4j.jar
答案 2 :(得分:0)
我找到了一个简单的解决方案,将cell
转换为字符串
cellStoreVector.addElement(cell+"");