JFile的导入xlsx与xls
首先需要有JAVA的一些jar包
下载地址:http://download.csdn.net/detail/qq_35980546/9892511
你要先配置好路由,还有能拿到绝对路径才行
下面直接给源码:
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
public class ExcelController extends Controller {
private static final String EXTENSION_XLS = "xls";
private static final String EXTENSION_XLSX = "xlsx";
public void Excel() throws FileNotFoundException, FileFormatException{//Excel数据导入
//String Excelion = PathKit.getWebRootPath() + "//upload//"+this.getPara("Excel");//这是找到项目中的路径
String Excelion=this.getPara("Excel"); //找到的Excel的绝对路径
// 检查
preReadCheck(Excelion);
// 获取workbook对象
Workbook workbook = null;
try {
workbook = getWorkbook(Excelion);
Integer ing=0;//长度
Integer lengthing=0;//表头标记
int lastColumnIndex = 0;//列数
Map<String, String> map=new HashMap<String, String>();
Map<String,String> maptable=new HashMap<String, String>();
// 读文件 一个sheet一个sheet地读取
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workbook.getSheetAt(numSheet);
if (sheet == null) {
break;
}
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
Row firstRow = sheet.getRow(firstRowIndex);
for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++) {
Cell cell = firstRow.getCell(i);
String cellValue = getCellValue(cell, true);//表头数据
System.out.println("表头数据:"+cellValue);
}
//System.out.println("");
// 读取数据行
for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
Row currentRow = sheet.getRow(rowIndex);// 当前行
int firstColumnIndex = currentRow.getFirstCellNum(); // 首列
lastColumnIndex = currentRow.getLastCellNum();// 最后一列
for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {
Cell currentCell = currentRow.getCell(columnIndex);// 当前单元格
String currentCellValue = getCellValue(currentCell, true);// 当前单元格的值
//System.out.print(currentCellValue + "\t");
System.out.println("表头数据:"+currentCellValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException {
// 常规检查
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("传入的文件不存在:" + filePath);
}
if (!(filePath.endsWith(EXTENSION_XLS) || filePath.endsWith(EXTENSION_XLSX))) {
throw new FileFormatException("传入的文件不是excel");
}
}
private static String getCellValue(Cell cell, boolean treatAsStr) {
if (cell == null) {
return "";
}
if (treatAsStr) {
// 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”
// 加上下面这句,临时把它当做文本来读取
cell.setCellType(Cell.CELL_TYPE_STRING);
}
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return String.valueOf(cell.getNumericCellValue());
} else {
return String.valueOf(cell.getStringCellValue());
}
}
private static Workbook getWorkbook(String filePath){
Workbook workbook = null;
InputStream is;
try {
is = new FileInputStream(filePath);
if (filePath.endsWith(EXTENSION_XLS)) { //判断Excel为2003还是2007版本
workbook = new HSSFWorkbook(is);
} else if (filePath.endsWith(EXTENSION_XLSX)) {
workbook = new XSSFWorkbook(is);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return workbook;
}
}
这是上传Excel的两个版本,前提是你能拿到绝对的路径。
这里我可以给你们拿到到绝对路径的一种方法,就是把文件上传到Tomcat服务器中,然后就读取Tomcat中的文件路径,网上有一大把上传的资料。
如果找不到上传到Tomcat服务器中的代码可以加我QQ:1262333765,非诚勿扰啊!
JFile的导入xlsx与xls的更多相关文章
- 关于Navicat Premium导入xlsx的问题
这段时间由于工作需要,频繁通过Oracle导入/导出大量数据,测试了很多软件,都不理想.PL/SQL Developer导入.导出都卡死:Oracle SQL Developer也是导入.导出都卡的半 ...
- java 关于xlsx(xls) 和 csv 文件的数据解析
1.适用于xlsx 和 xls <!--xlsx和xls文件pom依赖--> <dependency> <groupId>org.apache.poi</g ...
- Java 导出Excel xlsx、xls, CSV文件
通用导出功能: 1.支持Excel xlsx.xls 2.支持CSV文件导出 3.数据库查询分页导出.内存导出 4.支持大批量数据导出 使用步骤如下 导入jar <dependency> ...
- 把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中
把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中步骤如下: 把需要合并的文件放到同一个文件夹 在该文件夹中新建一个excel文件 打开新建的excel问价,把鼠标放到sh ...
- 判断上传的Excel为.xlsx还是.xls
这个问题其实蛮简单的,具体操作如下 判断获取到上传的文件id后下载文件,然后拿到文件名称,截取文件名称后缀,判断是.xlsx还是.xls进行不同的操作即可
- navicat for oracle 导入xlsx文件提示无法打开xlsx文件
navicat for oracle 导入xlsx文件提示:无法打开xlsx文件 导入环境: navicat for oracle wps状态的xlsx文件 处理: 将wps状态的xlsx文件,打开方 ...
- 在线预览(pptx、ppt、pps、docx、doc、xlsx、xls)
http://view.officeapps.live.com/op/view.aspx?src=<文档位置> 示例文档https://www.dujin.org/file/ppt/duj ...
- C#导入Excel、Excel导入、导入.xls 、导入.xlsx、Excel2003版本、Excel2007版本
C#导入Excel: 1.选择Excel 03版文件 2.选择需要读取数据的Excel工作表 3.选择工作表中需要读取的列 源码地址在图片下面,不要点击图片,点击下载地址跳转下载.
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...
随机推荐
- 前端小课堂 js:函数的创建方式及区别
js 函数的创建大体有这几种方式: -1-函数表达式(函数字面量): 说白了就是把一个函数赋值给了一个变量. var fun1 = function(index){ alert(index); } f ...
- JVM、GC与HashMap
阿里巴巴突然来了个面试邀请电话,问了些java底层的东西,不知所措,所以专门花了些时间做了下学习,顺便记录下,好记性不如烂笔头. 一.对JAVA的垃圾回收机制(GC)的理解 不同于C/C++需要手工释 ...
- How to use data analysis for machine learning (example, part 1)
In my last article, I stated that for practitioners (as opposed to theorists), the real prerequisite ...
- Android6.0-运行时权限处理
为什么需要有运行时权限? 大家都知道在Android6.0之前,权限在应用安装过程中只询问一次,以列表的形式展现给用户,如果点击取消(即不认可应用所申请的权限),则会取消应用的安装.而用户出于安装应用 ...
- xcode8.3 shell 自动打包脚本 记录
题记 xcode升级8.3后发现之前所用的xcode自动打包基本无法使用,因此在网上零碎找到些资料,将之前的脚本简化.此次脚本是基于xcode证书配置进行打包(之前是指定描述文件.相对繁琐).因此代码 ...
- SpringBoot学习helloworld
这几天开始学习springBoot记录一下(Hello World) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- springboot问题:解决异常Unable to start embedded container;
使用eclipse创建springboot练习时,当主函数与控制器同时写在同一个类时,启动项目正常运行,而当把主函数单独放在一个类中时,无论是与控制器同包还是控制器所在的包是其子包,都报: org.s ...
- php练习 租房子
题目要求 1.封装类 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 public $yonghuming=&q ...
- EntityFramework Core映射关系详解
前言 Hello,开始回归开始每周更新一到两篇博客,本节我们回归下EF Core基础,来讲述EF Core中到底是如何映射的,废话少说,我们开始. One-Many Relationship(一对多关 ...
- POJ 1459-Power Network(网络流-最大流-ISAP)C++
Power Network 时间限制: 1 Sec 内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...