java中使用poi导入excel文件
程序员文章站
2022-07-13 12:47:55
...
1、ExcelUtil工具类
- /**
- * Excel工具类
- * @author lp
- *
- */
- public class ExcelUtil {
- public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
- public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
- public static final String EMPTY = "";
- public static final String POINT = ".";
- public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
- /**
- * 获得path的后缀名
- * @param path
- * @return
- */
- public static String getPostfix(String path){
- if(path==null || EMPTY.equals(path.trim())){
- return EMPTY;
- }
- if(path.contains(POINT)){
- return path.substring(path.lastIndexOf(POINT)+1,path.length());
- }
- return EMPTY;
- }
- /**
- * 单元格格式
- * @param hssfCell
- * @return
- */
- @SuppressWarnings({ "static-access", "deprecation" })
- public static String getHValue(HSSFCell hssfCell){
- if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
- return String.valueOf(hssfCell.getBooleanCellValue());
- } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
- String cellValue = "";
- if(HSSFDateUtil.isCellDateFormatted(hssfCell)){
- Date date = HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue());
- cellValue = sdf.format(date);
- }else{
- DecimalFormat df = new DecimalFormat("#.##");
- cellValue = df.format(hssfCell.getNumericCellValue());
- String strArr = cellValue.substring(cellValue.lastIndexOf(POINT)+1,cellValue.length());
- if(strArr.equals("00")){
- cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
- }
- }
- return cellValue;
- } else {
- return String.valueOf(hssfCell.getStringCellValue());
- }
- }
- /**
- * 单元格格式
- * @param xssfCell
- * @return
- */
- public static String getXValue(XSSFCell xssfCell){
- if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
- return String.valueOf(xssfCell.getBooleanCellValue());
- } else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
- String cellValue = "";
- if(XSSFDateUtil.isCellDateFormatted(xssfCell)){
- Date date = XSSFDateUtil.getJavaDate(xssfCell.getNumericCellValue());
- cellValue = sdf.format(date);
- }else{
- DecimalFormat df = new DecimalFormat("#.##");
- cellValue = df.format(xssfCell.getNumericCellValue());
- String strArr = cellValue.substring(cellValue.lastIndexOf(POINT)+1,cellValue.length());
- if(strArr.equals("00")){
- cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
- }
- }
- return cellValue;
- } else {
- return String.valueOf(xssfCell.getStringCellValue());
- }
- }
- /**
- * 自定义xssf日期工具类
- * @author lp
- *
- */
- class XSSFDateUtil extends DateUtil{
- protected static int absoluteDay(Calendar cal, boolean use1904windowing) {
- return DateUtil.absoluteDay(cal, use1904windowing);
- }
- }</span></span>
- package com.ssm.util;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.web.multipart.MultipartFile;
- /**
- * 读取Excel
- * @author lp
- *
- */
- public class ExcelRead {
- public int totalRows; //sheet中总行数
- public static int totalCells; //每一行总单元格数
- /**
- * read the Excel .xlsx,.xls
- * @param file jsp中的上传文件
- * @return
- * @throws IOException
- */
- public List<ArrayList<String>> readExcel(MultipartFile file) throws IOException {
- if(file==null||ExcelUtil.EMPTY.equals(file.getOriginalFilename().trim())){
- return null;
- }else{
- String postfix = ExcelUtil.getPostfix(file.getOriginalFilename());
- if(!ExcelUtil.EMPTY.equals(postfix)){
- if(ExcelUtil.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
- return readXls(file);
- }else if(ExcelUtil.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)){
- return readXlsx(file);
- }else{
- return null;
- }
- }
- }
- return null;
- }
- /**
- * read the Excel 2010 .xlsx
- * @param file
- * @param beanclazz
- * @param titleExist
- * @return
- * @throws IOException
- */
- @SuppressWarnings("deprecation")
- public List<ArrayList<String>> readXlsx(MultipartFile file){
- List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
- // IO流读取文件
- InputStream input = null;
- XSSFWorkbook wb = null;
- ArrayList<String> rowList = null;
- try {
- input = file.getInputStream();
- // 创建文档
- wb = new XSSFWorkbook(input);
- //读取sheet(页)
- for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
- XSSFSheet xssfSheet = wb.getSheetAt(numSheet);
- if(xssfSheet == null){
- continue;
- }
- totalRows = xssfSheet.getLastRowNum();
- //读取Row,从第二行开始
- for(int rowNum = 1;rowNum <= totalRows;rowNum++){
- XSSFRow xssfRow = xssfSheet.getRow(rowNum);
- if(xssfRow!=null){
- rowList = new ArrayList<String>();
- totalCells = xssfRow.getLastCellNum();
- //读取列,从第一列开始
- for(int c=0;c<=totalCells+1;c++){
- XSSFCell cell = xssfRow.getCell(c);
- if(cell==null){
- rowList.add(ExcelUtil.EMPTY);
- continue;
- }
- rowList.add(ExcelUtil.getXValue(cell).trim());
- }
- }
- }
- }
- return list;
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * read the Excel 2003-2007 .xls
- * @param file
- * @param beanclazz
- * @param titleExist
- * @return
- * @throws IOException
- */
- public List<ArrayList<String>> readXls(MultipartFile file){
- List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
- // IO流读取文件
- InputStream input = null;
- HSSFWorkbook wb = null;
- ArrayList<String> rowList = null;
- try {
- input = file.getInputStream();
- // 创建文档
- wb = new HSSFWorkbook(input);
- //读取sheet(页)
- for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
- HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
- if(hssfSheet == null){
- continue;
- }
- totalRows = hssfSheet.getLastRowNum();
- //读取Row,从第二行开始
- for(int rowNum = 1;rowNum <= totalRows;rowNum++){
- HSSFRow hssfRow = hssfSheet.getRow(rowNum);
- if(hssfRow!=null){
- rowList = new ArrayList<String>();
- totalCells = hssfRow.getLastCellNum();
- //读取列,从第一列开始
- for(short c=0;c<=totalCells+1;c++){
- HSSFCell cell = hssfRow.getCell(c);
- if(cell==null){
- rowList.add(ExcelUtil.EMPTY);
- continue;
- }
- rowList.add(ExcelUtil.getHValue(cell).trim());
- }
- list.add(rowList);
- }
- }
- }
- return list;
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
@ResponseBody
@RequestMapping(value = "/import",method = RequestMethod.POST)
public responseRusult importIndex(@RequestParam(name = "file",required = false)MultipartFile indexLabelFile) {
ResponseRusult responseRusult = newResponseRusult ();
try {
if(indexLabelFile == null) {
responseRusult.error("请选择要上传的文件");
returnresponseRusult;
}
//读取Excel数据到List中
List<ArrayList<String>> list = new ExcelRead().readExcel(file);
indexService.indexImport(list);
responseRusult.success("导入成功");
} catch (Exception e) {
responseRusult.error("导入失败");
}
returnresponseRusult;
}
转载地址:点击打开链接 推荐阅读
-
使用Python和xlwt向Excel文件中写入中文的实例
-
Java使用POI操作Excel
-
使用Python在Excel中嵌入附件(txt文件,zip压缩包)对象
-
java中针对safari下载pdf、excel、word等文档变成exe文件解决办法
-
EXCEL使用文本导入向导打开含有 0开头的CSV格式的文件
-
Java实现Excel导入数据库,数据库中的数据导入到Excel
-
java开发中利用POI的 HSSFWorkbook 对excel进行操作
-
使用NPOI将Excel表导入到数据库中
-
在ASP.NET Core中使用EPPlus导入出Excel文件
-
将TXT文件中的内容正确导入到Excel表格中不混乱的方法