这样接收,当然不行了,,,,,,使用smartupload等接收上传,或Struts接收上传的文件
文件上传,要浏览器端编码的 <form action="UploadServlet" method="post" enctype="multipart/form-data">
package comsidioautil;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoipoifsfilesystemPOIFSFileSystem;
/
@author (版权归原作者)
用于读取excel
/
public class ExcelReader {
private HSSFWorkbook wb = null;// book [includes sheet]
private HSSFSheet sheet = null;
private HSSFRow row = null;
private int sheetNum = 0; // 第sheetnum个工作表
private int rowNum = 0;
private FileInputStream fis = null;
private File file = null;
public ExcelReader() {
}
public ExcelReader(File file) {
thisfile = file;
}
public void setRowNum(int rowNum) {
thisrowNum = rowNum;
}
public void setSheetNum(int sheetNum) {
thissheetNum = sheetNum;
}
public void setFile(File file) {
thisfile = file;
}
/
读取excel文件获得HSSFWorkbook对象
/
public void open() throws IOException {
fis = new FileInputStream(file);
wb = new HSSFWorkbook(new POIFSFileSystem(fis));
fisclose();
}
/
返回sheet表数目
@return int
/
public int getSheetCount() {
int sheetCount = -1;
sheetCount = wbgetNumberOfSheets();
return sheetCount;
}
/
sheetNum下的记录行数
@return int
/
public int getRowCount() {
if (wb == null)
Systemoutprintln("=============>WorkBook为空");
HSSFSheet sheet = wbgetSheetAt(thissheetNum);
int rowCount = -1;
rowCount = sheetgetLastRowNum();
return rowCount;
}
/
读取指定sheetNum的rowCount
@param sheetNum
@return int
/
public int getRowCount(int sheetNum) {
HSSFSheet sheet = wbgetSheetAt(sheetNum);
int rowCount = -1;
rowCount = sheetgetLastRowNum();
return rowCount;
}
/
得到指定行的内容
@param lineNum
@return String[]
/
public String[] readExcelLine(int lineNum) {
return readExcelLine(thissheetNum, lineNum);
}
/
指定工作表和行数的内容
@param sheetNum
@param lineNum
@return String[]
/
public String[] readExcelLine(int sheetNum, int lineNum) {
if (sheetNum < 0 || lineNum < 0)
return null;
String[] strExcelLine = null;
try {
sheet = wbgetSheetAt(sheetNum);
row = sheetgetRow(lineNum);
int cellCount = rowgetLastCellNum();
strExcelLine = new String[cellCount + 1];
for (int i = 0; i <= cellCount; i++) {
strExcelLine[i] = readStringExcelCell(lineNum, i);
}
} catch (Exception e) {
eprintStackTrace();
}
return strExcelLine;
}
/
读取指定列的内容
@param cellNum
@return String
/
public String readStringExcelCell(int cellNum) {
return readStringExcelCell(thisrowNum, cellNum);
}
/
指定行和列编号的内容
@param rowNum
@param cellNum
@return String
/
public String readStringExcelCell(int rowNum, int cellNum) {
return readStringExcelCell(thissheetNum, rowNum, cellNum);
}
/
指定工作表、行、列下的内容
@param sheetNum
@param rowNum
@param cellNum
@return String
/
public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
if (sheetNum < 0 || rowNum < 0)
return "";
String strExcelCell = "";
try {
sheet = wbgetSheetAt(sheetNum);
row = sheetgetRow(rowNum);
if (rowgetCell((short) cellNum) != null) { // add this condition
// judge
switch (rowgetCell((short) cellNum)getCellType()) {
case HSSFCellCELL_TYPE_FORMULA:
strExcelCell = "FORMULA ";
break;
case HSSFCellCELL_TYPE_NUMERIC: {
strExcelCell = StringvalueOf(rowgetCell((short) cellNum)
getNumericCellValue());
}
break;
case HSSFCellCELL_TYPE_STRING:
strExcelCell = rowgetCell((short) cellNum)
getStringCellValue();
break;
case HSSFCellCELL_TYPE_BLANK:
strExcelCell = "";
break;
default:
strExcelCell = "";
break;
}
}
} catch (Exception e) {
eprintStackTrace();
}
return strExcelCell;
}
//主函数用于测试
public static void main(String args[]) {
File file = new File("C:\\importpersonxls");
ExcelReader readExcel = new ExcelReader(file);
try {
readExcelopen();
} catch (IOException e) {
eprintStackTrace();
}
readExcelsetSheetNum(0); // 设置读取索引为0的工作表
// 总行数
int count = readExcelgetRowCount();
for (int i = 0; i <= count; i++) {
String[] rows = readExcelreadExcelLine(i);
for (int j = 0; j < rowslength; j++) {
Systemoutprint(rows[j] + " ");
}
Systemoutprint("\n");
}
}
}
注:这是我在网上找的,这几天也在整这个问题,经测试这段代码完全没问题,版权归原作者所有。怎么样,给分吧,哪里不行,可以问我 呵呵
需要对Excel中的数据进行读取 *** 作。一、在开始进行Java读写Excel前,需要先下一个jxl的jar包,这个jar包中提供了相关读写Excel的方法,将jxljar放到classpath下或者在工程的buildpath中添加jxljar后,便可以开始Java读写Excel了。二、Java读取Excel数据,首先,创建一个xls文件(如:jxltestxls),然后在文件中添加一些数据,Excel文件创建完成后,便可以开始写代码读取了。三、进行一个小小的扩展,读取一个目录下的所有Excel文件,读取的每个Excel文件的数据写入到不同的txt中。四、生成EXCEL需要手动写查询语句把ORACLE数据库中的数据查询出来,再通过 *** 作写到EXCEL文件里面。通过EXCEL把数据读取到ORACLE,同样需要去读取EXCEL工作薄里面的内容,再通过INSERT语句去插入数据库 *** 作。
以上就是关于JAVA Excel文件上传,上传后的Excel文件打开时报有不可读取的内容,怎么回事全部的内容,包括:JAVA Excel文件上传,上传后的Excel文件打开时报有不可读取的内容,怎么回事、一道java读取excel文件的问题、浅谈JAVA读写Excel的几种途径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)