<a class="btn btn-warning" onclick="easyImportExcel()">
<i class="fa fa-download"></i> 导入
</a>
<input type="file" id="file" onchange="easyImportExcelDo()"
style="filter:alpha(opacity=0);opacity:0;width: 0;height: 0;"/>
function easyImportExcel(fileId) {
var currentId = $.common.isEmpty(fileId) ? 'file' : fileId;
$("#" + currentId).trigger("click");
}
function easyImportExcelDo(fileId) {
$.modal.loading("正在导入,请等待");
var currentId = $.common.isEmpty(fileId) ? 'file' : fileId;
var file = $("#" + currentId)[0].files[0];
if (file == 'undefined' || file == undefined) {
return;
}
var myform = new FormData();
myform.append('file', file);
$.ajax({
url: prefix + "/import",
type: "POST",
data: myform,
contentType: false,
processData: false,
success: function (result) {
if (result.code == 0) {
$("#" + currentId).val("");
$.modal.closeLoading();
$.modal.msgSuccess(result.msg);
$.table.refresh();
} else {
$.modal.msgError(result.msg);
}
$("#" + currentId).val("");
$.modal.closeLoading();
},
error: function (data) {
console.log(data);
$("#" + currentId).val("");
$.modal.closeLoading();
}
});
}
import static com.ruoyi.common.utils.UploadFileToOSSUtils.uploadImgByByte;
import static com.ruoyi.common.utils.poi.EasyExcelUtil.getPictureFromExcel;
@PostMapping("/import")
@ResponseBody
public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws Exception {
List<Goods> itemList = EasyExcelUtil.importExcel(file, 1, 1, Goods.class);
if (itemList == null) {
return error(AjaxResult.Type.WARN, "请选择正确的导入模板");
}
int insertCount = 0;
List<Map<String, PictureData>> pictureList = getPictureFromExcel(file);
int row = 2;
for (Goods item : itemList) {
Shop shop = shopService.selectShopByName(item.getShopName());
GoodsType goodstype = goodsTypeService.selectGoodsTypeByTypenameAndShopname(item.getTypeName(), item.getShopName());
if (null == shop) {
return error(AjaxResult.Type.WARN, item.getName() + "这件商品信息错误! 原因:未找到对应店铺");
}
if (null == goodstype) {
return error(AjaxResult.Type.WARN, item.getName() + "这件商品信息错误! 原因:未找到对应的分类");
}
PictureData img = pictureList.get(0).get("0_" + row + "_4");
String imgUrl = uploadImgByByte(img.getData());
item.setImgUrl(imgUrl);
row++;
}
for (Goods item : itemList) {
Shop shop = shopService.selectShopByName(item.getShopName());
GoodsType goodstype = goodsTypeService.selectGoodsTypeByTypenameAndShopname(item.getTypeName(), item.getShopName());
int i = goodsService.importGoods(item, shop, goodstype);
insertCount+=i;
}
return success("导入成功,新增【" + insertCount + "】条记录!");
}
public static List<Map<String, PictureData>> getPictureFromExcel(MultipartFile file) throws EncryptedDocumentException, InvalidFormatException, IOException {
InputStream input = file.getInputStream();
String fileExt = file.getName().substring(file.getName().lastIndexOf(".") + 1);
Workbook wb = null;
Sheet sheet = null;
if (fileExt.equals("xls")) {
wb = (HSSFWorkbook) WorkbookFactory.create(input);
} else {
wb = new XSSFWorkbook(input);
}
int sheetNumbers = wb.getNumberOfSheets();
List<Map<String, PictureData>> sheetList = new ArrayList<Map<String, PictureData>>();
for (int i = 0; i < sheetNumbers; i++) {
sheet = wb.getSheetAt(i);
Map<String, PictureData> sheetIndexPicMap;
if (fileExt.equals("xls")) {
sheetIndexPicMap = getSheetPictrues03(i, (HSSFSheet) sheet, (HSSFWorkbook) wb);
} else {
sheetIndexPicMap = getSheetPictrues07(i, (XSSFSheet) sheet, (XSSFWorkbook) wb);
}
sheetList.add(sheetIndexPicMap);
}
return sheetList;
}
public static Map<String, PictureData> getSheetPictrues03(int sheetNum, HSSFSheet sheet, HSSFWorkbook workbook) {
Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
List<HSSFPictureData> pictures = workbook.getAllPictures();
if (pictures.size() != 0) {
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
if (shape instanceof HSSFPicture) {
HSSFPicture pic = (HSSFPicture) shape;
int pictureIndex = pic.getPictureIndex() - 1;
HSSFPictureData picData = pictures.get(pictureIndex);
String picIndex = String.valueOf(sheetNum) + "_"
+ String.valueOf(anchor.getRow1()) + "_"
+ String.valueOf(anchor.getCol1());
sheetIndexPicMap.put(picIndex, picData);
}
}
return sheetIndexPicMap;
} else {
return null;
}
}
public static Map<String, PictureData> getSheetPictrues07(int sheetNum,XSSFSheet sheet, XSSFWorkbook workbook) {
Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) dr;
List<XSSFShape> shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture pic = (XSSFPicture) shape;
XSSFClientAnchor anchor = pic.getPreferredSize();
CTMarker ctMarker = anchor.getFrom();
String picIndex = String.valueOf(sheetNum) + "_"
+ ctMarker.getRow() + "_" + ctMarker.getCol();
sheetIndexPicMap.put(picIndex, pic.getPictureData());
}
}
}
return sheetIndexPicMap;
}
public static String uploadImgByByte(byte[] img) {
InputStream sbs = new ByteArrayInputStream(img);
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String key = UUID.randomUUID().toString() + System.currentTimeMillis() + ".png";
ossClient.putObject(bucketName, key, sbs);
OSSObject object = ossClient.getObject("zhuxing-public", key);
ossClient.shutdown();
String uri = object.getResponse().getUri();
if (uri.contains("http")) {
uri = uri.replace("http", "https");
}
return uri;
}