简体   繁体   中英

Returning Response while doing a background process in Spring boot

I am using spring boot to process a huge excel file. As it`sa time-consuming process, I want to return total number of records (as response) before my file is processed (after the file is processed I return the records as response). How can I do it?

You can get total rows using Apache POI.


            File file = new File(filePath);
            XSSFWorkbook myExcelBook = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet myExcelSheet = myExcelBook.getSheetAt(0);
            XSSFRow headerRow = myExcelSheet.getRow(0);
            
            int totalRows = myExcelSheet.getPhysicalNumberOfRows();


totalRows returns total number of rows in the excel sheet.

Edited

You can use two method calls for doing the same.

String file="/path/to/file";
int count=getExcelRecordCount(file);
Records records= getActualExcelRecords(file); 

Sounds like spring batch would be a good idea. Your first step would check the # of rows in the file by importing the data & counting the number of instances of a unique identifier. Or if you know the delimiter (pipe |? comma,? ) & the # of expected columns, you could just count the # of delimeters in the file and divide that by the number of feilds (or columns) and you have the # of rows.

In your next spring batch step you could do the rest of the processing.

Hope this is helpful, Pioneer!

Have fun with Boot, its very opinionated imo. but useful if you and spring both want the same things haha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM