org.apache.poi
poi-ooxml
3.17
org.apache.poi
poi-ooxml-schemas
3.17
org.apache.poi
poi
3.17
km.importExcel = function(){
//弹出导入对话框
$('#importExcel').dialog('open');
}
//导入
km.uploadExcel = function(){
/* 配置导入框 */
$("#icItemPackImport").form({
type : 'post',
url : '${pageContext.request.contextPath}/uploadIn',
dataType : "json",
onSubmit: function() {
var fileName= $('#excelIcItemPack').filebox('getValue');
//对文件格式进行校验
var d1 = /\.[^\.]+$/.exec(fileName);
if (fileName == "") {
$.messager.alert('Excel批量导入', '请选择将要导入的文件!');
return false;
}else if(d1 != ".xls" && d1 != ".xlsx" ){
$.messager.alert('提示','请选择.xls格式或者.xlsx文件!','info');
return false;
}
$("#booten").linkbutton('disable');
return true;
},
success : function(data) {
var arr = JSON.parse(data)
console.log(arr)
if(arr.code == 200){
$('#dvshow').edatagrid('reload');
$.messager.alert('提示',arr.msg);
$('#importExcel').dialog('close');
}else if(arr.code == 101){
$.messager.alert('文件类型错误', arr.msg);
}else if(arr.code == 100) {
$('#dvshow').datagrid('loadData',[]);
$.messager.alert('提示',arr.msg);
}else{
$.messager.alert('提示', '系统繁忙,请稍后再试!');
}
}
});
$("#icItemPackImport").submit();
}
@RequestMapping("/uploadIn")
@ResponseBody
public Msg uploadIn(HttpServletRequest request) throws IOException {
//获取上传的文件
MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
//获取 from 表单提交的数据
MultipartFile file = multipart.getFile("excelIcItemPack");
//获取 文件 的名称
String originalFilename = file.getOriginalFilename();
// 获取 流
InputStream inputStream = file.getInputStream();
//定义 ... 呃 ...类似于定义Execl文件吧
Workbook workbook = new HSSFWorkbook();
//定义数据 返回格式 code msg 等等
Msg m = new Msg();
try {
// 2.通过poi解析流 得到 Excel文件对象
if(originalFilename.endsWith(".xls")) {
//2003
workbook = new HSSFWorkbook(inputStream);
}else if(originalFilename.endsWith(".xlsx")){
//2007
workbook = new XSSFWorkbook(inputStream);
}
} catch (OfficeXmlFileException e) {
m.setCode(101);
m.setMsg("只支持xls 类型文件和.xlsx结尾的,别的类型文件 私自更改为.xls或者.xlsx后缀,也无效哦~");
return m;
}
//3.通过对象获取数据 得到表
Sheet sheet = workbook.getSheetAt(0);
//4.通过表 得到所有行(获取有多少行,方便下面循环取值)
int lastRowNum = sheet.getLastRowNum();
//创建集合 存放数据
List arrlist = new ArrayList();
//创建全局时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 1; i <= lastRowNum; i++) {
//创建对象 存放Execl 每一行数据
StandingBook guru = new StandingBook();
//获取一行
Row row = sheet.getRow(i);
//判断是否为null 如果是null则获取方式不同 要用 createCell
//row.getCell(0) 表示 获取 一行的 第一个单元格
if(null == row.getCell(0)){
//取到的 是 null 用 createCell
guru.setProjectNumber(row.createCell(0).getStringCellValue());
}else{
//有值 用getCell 获取 getStringCellValue代表这个单元格是String类型 ,我获取时也是用String类型
guru.setProjectNumber(row.getCell(0).getStringCellValue());
}
// 以下同上
//row.getCell(1) 表示 获取 一行的 第二个单元格 记得是用下标获取
if(null == row.getCell(1)){
guru.setProjectName(row.createCell(1).getStringCellValue());
}else{
guru.setProjectName(row.getCell(1).getStringCellValue());
}
if(null == row.getCell(2)){
guru.setContractNo(row.createCell(2).getStringCellValue());
}else{
guru.setContractNo(row.getCell(2).getStringCellValue());
}
if(null == row.getCell(3)){
guru.setCustName(row.createCell(3).getStringCellValue());
}else{
guru.setCustName(row.getCell(3).getStringCellValue());
}
if(null == row.getCell(4)){
guru.setDeptName(row.createCell(4).getStringCellValue());
}else{
guru.setDeptName(row.getCell(4).getStringCellValue());
}
if(null == row.getCell(5)){
guru.setContractMonty(row.createCell(5).getStringCellValue());
}else{
//这里有值 单获取的时候因为类型的原因 我先把单元格的类型转为 String 之后再获取 String类型
Cell cell = row.getCell(5);
//转 String 类型
cell.setCellType(CellType.STRING);
//获取 它
guru.setContractMonty(cell.getStringCellValue());
}
if(null == row.getCell(6)){
guru.setReceiveBillDate(row.createCell(6).getDateCellValue().toString());
}else{
guru.setReceiveBillDate(sdf.format(row.getCell(6).getDateCellValue()));
}
if(null == row.getCell(7)){
guru.setProjectLeadName(row.createCell(7).getStringCellValue());
}else{
guru.setProjectLeadName(row.getCell(7).getStringCellValue());
}
if(null == row.getCell(8)){
guru.setManagement(row.createCell(8).getNumericCellValue());
}else{
Cell cell = row.getCell(8);
cell.setCellType(CellType.STRING);
// 这里我不能是null 或者 "" 因为下面要做运算 所以用三目判断一下 在转Double
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
// 运算
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setManagement(dgMultiply);
}
if(null == row.getCell(9)){
guru.setManagementBefore(row.createCell(9).getNumericCellValue());
}else{
Cell cell = row.getCell(9);
cell.setCellType(CellType.STRING);
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setManagementBefore(dgMultiply);
}
if(null == row.getCell(10)){
guru.setTaxes(row.createCell(10).getNumericCellValue());
}else{
Cell cell = row.getCell(10);
cell.setCellType(CellType.STRING);
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setTaxes(dgMultiply);
}
if(null == row.getCell(11)){
guru.setDuty(row.createCell(11).getNumericCellValue());
}else{
Cell cell = row.getCell(11);
cell.setCellType(CellType.STRING);
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setDuty(dgMultiply);
}
if(null == row.getCell(12)){
guru.setDatasets(row.createCell(12).getNumericCellValue());
}else{
Cell cell = row.getCell(12);
cell.setCellType(CellType.STRING);
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setDatasets(dgMultiply);
}
if(null == row.createCell(13)){
guru.setComplaintDeposit(row.createCell(13).getNumericCellValue());
}else{
Cell cell = row.getCell(13);
cell.setCellType(CellType.STRING);
Double stringCellValue = Double.parseDouble("".equals(cell.getStringCellValue())? "0.00":cell.getStringCellValue());
double dgMultiply = BigdecimalUtil.dgMultiply(stringCellValue+"","100");
guru.setComplaintDeposit(dgMultiply);
}
if(null == row.getCell(14)){
guru.setRemarkOne(row.createCell(14).getStringCellValue());
}else{
guru.setRemarkOne(row.getCell(14).getStringCellValue());
}
if(null == row.getCell(15)){
guru.setRemarkTwo(row.createCell(15).getStringCellValue());
}else{
guru.setRemarkTwo(row.getCell(15).getStringCellValue());
}
//添加进集合
arrlist.add(guru);
}
// 插入数据库
return importDataService.findProject(arrlist);
}