excelutil工具类
package com.zckj.utils;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
public class ExcelUtil {
// 总行数
private int totalRows = 0;
// 总条数
private int totalCells = 0;
// 错误信息接收器
private String errorMsg;
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalCells() {
return totalCells;
}
public void setTotalCells(int totalCells) {
this.totalCells = totalCells;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
/**
* 读EXCEL文件,获取信息集合
* @param fielName
* @return
*/
public List> getExcelInfo(MultipartFile mFile,String head1,String head2,String head3,String head4,String head5) {
String fileName = mFile.getOriginalFilename();// 获取文件名
List> list = null;
try {
if (!validateExcel(fileName)) {// 验证文件名是否合格
return null;
}
boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
if (isExcel2007(fileName)) {
isExcel2003 = false;
}
list = createExcel(mFile.getInputStream(), isExcel2003,head1,head2,head3,head4,head5);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public List> getExcelInfo(MultipartFile mFile,String head1,String head2) {
String fileName = mFile.getOriginalFilename();// 获取文件名
List> list = null;
try {
if (!validateExcel(fileName)) {// 验证文件名是否合格
return null;
}
boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
if (isExcel2007(fileName)) {
isExcel2003 = false;
}
list = createExcel(mFile.getInputStream(), isExcel2003,head1,head2);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 根据excel里面的内容读取客户信息
* @param is 输入流
* @param isExcel2003 excel是2003还是2007版本
* @return
* @throws IOException
*/
public List> createExcel(InputStream is, boolean isExcel2003,String head1,String head2,String head3,String head4,String head5) {
List> list=null;
try{
Workbook wb = null;
if (isExcel2003) {// 当excel是2003时,创建excel2003
wb = new HSSFWorkbook(is);
} else {// 当excel是2007时,创建excel2007
wb = new XSSFWorkbook(is);
}
list= readExcelValue(wb,head1,head2,head3,head4,head5);// 读取Excel里面客户的信息
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public List> createExcel(InputStream is, boolean isExcel2003,String head1,String head2) {
List> list=null;
try{
Workbook wb = null;
if (isExcel2003) {// 当excel是2003时,创建excel2003
wb = new HSSFWorkbook(is);
} else {// 当excel是2007时,创建excel2007
wb = new XSSFWorkbook(is);
}
list= readExcelValue(wb,head1,head2);// 读取Excel里面客户的信息
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 读取Excel里面客户的信息
*
* @param wb
* @return
*/
private List> readExcelValue(Workbook wb,String head1,String head2,String head3,String head4,String head5) {
// 得到第一个shell
Sheet sheet = wb.getSheetAt(0);
// 得到Excel的行数
this.totalRows = sheet.getPhysicalNumberOfRows();
// 得到Excel的列数(前提是有行数)
if (totalRows > 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println(totalCells);
}
List> lists= new ArrayList>();
// 循环Excel行数
String pdhead1="";
String pdhead2="";
String pdhead3="";
String pdhead4="";
String pdhead5="";
for (int r = 0; r < 1; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
Cell cell3 = row.getCell(2);
Cell cell4 = row.getCell(3);
Cell cell5 = row.getCell(4);
pdhead1=cell1.getStringCellValue().trim();// 名称
pdhead2=cell2.getStringCellValue().trim();// 名称
pdhead3=cell3.getStringCellValue().trim();// 名称
pdhead4=cell4.getStringCellValue().trim();// 名称
pdhead5=cell5.getStringCellValue().trim();// 名称
}
if(head1.equals(pdhead1)&&head2.equals(pdhead2)&&head3.equals(pdhead3)&&head4.equals(pdhead4)&&head5.equals(pdhead5)){
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
List list =new ArrayList();
// 循环Excel的列
for (int c = 0; c < this.totalCells; c++) {
Cell cell = row.getCell(c);
String str=" ";
if (null != cell) {
/*row.getCell(c).setCellType(Cell.CELL_TYPE_STRING);
str=row.getCell(c).getStringCellValue().trim();*/
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
str=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
else{
//DecimalFormat formatter =new DecimalFormat();
//str= String.valueOf(cell.getNumericCellValue());
//str=str.substring(0, str.length() - 2 > 0 ? str.length() - 2 : 1);
row.getCell(c).setCellType(Cell.CELL_TYPE_STRING);
str=row.getCell(c).getStringCellValue().trim();
}
} else {
str=cell.getStringCellValue();// 名称
}
/*// 如果是纯数字,比如你写的是25,cell.getNumericCellValue()获得是25.0,通过截取字符串去掉.0获得25
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String name = String.valueOf(cell.getNumericCellValue());
user.setName(name.substring(0, name.length() - 2 > 0 ? name.length() - 2 : 1));// 名称
} else {
user.setName(cell.getStringCellValue());// 名称
}*/
}
// 添加到list
list.add(str);
}
lists.add(list);
if(r==2){
System.out.println("+++++++++"+lists);
}
}
}
return lists;
}
private List> readExcelValue(Workbook wb,String head1,String head2) {
Sheet sheet = wb.getSheetAt(0);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (totalRows > 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println(totalCells);
}
List> lists= new ArrayList>();
String pdhead1="";
String pdhead2="";
for (int r = 0; r < 1; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
pdhead1=cell1.getStringCellValue().trim();// 名称
pdhead2=cell2.getStringCellValue().trim();// 名称
}
if(head1.equals(pdhead1)&&head2.equals(pdhead2)){
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
List list =new ArrayList();
for (int c = 0; c < this.totalCells; c++) {
Cell cell = row.getCell(c);
String str=" ";
if (null != cell) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
str=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
else{
row.getCell(c).setCellType(Cell.CELL_TYPE_STRING);
str=row.getCell(c).getStringCellValue().trim();
}
} else {
str=cell.getStringCellValue();// 名称
}
}
list.add(str);
}
lists.add(list);
}
}
return lists;
}
/**
* 验证EXCEL文件
*
* @param filePath
* @return
*/
public boolean validateExcel(String filePath) {
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorMsg = "文件名不是excel格式";
return false;
}
return true;
}
// @描述:是否是2003的excel,返回true是2003
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
// @描述:是否是2007的excel,返回true是2007
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
}
注: 标题栏中制表符、空格问题
service
ExcelUtil readExcel = new ExcelUtil();
List lists =new ArrayList();
List> list = readExcel.getExcelInfo(file,"","","","","");
for (int i=0;i
controller
@RequestParam(value="file",required=false)MultipartFile file
ajax
var formData = new FormData();
if($("#in_td")[0].files[0]==undefined){
alert("请选择文件");
return;
}
formData.append("file", $("#in_td")[0].files[0]);
$.ajax({
type : 'POST',
url : "cj/insert_td",
data : formData,
contentType : false,
processData : false, //这个很有必要,不然不行
mimeType : "multipart/form-data",
success : function() {
$.messager.alert("提示","导入成功");
},error:function(){
$.messager.alert("提示","导入失败");
}
});