Guest User

Untitled

a guest
Nov 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import org.apache.poi.ss.usermodel.Cell;
  12. import org.apache.poi.ss.usermodel.Row;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14. import org.springframework.stereotype.Component;
  15.  
  16. @Component
  17. public class ExelUtil {
  18.  
  19.  
  20. public XSSFWorkbook getExcelFile(File file){
  21. try {
  22. XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
  23. return wb;
  24. } catch (FileNotFoundException e) {
  25. throw new BarcodeException("ERROR.5000");
  26. } catch (IOException e) {
  27. throw new BarcodeException("ERROR.0000");
  28. }
  29. }
  30.  
  31. public List<Map<String, String>> getExcelList(XSSFWorkbook wb,String headerData){
  32.  
  33. List<Map<String,String>> tempList = new ArrayList<Map<String,String>>();
  34. Map<Integer,String> header = new HashMap<Integer,String>();
  35. String[] arg =headerData.split(",");
  36.  
  37. for(int i=0;i<arg.length; i++){
  38. if(!arg[i].equals(""))
  39. header.put(i, arg[i]);
  40. }
  41.  
  42. for( Row row : wb.getSheetAt(0) ) {
  43.  
  44. Map<String,String> tempMap = new HashMap<String,String>();
  45. for(Cell cell :row){
  46. if(header.get(cell.getColumnIndex()) != null){
  47. switch( cell.getCellType()) {
  48. case Cell.CELL_TYPE_STRING :
  49. tempMap.put( header.get(cell.getColumnIndex()),cell.getRichStringCellValue().getString());
  50. break;
  51. case Cell.CELL_TYPE_NUMERIC :
  52. if(org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell))
  53. tempMap.put( header.get(cell.getColumnIndex()),cell.getDateCellValue().toString());
  54. else
  55. tempMap.put( header.get(cell.getColumnIndex()),Integer.toString((int)cell.getNumericCellValue()));
  56. break;
  57. case Cell.CELL_TYPE_FORMULA :
  58. tempMap.put( header.get(cell.getColumnIndex()),cell.getCellFormula());
  59. break;
  60. }
  61. }
  62. }
  63.  
  64. tempList.add(tempMap);
  65. }
  66.  
  67. return tempList;
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment