如何将 excel 数据读入向量 table
How to read excel data into vector table
抱歉,我是 java 的新手。我已将方法从 JXL 更改为 Apache POI 以读取 excel 文件。现在我正在为 java 代码苦苦挣扎。我可以从我的 vector dataholder
中获取 return 输出,它逐行列出了 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 文件中的编码有误。
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;
}
}
当我尝试读取 excel 文件时,它显示如下错误消息:
你在cellStoreVector.addElement(cell);
这行得到exception
了吗?
您添加一个 XSSFCell
,我认为 element
必须是 String
而不是 Cell
。
这是工作示例:
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
我找到了一个简单的解决方案,将 cell
转换为字符串
cellStoreVector.addElement(cell+"");
抱歉,我是 java 的新手。我已将方法从 JXL 更改为 Apache POI 以读取 excel 文件。现在我正在为 java 代码苦苦挣扎。我可以从我的 vector dataholder
中获取 return 输出,它逐行列出了 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 文件中的编码有误。
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;
}
}
当我尝试读取 excel 文件时,它显示如下错误消息:
你在cellStoreVector.addElement(cell);
这行得到exception
了吗?
您添加一个 XSSFCell
,我认为 element
必须是 String
而不是 Cell
。
这是工作示例:
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
我找到了一个简单的解决方案,将 cell
转换为字符串
cellStoreVector.addElement(cell+"");