溜溜问答 > 日常办公 > Excel > poi导入Excel
高傑西
高傑西
邀请你来回答
116人阅读 2022-04-22

poi导入Excel

求告知,为什么我进行导入Excel时,前台页面导入的文件为空呢?我打印了importfile,显示为空,为什么,求大牛告知!
我要回答
1个回答

方法/步骤

一,ExcelUtils工具类(也就是解析EXCEL文件,判断EXCEL的类型以及数据的类型)

importjava.io.IOException;

importjava.io.InputStream;

importjava.math.BigDecimal;

importjava.text.SimpleDateFormat;

importjava.util.ArrayList;

importjava.util.Date;

importjava.util.List;

importorg.apache.poi.hssf.usermodel.HSSFDateUtil;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

importorg.apache.poi.ss.usermodel.Cell;

importorg.apache.poi.ss.usermodel.Row;

importorg.apache.poi.ss.usermodel.Sheet;

importorg.apache.poi.ss.usermodel.Workbook;

importorg.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

privatefinalstaticString excel2003L =“.xls”;// 2003-版本的excel

privatefinalstaticString excel2007U =“.xlsx”;// 2007 +版本的excel

/ **

*描述:获取IO流中的数据,组装成List 对象

* @param in,fileName

* @返回

* @throws IOException

* /

publicList getBankListByExcel(InputStream in,String fileName)throwsException {

列表 list =null;

//创建的Excel工作薄

Workbook work =this.getWorkbook(in,fileName);

if(null== work){

抛出新的异常(“创建Excel工作薄为空!”);

}

Sheet sheet =null;//页数

行row =null;//行数

Cell cell =null;//列数

list =newArrayList >();

//遍历的Excel中所有的片

for(inti =0; i

sheet = work.getSheetAt(i);

if(sheet ==null){continue;}

//遍历当前片中的所有行

for(intj = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j ++){

row = sheet.getRow(j);

if(row ==null|| row.getFirstCellNum()== j){continue;}

//遍历所有的列

列表 li =newArrayList ();

for(inty = row.getFirstCellNum(); y

cell = row.getCell(y);

li.add(this.getValue(cell));

}

list.add(LI);

}

}

return 单

}

/ **

*描述:根据文件后缀,自适应上传文件的版本

* @param inStr,fileName

* @返回

* @throws异常

* /

publicWorkbook getWorkbook(InputStream inStr,String fileName)throwsException {

工作簿wb =null;

String fileType = fileName.substring(fileName.lastIndexOf(“。”));

if(excel2003L.equals(fileType)){

wb =newHSSFWorkbook(inStr);// 2003-

}elseif(excel2007U.equals(fileType)){

wb =newXSSFWorkbook(inStr);// 2007 +

}else{

抛出新的异常(“解析的文件格式有误!”);

}

返回wb;

}

/ **

*描述:对表格中数值进行格式化

* @param单元格

* @返回

* /

//解决擅长类型问题,获得数值

publicString getValue(Cell cell){

String value =“”;

if(null== cell){

返回值

}

switch(cell.getCellType()){

//数值型

案例Cell.CELL_TYPE_NUMERIC:

if(HSSFDateUtil.isCellDateFormatted(cell)){

//如果是date类型则,获取该单元格的日期值

Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());

SimpleDateFormat format =newSimpleDateFormat(“yyyy-MM-dd”);

value = format.format(date);;

}else{//纯数字

BigDecimal big =newBigDecimal(cell.getNumericCellValue());

value = big.toString();

//解决1234.0去掉后面的.0

if(null!= value &&!“”.equals(value.trim())){

String [] item = value.split(“[。]”);

if(1

value = item [0];

}

}

}

break;

//字符串类型

案例Cell.CELL_TYPE_STRING:

value = cell.getStringCellValue()。toString();

break;

//公式类型

案例Cell.CELL_TYPE_FORMULA:

//读公式计算值

value = String.valueOf(cell.getNumericCellValue());

if(value.equals(“NaN”)){//如果获取的数据值为非法值,则转换为获取字符串

value = cell.getStringCellValue()。toString();

}

break;

//布尔类型

案例Cell.CELL_TYPE_BOOLEAN:

value =“”+ cell.getBooleanCellValue();

break;

默认值:

value = cell.getStringCellValue()。toString();

}

