测试类 导入excel

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ "/applicationContext.xml", "classpath*:spring/whq.core.service.xml",
        "classpath*:spring-mvc.xml" })
public class BaseJunit extends AbstractJUnit4SpringContextTests {

   @Autowired
    protected ApplicationContext ctx;

    protected ApplicationContext getContext() {
        return applicationContext;
    }

}

 

 

public class ShopeeCategoryRelationTest extends BaseJunit {

    @Autowired
    private IShopeeCategoryRelationService shopeeCategoryRelationService;

    private static final String EXCEL_FILE = "E:\\shopee.xlsx";

    @Test
    public void syncShopeeLogistics() throws IOException {
        FileInputStream fis = new FileInputStream(new File(EXCEL_FILE));

        Workbook workbook = readExcel(fis);

        Sheet sheet = workbook.getSheetAt(0); // 得到第一个sheet
        int rowNum = sheet.getLastRowNum();

        Row row = null;

        shopeeCategoryRelationService.deleteByExample(null);

        for (int i = 1; i < rowNum + 1; i++) {

            row = sheet.getRow(i);

        } // end for row

        System.out.println("完成");
        fis.close();
    }

 

 

public static Workbook readExcel(InputStream inputStream) {

        try {
            if (!inputStream.markSupported()) {
                inputStream = new PushbackInputStream(inputStream, 8);
            }
            if (POIFSFileSystem.hasPOIFSHeader(inputStream)) {
                return new HSSFWorkbook(inputStream);
            }
            if (POIXMLDocument.hasOOXMLHeader(inputStream)) {
                return new XSSFWorkbook(OPCPackage.open(inputStream));
            }

        }
        catch (Exception e) {
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                }
            }
        }

        return null;
    }

你可能感兴趣的:(测试类 导入excel)