猜你想搜
最近搜索
热门搜索
新人福利
下载币300赠币券 (充值可用)
渲染币300赠币券 (充值可用)
永久收益
在线云存储
版权登记
我要上传
上传活动
VIP模型免费下
VIP素材免费下
模型下载折扣
云渲染低至7折
专区课程免费看
每月会员礼包
方法/步骤
一,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去掉后面的.0if(null!= value &&!“”.equals(value.trim())){String [] item = value.split(“[。]”);if(1value = 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.javaimportjava.util.Date;公共课人私有整数idprivateString 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条回答 > あ: 缺少方法,自己写一个吧,并不难 kiss mayue邀请你来回答 赞 回复 素材推荐 3D模型 SU模型 材质 贴图 CAD 灵感图 效果图 现代台灯 北欧烛台 现代盆栽 北欧卫生间 简欧鸟瞰图 现代鸟瞰图 其它绒布34 其它绒布36 其它混凝土1 其它花岗岩 霞红荔枝面花岗岩 其它流水纹大理石 其它木纹 刺猬紫檀花梨木 办公桌图块 钢琴图块 厨房水槽 现代室内设计中的绿色沙发椅 现代浴室室内设计3d渲染 现代简约风格庭院设计 海滨别墅--海风阵阵,潮涨潮落 沙漠中的飞机场 Studioninedots--The Rebel,用绿植妆点建筑 ? 这可能对您有用 更多解决方案 热门回答 热门文章 草图大师怎么批量加树? 67人阅读 SU草图大师怎样调整材质? 3人阅读 SketchUp怎样绘制凸窗? 14人阅读 怎么把3dsmax模型导入Unity? 2人阅读 3dmax拖模型进去卡怎么办? 55人阅读 问答标签 poi的excel导入 poi导入Excel poi导入Excel数据 poi Excel Excel poi poi生成Excel poi解析Excel poi下载Excel poi Excel 导出 poi读写Excel poi的Excel导出 Excel导出 poi 发表成功! 感谢您的分享! 好的
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();
//公式类型
案例Cell.CELL_TYPE_FORMULA:
//读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if(value.equals(“NaN”)){//如果获取的数据值为非法值,则转换为获取字符串
//布尔类型
案例Cell.CELL_TYPE_BOOLEAN:
value =“”+ cell.getBooleanCellValue();
默认值:
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();
这个.cols = cols;
publicString getHeadTextName(){
returnheadTextName;
publicvoidsetHeadTextName(String headTextName){
publicString getPropertyName(){
returnpropertyName;
publicvoidsetPropertyName(String propertyName){
publicInteger getCols(){
返回列;
公共无效setCols(Integer cols){
上市XSSFCellStyle getCellStyle(){
返回cellStyle;
公共无效setCellStyle(XSSFCellStyle cellStyle){
这个.cellStyle = cellStyle;
people.java
公共课人
私有整数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