package com.offcn;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AppTest {
public void test02(){
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File("d:\\Desktop\\Book1.xls"));
//获取sheet表,默认是从0开始的
Sheet sheet = wb.getSheetAt(0);
//第一行行号为0
int firstRowNum = sheet.getFirstRowNum();
//最后一行的行号
int lastRowNum = sheet.getLastRowNum();
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
//第一列的索引为0
System.out.println(row.getFirstCellNum());
//最后一列的索引要大1
System.out.println(row.getLastCellNum());
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
String value=parseExcelValueToString(cell);
//String stringCellValue = cell.getStringCellValue();
//System.out.print(stringCellValue+" ");
}
}
} catch (Exception e) {
System.out.println("出异常了");
}
}
//将excel中加载过来的数据转为String类型
private String parseExcelValueToString(Cell cell) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String result="";
//getCellTypeEnum()获取单元格的枚举类型
switch (cell.getCellTypeEnum()){
case STRING:
result=cell.getStringCellValue();
break;
case BOOLEAN:
result=String.valueOf(cell.getBooleanCellValue());
break;
//公式类型
case FORMULA:
result=cell.getCellFormula();
break;
case BLANK:
result="";
break;
case NUMERIC:
//在excel中将数字和时间类型统一看作NUMERIC类型
//判断单元格内的类型是否是时间类型的
if(HSSFDateUtil.isCellDateFormatted(cell)){
//获取单元格内的时间类型的值
Date date = cell.getDateCellValue();
//转为日期的格式
result=sdf.format(date);
}else{
//若是数字类型直接获取值
double numericCellValue = cell.getNumericCellValue();
//从excel中加载过来的数字是科学计数法的形式,需要转换
//将科学计数法的形式转为正常的数字形式
BigDecimal decimal=new BigDecimal(String.valueOf(numericCellValue));
result=decimal.toPlainString();
}
break;
default:
result="";
}
return result;
}
}
- 上传excel
前端:
function importExcel() {
var formData=new FormData();
var result=setInterval(function () {
var file=$("file")[0].file[0];
if(file != undefined){
clearInterval(result);
formData.append("excel",file);
//异步上传excel
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/cust/importExcel",
cache:false,
processData:false,
contentType:false,
data:formData,
success:function (msg) {
}
});
}
},1000)
}
<style>
#uploadImg{
font-size: 16px;
overflow: hidden;
position: absolute;
}
#file{
position: absolute;
z-index: 100;
margin-left: -180px;
font-size: 60px;
opacity: 0;
filter: alpha(opacity=0);
margin-top: -5px;
}
</style>
<span id="uploadImg">
<input type="file" id="file" size="1" onclick="importExcel()">
<a href="javascript:void(0)" > 上传Excel</a>
</span>
后端:
@RequestMapping(value="/importExcel",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> importExcel(MultipartFile excel){
Map<String,Object> map=new HashMap<String,Object>();
List<Customer> customers=new ArrayList<Customer>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Workbook wb = null;
try {
wb = WorkbookFactory.create(excel.getInputStream());
//获取sheet表,默认是从0开始的
//getNumberOfSheets():sheet的个数
for (int k = 0; k <wb.getNumberOfSheets() ; k++) {
Sheet sheet = wb.getSheetAt(k);
//第一行行号为0
int firstRowNum = sheet.getFirstRowNum();
//最后一行的行号
int lastRowNum = sheet.getLastRowNum();
//标题行必须要获取,所以getFirstRowNum()加1
for (int i = sheet.getFirstRowNum()+1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Customer customer=new Customer();
if(row!=null){
//第一列的索引为0
System.out.println(row.getFirstCellNum());
//最后一列的索引要大1
System.out.println(row.getLastCellNum());
String linkMan=row.getCell(1).getStringCellValue();
customer.setCompanyperson(linkMan);
String companyName=row.getCell(2).getStringCellValue();
customer.setComname(companyName);
Date dateCellValue = row.getCell(3).getDateCellValue();
String format=sdf.format(dateCellValue);
Date addTime = sdf.parse(format);
customer.setAddtime(addTime);
double numericCellValue = row.getCell(4).getNumericCellValue();
BigDecimal decimal=new BigDecimal(String.valueOf(numericCellValue));
customer.setComphone(decimal.toPlainString());
// for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
// Cell cell = row.getCell(j);
// String value= ExcelUtils.parseExcelValueToString(cell);
// //String stringCellValue = cell.getStringCellValue();
// //System.out.print(stringCellValue+" ");
// }
}
customers.add(customer);
}
}
map.put("statusCode",200);
map.put("message","上传成功");
} catch (Exception e) {
System.out.println("出异常了");
map.put("statusCode",500);
map.put("message","上传失败");
}
return map;
}