if(“null”.endsWith(value.trim())){

value =“”;

}

返回值

}

}

二,定义两个实体类,一个是对于的Excel文件,解析它的数据(ExcelBean),另一个是导入数据库表的实体类(人)

ExcelBean.java

importorg.apache.poi.xssf.usermodel.XSSFCellStyle;

公共类ExcelBean实现java.io.Serializable {

privateString headTextName;//列头(标题)名

privateString propertyName;//对应字段名

私有整数列;//合并单元格数

私人XSSFCellStyle cellStyle;

publicExcelBean(){

}

publicExcelBean(String headTextName,String propertyName){

这个.headTextName = headTextName;

这个.propertyName = propertyName;

}

publicExcelBean(String headTextName,String propertyName,Integer cols){

super();

这个.headTextName = headTextName;

这个.propertyName = propertyName;

这个.cols = cols;

}

publicString getHeadTextName(){

returnheadTextName;

}

publicvoidsetHeadTextName(String headTextName){

这个.headTextName = headTextName;

}

publicString getPropertyName(){

returnpropertyName;

}

publicvoidsetPropertyName(String propertyName){

这个.propertyName = propertyName;

}

publicInteger getCols(){

返回列;

}

公共无效setCols(Integer cols){

这个.cols = cols;

}

上市XSSFCellStyle getCellStyle(){

返回cellStyle;

}

公共无效setCellStyle(XSSFCellStyle cellStyle){

这个.cellStyle = cellStyle;

}

}

people.java

importjava.util.Date;

公共课人

私有整数id

privateString userName;

私人字符串密码;

私人整数年龄;

私人日期;

publicInteger getId(){

返回id

}

publicvoidsetId(Integer id){

这个.id = id;

}

publicString getUserName(){

returnuserName;

}

publicvoidsetUserName(String userName){

这个.userName = userName ==null?null:userName.trim();

}

publicString getPassword(){

返回密码

}

publicvoidsetPassword(String password){

这个.password = password ==null?null:password.trim();

}

publicInteger getAge(){

回归年龄

}

publicvoidsetAge(Integer age){

这个.age = age

}

publicDate getDate(){

退货日期

}

publicvoidsetDate(Date date){

这个.date = date

}

}

查看全部
2022-04-22
回复 采纳

相关问题

JAVA poi怎么导入Excel数据?
共3条回答 >
薰衣草的婚礼: packagepoi;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Iterator;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell....
回复
poi组件操作导入Excel问题
共3条回答 >
狸猫: 这是我写的工具类里面的初始化函数,希望能够对你有所帮助:publicvoidinit(Stringfile_name){try{myxls=newFileInputStream(file_name);workbook=newHSSFWorkbook(myxls);sheetNum=workbook.getNumberOfSheets();}catch(Exceptione){thrownewRuntimeException("打开Excel文件["+file_name+"]出错,请检查Excel....
(3) 回复
如何实现poi快速导入Excel?
共1条回答 >
哈鑫: 参考:publicstaticvoidxlsDto2Excel(Listxls)throwsException{//获取总列数intCountColumnNum=xls.size();//创建Excel文档HSSFWorkbookhwb=newHSSFWorkbook();XlsDtoxlsDto=null;//sheet对应一个工作页HSSFSheetsheet=hwb.createSheet("pldrxkxxmb");HSSFRowfirstrow=sheet.createRow(0);/....
回复
JAVA Excel poi 怎么导入?
共1条回答 >
Cindy🎀: 1、下载poi相关jar,maven的集成如下:(把${poi.version}替换成你要的版本)org.apache.poipoi${poi.version}providedorg.apache.poipoi-ooxml${poi.version}providedorg.apache.poipoi-ooxml-schemas${poi.version}provided2、根据poi相关api读取sheet、row、cell,获得excel的数据:封装row的对象,即每一行数据为一个对象,每个c....
(1) 回复
ssm框架poi导入Excel
共1条回答 >
あ: 缺少方法,自己写一个吧,并不难
回复
发表成功!
感谢您的分享!
好的