code
import com.lenovo.pcsd.bp.businesspartner.bean.LenovoShopProductInfo; import com.lenovo.pcsd.bp.businesspartner.bean.ShopProductInfoPageData; import com.lenovo.pcsd.bp.businesspartner.bean.ShopProductInfoSearchParam; import com.lenovo.pcsd.bp.businesspartner.mapper.LenovoShopProductInfoMapper; import com.lenovo.pcsd.bp.businesspartner.service.LenovoShopProductInfoService; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ClassUtils; import org.springframework.util.ResourceUtils; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io. *; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class LenovoShopProductInfoServiceImpl implements LenovoShopProductInfoService { @Autowired private LenovoShopProductInfoMapper lenovoShopProductInfoInfoMapper; @Override public ShopProductInfoPageData getLenovoShopProductInfoList(ShopProductInfoSearchParam param){ if(null != param){ Integer dataCount = lenovoShopProductInfoInfoMapper.getLenovoShopProductInfoDataCount(param); Integer pageCount = dataCount%param.getPageSize()==0 ? (dataCount/param.getPageSize()) : (dataCount/param.getPageSize() + 1); ShopProductInfoPageData pageData = new ShopProductInfoPageData(); pageData.setDataCount(dataCount); pageData.setPageSize(param.getPageSize()); pageData.setCurPageNo(param.getCurPageNo()); pageData.setPageCount(pageCount); pageData.setProductInfoList(lenovoShopProductInfoInfoMapper.getLenovoShopProductInfoList(param)); return pageData; } else { return null; } } @Override public File getLenovoShopProductInfoFile(ShopProductInfoSearchParam param) throws IOException, NoSuchFieldException, IntrospectionException, InvocationTargetException, IllegalAccessException { if(null != param){ List<LenovoShopProductInfo> dataList = lenovoShopProductInfoInfoMapper.getLenovoShopProductInfoList(param); // Copy the template file as a data export file, // String templateFileName = Thread.currentThread().getContextClassLoader() // .getResource("excelTemplate/export_shop_product_info.xls").getPath(); // File excelTemplate = ResourceUtils.getFile("classpath:excelTemplate/export_shop_product_info.xls"); // After the project is packaged into a jar package, the template cannot be read and can only be read in the input stream mode, so the copy template is changed to this writing InputStream in = ClassUtils.class.getClassLoader().getResourceAsStream("excelTemplate/export_shop_product_info.xls"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(System.currentTimeMillis()); String parentDirName = sdf1.format(date); String dataFileEnd = sdf2.format(date)+"_"+System.currentTimeMillis()+".xls"; String dataFileName = "/lenovo-store-export/shop-product-info/"+parentDirName+"/"+dataFileEnd; File dataFile = new File(dataFileName); //FileUtils.copyFile(excelTemplate, dataFile); FileOutputStream fileOut1 = new FileOutputStream(dataFileName); IOUtils.copy(in, fileOut1); fileOut1.close(); // write data to dataFile Workbook wb = new HSSFWorkbook(new FileInputStream(dataFile)); for (int t = 0; t < wb.getNumberOfSheets(); t++) { Sheet sheet = wb.getSheetAt(t); int lastRowNum = sheet.getLastRowNum(); if(lastRowNum <0){ continue; } Row row = sheet.getRow(0); List<String> fieldList = new ArrayList<String>(); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String value = getCellValue(cell) ; if(!value.equals("")){ fieldList.add(value); } } } if(fieldList.isEmpty()){ continue; } // delete the configuration field line of the data file removeRow(sheet, 0); / / Loop data data, fill in the data column by column according to the fields required by the excel file if(null != dataList && !dataList.isEmpty()){ Class clazz = dataList.get(0).getClass(); for(int i=0; i<dataList.size(); i++){ LenovoShopProductInfo productInfo = dataList.get(i); Row newRow = sheet.createRow(i+1); for(int x=0; x < fieldList.size(); x++ ){ String fName = fieldList.get(x); Field f = clazz.getDeclaredField(fName); PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz); Method rM = pd.getReadMethod();//Get the read method Object val = (Object) rM.invoke(productInfo); Cell tempCell = newRow.createCell(x); tempCell.setCellValue(String.valueOf(val)); } } } } // keep FileOutputStream fileOut2 = new FileOutputStream(dataFileName); wb.write(fileOut2); fileOut2.close(); return dataFile; } else { return null; } } /*** * read cell value * @param cell * @return */ private String getCellValue(Cell cell) { Object result = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; case Cell.CELL_TYPE_ERROR: result = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_BLANK: break; default: break; } } return result.toString(); } /** * Delete row records in sheet page * @param sheet * @param rowIndex */ private void removeRow(Sheet sheet, int rowIndex) { int lastRowNum = sheet.getLastRowNum(); if(rowIndex>=0 && rowIndex<lastRowNum) { //Move all the cells whose row number is rowIndex+1 until the row number is lastRowNum up one row, so as to delete rowIndex row sheet.shiftRows(rowIndex + 1, lastRowNum, -1); } if(rowIndex == lastRowNum){ Row removingRow = sheet.getRow(rowIndex); if(removingRow!=null){ sheet.removeRow(removingRow); } } } }