|
@@ -0,0 +1,234 @@
|
|
|
|
+package com.yc.education.controller.admin;
|
|
|
|
+
|
|
|
|
+import com.yc.education.model.CommercialCarType;
|
|
|
|
+import com.yc.education.model.Provider;
|
|
|
|
+import com.yc.education.service.ICommercialCarTypeService;
|
|
|
|
+import com.yc.education.util.AjaxMessage;
|
|
|
|
+import com.yc.education.util.MapUtils;
|
|
|
|
+import com.yc.education.util.PinYin2Abbreviation;
|
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
|
+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.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @ClassName CommercialCarTypeController 商用车车型数据导入
|
|
|
|
+ * @Description TODO
|
|
|
|
+ * @Author QuZhangJing
|
|
|
|
+ * @Date 2019/12/19 9:35
|
|
|
|
+ * @Version 1.0
|
|
|
|
+ */
|
|
|
|
+@Controller
|
|
|
|
+@RequestMapping("admin")
|
|
|
|
+public class CommercialCarTypeController {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICommercialCarTypeService iCommercialCarTypeService;
|
|
|
|
+
|
|
|
|
+ @RequestMapping("commercialCarTypeExcel.html")
|
|
|
|
+ public ModelAndView commercialCarTypeExcel(){
|
|
|
|
+ return new ModelAndView();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @ResponseBody
|
|
|
|
+ @RequestMapping("commercialCarTypeExcelIn.html")
|
|
|
|
+ public AjaxMessage< Object > providerExcelIn(String url, HttpServletRequest request) throws IOException {
|
|
|
|
+
|
|
|
|
+ System.err.println("url:" + url);
|
|
|
|
+
|
|
|
|
+ Date begindata = new Date( );
|
|
|
|
+
|
|
|
|
+ String contextPath = request.getSession( ).getServletContext( ).getContextPath( );
|
|
|
|
+
|
|
|
|
+ List<CommercialCarType> list = new ArrayList< CommercialCarType>();
|
|
|
|
+
|
|
|
|
+ if (url.endsWith(".xls")) {
|
|
|
|
+ //2003版本的excel,用.xls结尾
|
|
|
|
+
|
|
|
|
+ InputStream is = new FileInputStream(url);
|
|
|
|
+ //得到工作簿
|
|
|
|
+
|
|
|
|
+ HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
|
|
|
|
+ //表格
|
|
|
|
+
|
|
|
|
+ HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
|
|
|
|
+
|
|
|
|
+ for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum( ); rowNum++) {
|
|
|
|
+ HSSFRow hssfRow = (HSSFRow) hssfSheet.getRow(rowNum);
|
|
|
|
+ //获取表格数据
|
|
|
|
+ if (hssfRow != null) {
|
|
|
|
+ CommercialCarType commercialCarType = new CommercialCarType();
|
|
|
|
+ commercialCarType.setKind(getValue(hssfRow.getCell(0)));
|
|
|
|
+ commercialCarType.setFactory(getValue(hssfRow.getCell(1)));
|
|
|
|
+ commercialCarType.setBrand(getValue(hssfRow.getCell(2)));
|
|
|
|
+ commercialCarType.setSaleVersion(getValue(hssfRow.getCell(3)));
|
|
|
|
+ commercialCarType.setNoticeType(getValue(hssfRow.getCell(4)));
|
|
|
|
+ commercialCarType.setNoticeBatch(getValue(hssfRow.getCell(5)));
|
|
|
|
+ commercialCarType.setEngineType(getValue(hssfRow.getCell(6)));
|
|
|
|
+ commercialCarType.setFuelKind(getValue(hssfRow.getCell(7)));
|
|
|
|
+ commercialCarType.setBlowdownStandard(getValue(hssfRow.getCell(8)));
|
|
|
|
+ commercialCarType.setBrandLetter(PinYin2Abbreviation.cn2py(commercialCarType.getBrand().substring(0,1)).toUpperCase());
|
|
|
|
+ commercialCarType.setCreateDate(new Date());
|
|
|
|
+ list.add(commercialCarType);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ FileInputStream fis = new FileInputStream(url);
|
|
|
|
+ //2007版本的excel,用.xlsx结尾
|
|
|
|
+ Workbook wookbook = new XSSFWorkbook(fis);//得到工作簿
|
|
|
|
+
|
|
|
|
+ //得到一个工作表
|
|
|
|
+ Sheet sheet = wookbook.getSheetAt(0);
|
|
|
|
+ //获得数据的总行数
|
|
|
|
+ int totalRowNum = sheet.getLastRowNum( );
|
|
|
|
+ //获得所有数据
|
|
|
|
+ for (int i = 1; i <= totalRowNum; i++) {
|
|
|
|
+ //获得第i行对象
|
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
|
+ //获取表格数据
|
|
|
|
+ CommercialCarType commercialCarType = new CommercialCarType();
|
|
|
|
+ commercialCarType.setKind(getValue(row.getCell(0)));
|
|
|
|
+ commercialCarType.setFactory(getValue(row.getCell(1)));
|
|
|
|
+ commercialCarType.setBrand(getValue(row.getCell(2)));
|
|
|
|
+ commercialCarType.setSaleVersion(getValue(row.getCell(3)));
|
|
|
|
+ commercialCarType.setNoticeType(getValue(row.getCell(4)));
|
|
|
|
+ commercialCarType.setNoticeBatch(getValue(row.getCell(5)));
|
|
|
|
+ commercialCarType.setEngineType(getValue(row.getCell(6)));
|
|
|
|
+ commercialCarType.setFuelKind(getValue(row.getCell(7)));
|
|
|
|
+ commercialCarType.setBlowdownStandard(getValue(row.getCell(8)));
|
|
|
|
+ commercialCarType.setBrandLetter(PinYin2Abbreviation.cn2py(commercialCarType.getBrand().substring(0,1)).toUpperCase());
|
|
|
|
+ commercialCarType.setCreateDate(new Date());
|
|
|
|
+ list.add(commercialCarType);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //批量导入数据
|
|
|
|
+ if (list.size( ) != 0) {
|
|
|
|
+ for (int i = 0; i < list.size( ) / 400 + 1; i++) {
|
|
|
|
+ int end = (i + 1) * 400;
|
|
|
|
+ if (end >= list.size( )) {
|
|
|
|
+ end = list.size( );
|
|
|
|
+ }
|
|
|
|
+ List< CommercialCarType > serviceList = list.subList(i * 400, end);
|
|
|
|
+ if (serviceList != null && serviceList.size( ) > 0) {
|
|
|
|
+ iCommercialCarTypeService.addByList(serviceList);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Date endtime = new Date( );
|
|
|
|
+ System.err.println("耗时:" + (endtime.getTime( ) - begindata.getTime( )));
|
|
|
|
+ AjaxMessage< Object > ajax = new AjaxMessage<>( );
|
|
|
|
+ //删除重复的
|
|
|
|
+ List< String > repeatList = iCommercialCarTypeService.queryId( );
|
|
|
|
+ iCommercialCarTypeService.deleteByRepeat(repeatList);
|
|
|
|
+ return ajax;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings({"static-access"})
|
|
|
|
+ public static String getValue(Cell cell) {
|
|
|
|
+ String value = "";
|
|
|
|
+ if (null == cell) {
|
|
|
|
+ return value;
|
|
|
|
+ }
|
|
|
|
+ switch (cell.getCellType( )) {
|
|
|
|
+ //数值型
|
|
|
|
+ case Cell.CELL_TYPE_NUMERIC:
|
|
|
|
+ if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
|
|
|
|
+ SimpleDateFormat sdf = null;
|
|
|
|
+ if (cell.getCellStyle( ).getDataFormat( ) == HSSFDataFormat
|
|
|
|
+ .getBuiltinFormat("h:mm")) {
|
|
|
|
+ sdf = new SimpleDateFormat("HH:mm");
|
|
|
|
+ } else {// 日期
|
|
|
|
+ sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
|
+ }
|
|
|
|
+ Date date = cell.getDateCellValue( );
|
|
|
|
+ value = sdf.format(date);
|
|
|
|
+ break;
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ value = String.valueOf(cell.getNumericCellValue( ));
|
|
|
|
+
|
|
|
|
+ String isdouble[] = value.split("\\.");
|
|
|
|
+ if (value.length( ) > 3) {
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0");
|
|
|
|
+ String whatYourWant = df.format(cell.getNumericCellValue( ));
|
|
|
|
+ value = whatYourWant;
|
|
|
|
+ } else if (isdouble.length > 1) {
|
|
|
|
+ if ("0".equals(isdouble[1])) {
|
|
|
|
+ DecimalFormat df = new DecimalFormat("0");
|
|
|
|
+ String whatYourWant = df.format(cell.getNumericCellValue( ));
|
|
|
|
+ value = whatYourWant;
|
|
|
|
+ } else {
|
|
|
|
+ double d = Double.valueOf(value);
|
|
|
|
+ d = getdouble(d);
|
|
|
|
+ value = Double.toString(d);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ //字符串类型
|
|
|
|
+ case Cell.CELL_TYPE_STRING:
|
|
|
|
+ value = cell.getStringCellValue( ).toString( );
|
|
|
|
+ break;
|
|
|
|
+ // 公式类型
|
|
|
|
+ case Cell.CELL_TYPE_FORMULA:
|
|
|
|
+ //读公式计算值
|
|
|
|
+ value = String.valueOf(cell.getNumericCellValue( ));
|
|
|
|
+ if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
|
|
|
|
+ value = cell.getStringCellValue( ).toString( );
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ // 布尔类型
|
|
|
|
+ case Cell.CELL_TYPE_BOOLEAN:
|
|
|
|
+ value = " " + cell.getBooleanCellValue( );
|
|
|
|
+ break;
|
|
|
|
+ // 空值
|
|
|
|
+ case Cell.CELL_TYPE_BLANK:
|
|
|
|
+ value = "";
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ // 故障
|
|
|
|
+ case Cell.CELL_TYPE_ERROR:
|
|
|
|
+ value = "";
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ value = cell.getStringCellValue( ).toString( );
|
|
|
|
+ }
|
|
|
|
+ if ("null".endsWith(value.trim( ))) {
|
|
|
|
+ value = "";
|
|
|
|
+ }
|
|
|
|
+ return value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static double getdouble(double d) {
|
|
|
|
+ BigDecimal b = new BigDecimal(d);
|
|
|
|
+ double f1 = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue( );
|
|
|
|
+ return f1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //================= 适用poi导入表格数据 结束 ===========================
|
|
|
|
+
|
|
|
|
+}
|