Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.43 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.Scanner;
  10.  
  11. import org.apache.poi.hssf.util.CellReference;
  12. import org.apache.poi.ss.usermodel.*;
  13.  
  14.  
  15. public class Main {
  16.     public static ArrayList<File> files = new ArrayList<>();
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         Scanner in = new Scanner(System.in);
  21.         System.out.println("Unesite pocetni indeks: ");
  22.         int ID  = in.nextInt() - 1;
  23.         String tmpParent = null;
  24.         System.out.println("Unesite putanju do IZVOZ_arh");
  25.         in = new Scanner(System.in);
  26.         String path = in.nextLine();
  27.         listFilesForFolder(path,files);
  28.         Workbook workbook = null;
  29.         boolean isPL;
  30.         for(File f: files){
  31.             isPL = false;
  32.             if(f.getName().contains("xlsx") && f.getName().charAt(0)!='~') {
  33.                 try {
  34.                     workbook = WorkbookFactory.create(f);
  35.                     for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
  36.                         if (workbook.getSheetAt(i).getSheetName().contains("PL"))
  37.                             isPL = true;
  38.                     }
  39.                     if(!f.getParent().equals(tmpParent)) {
  40.                         ID++;
  41.                         tmpParent = f.getParent();
  42.                     }
  43.                     if (isPL) {
  44.                         System.out.println("\nPACKING LIST\n");
  45.                         System.out.println(f.getAbsoluteFile());
  46.                         //printToFile("\n"+f.getAbsolutePath()+"\n",true);
  47.                         handlePL(workbook,f,ID);
  48.                     } else {
  49.                         System.out.println("\nOTHER\n");
  50.                         System.out.println(f.getAbsoluteFile());
  51.                         //printToFile("\n"+f.getAbsolutePath()+"\n",false);
  52.                         handleOther(workbook,f,ID);
  53.                     }
  54.                 }catch (Exception e) {
  55.                     e.printStackTrace();
  56.                     System.err.println("\n\n"+f.getAbsolutePath()+"\n\n*******Neispravan fajl*******");
  57.                 }
  58.             }
  59.         }
  60.  
  61.     }
  62.  
  63.     private static void handlePL(Workbook workbook, File f, int ID){
  64.         int gornjaGranica = 9;
  65.         FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
  66.         boolean imaNapomenu = false;
  67.         int cliente = 0;
  68.  
  69.         try {
  70.             String[] fakture = null;
  71.             Sheet sheet;
  72.             Cell fatturaCell = null;
  73.             String fattura;
  74.             Iterator<Row> rowIterator = null;
  75.             Cell dateCell = null;
  76.             sheet = workbook.getSheet("PL");
  77.             if (sheet.iterator() != null)
  78.                 rowIterator = sheet.iterator();
  79.             if (sheet.getRow(9).getCell(0).getCellType() == Cell.CELL_TYPE_STRING)
  80.                 if (sheet.getRow(9).getCell(0).getStringCellValue().contains("CLIENTE")) {
  81.                     gornjaGranica = 10;
  82.                     cliente = 1;
  83.                 }
  84.             if (rowIterator != null) {
  85.                 while (rowIterator.hasNext()) {
  86.                     Row r = rowIterator.next();
  87.                     Cell c;
  88.                     boolean found = false;
  89.                     if (r.getRowNum() > 4)
  90.                         break;
  91.                     int start = r.getFirstCellNum();
  92.                     int end = r.getLastCellNum();
  93.                     for (int i = start; i < end; i++) {
  94.                         if ((r.getCell(i) != null) && (c = r.getCell(i)).getCellType() == Cell.CELL_TYPE_STRING) {
  95.                             if (c.getStringCellValue().contains("Fattura") || c.getStringCellValue().contains("fattura")) {
  96.                                 if(cliente == 0)
  97.                                     fatturaCell = c;
  98.                                 else
  99.                                     fatturaCell = r.getCell(i+1);
  100.                                 found = true;
  101.                                 break;
  102.                             }
  103.                         }
  104.                     }
  105.                     if (found)
  106.                         break;
  107.                 }
  108.             }
  109.             if (fatturaCell != null) {
  110.                 fattura = fatturaCell.getStringCellValue();
  111.                 if(cliente == 0) {
  112.                     fakture = fattura.substring(fattura.indexOf(":") + 1).trim().split(";");
  113.                     System.out.println(fattura.substring(fattura.indexOf(":") + 1).trim());
  114.                 }
  115.                 else{
  116.                     fakture = fattura.trim().split(";");
  117.                     System.out.println(fattura.trim());
  118.                 }
  119.  
  120.                 //printToFile(fattura.substring(fattura.indexOf(":") + 1).trim(),true);
  121.             }
  122.             rowIterator = sheet.iterator();
  123.             while (rowIterator.hasNext()) {
  124.                 Row r = rowIterator.next();
  125.                 Cell c;
  126.                 boolean found = false;
  127.                 if (r.getRowNum() > 4)
  128.                     break;
  129.                 int start = r.getFirstCellNum();
  130.                 int end = r.getLastCellNum();
  131.                 for (int i = start; i <= end; i++) {
  132.                     if (r.getCell(i) != null && (c = r.getCell(i)).getCellType() == Cell.CELL_TYPE_NUMERIC) {
  133.                         if (DateUtil.isCellDateFormatted(c)) {
  134.                             dateCell = c;
  135.                             found = true;
  136.                             break;
  137.                         }
  138.                     }
  139.                 }
  140.                 if (found)
  141.                     break;
  142.             }
  143.             if (dateCell != null) {
  144.                 String format = "yyyy-MM-dd";
  145.                 SimpleDateFormat date = new SimpleDateFormat(format);
  146.                 String dateString = date.format(dateCell.getDateCellValue());
  147.                 System.out.println("DATUM: " + dateString);
  148.                 //printToFile("DATUM: " + dateString,true);
  149.                 if (fatturaCell != null) {
  150.                     for (int i = 0; i < fakture.length; i++) {
  151.                         printID(ID + "#" + dateString + "#" + fakture[i].trim());
  152.                     }
  153.  
  154.                 }
  155.             }
  156.             rowIterator = sheet.iterator();
  157.             while (rowIterator.hasNext()) {
  158.  
  159.                 Row rowIte = rowIterator.next();
  160.                 if (cliente == 0)
  161.                     if (rowIte.getRowNum() == gornjaGranica - 1) {
  162.                         if (rowIte.getCell(12) != null && rowIte.getCell(12).getCellType() == Cell.CELL_TYPE_STRING &&
  163.                                 rowIte.getCell(12).getStringCellValue().contains("Napomena")) {
  164.                             imaNapomenu = true;
  165.                         }
  166.                     }
  167.  
  168.                 String rowBuff = "";
  169.                 if (cliente == 0) {
  170.                     try {
  171.                         if (rowIte.getRowNum() >= gornjaGranica) {
  172.                             if(rowIte.getCell(0)!= null) {
  173.                                 if (rowIte.getCell(0).getCellType() == Cell.CELL_TYPE_BLANK)
  174.                                     return;
  175.                                 if (rowIte.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
  176.                                     if (rowIte.getCell(0).getStringCellValue().contains("blank"))
  177.                                         return;
  178.                                 }
  179.                             }
  180.                             else return;
  181.                             if (rowIte.getCell(2 ).getCellType() == Cell.CELL_TYPE_STRING)
  182.                                 rowBuff += rowIte.getCell(2 ).getStringCellValue();
  183.                             else if (rowIte.getCell(2 ).getCellType() == Cell.CELL_TYPE_NUMERIC)
  184.                                 rowBuff += rowIte.getCell(2 ).getNumericCellValue();
  185.                             rowBuff += "#";
  186.                             if (rowIte.getCell(3 ).getCellType() == Cell.CELL_TYPE_STRING)
  187.                                 rowBuff += rowIte.getCell(3 ).getStringCellValue();
  188.                             else if (rowIte.getCell(3 ).getCellType() == Cell.CELL_TYPE_NUMERIC)
  189.                                 rowBuff += rowIte.getCell(3 ).getNumericCellValue();
  190.                             rowBuff += "#";
  191.                             if (rowIte.getCell(8 ).getCellType() == Cell.CELL_TYPE_STRING)
  192.                                 rowBuff += rowIte.getCell(8 ).getStringCellValue();
  193.                             else if (rowIte.getCell(8 ).getCellType() == Cell.CELL_TYPE_NUMERIC)
  194.                                 rowBuff += rowIte.getCell(8 ).getNumericCellValue();
  195.                             rowBuff += "#";
  196.  
  197.                             if (evaluator.evaluate(rowIte.getCell(9 )).getCellType() == Cell.CELL_TYPE_NUMERIC)
  198.                                 rowBuff += String.format("%.0f", evaluator.evaluate(rowIte.getCell(9 )).getNumberValue());
  199.                             else
  200.                                 rowBuff += evaluator.evaluate(rowIte.getCell(9 )).getStringValue();
  201.                             rowBuff += "#";
  202.  
  203.                             int napomena;
  204.  
  205.                             if (imaNapomenu) napomena = 1;
  206.                             else napomena = 0;
  207.  
  208.                             if(evaluator.evaluate(rowIte.getCell(12+napomena))!= null) {
  209.                                 if (evaluator.evaluate(rowIte.getCell(12 + napomena)).getCellType() == Cell.CELL_TYPE_NUMERIC)
  210.                                     rowBuff += String.format("%.0f", evaluator.evaluate(rowIte.getCell(12 + napomena)).getNumberValue());
  211.                                 else
  212.                                     rowBuff += evaluator.evaluate(rowIte.getCell(12 + napomena)).getStringValue();
  213.                             }
  214.                             rowBuff += "#";
  215.  
  216.  
  217.                             if (rowIte.getCell(13 + napomena ) != null && evaluator.evaluate(rowIte.getCell(13 + napomena )).getCellType() == Cell.CELL_TYPE_NUMERIC) {
  218.                                 double n = evaluator.evaluate(rowIte.getCell(13 + napomena )).getNumberValue();
  219.                                 String s = String.format("%.0f", n);
  220.                                 int length = s.length();
  221.                                 for (int i = 0; i < (12 - length); i++) {
  222.                                     s += "0";
  223.                                 }
  224.                                 rowBuff += s;
  225.                             } else if (rowIte.getCell(13 + napomena ) != null) {
  226.                                 String s = evaluator.evaluate(rowIte.getCell(13 + napomena )).getStringValue();
  227.                                 int length = s.length();
  228.                                 for (int i = 0; i < (12 - length); i++) {
  229.                                     s += "0";
  230.                                 }
  231.                                 rowBuff += s;
  232.                             } else rowBuff += "";
  233.  
  234.                             rowBuff += "#";
  235.                             if (evaluator.evaluate(rowIte.getCell(14 + napomena)) != null) {
  236.                                 if (evaluator.evaluate(rowIte.getCell(14 + napomena)).getCellType() == Cell.CELL_TYPE_NUMERIC)
  237.                                     rowBuff += String.format("%.0f", evaluator.evaluate(rowIte.getCell(14 + napomena)).getNumberValue());
  238.                                 else if (rowIte.getCell(14 + napomena) != null)
  239.                                     rowBuff += evaluator.evaluate(rowIte.getCell(14 + napomena)).getStringValue();
  240.                             }
  241.                             else
  242.                                 System.err.println("\nGRESKA:Neispravna celija u 14. koloni " + rowIte.getRowNum() + ". reda");
  243.  
  244.                         }
  245.                     } catch (NullPointerException e) {
  246.                         e.printStackTrace();
  247.                         System.err.println(f.getAbsolutePath());
  248.                     }
  249.                     if (!rowBuff.isEmpty() && !rowBuff.contains("####")) {
  250.                         printToFile(ID + "#" + rowBuff, true);
  251.                         System.out.print("\n" + ID + "#" + rowBuff);
  252.                         // System.out.println(Folder1 + " ~ " + Fajl1 + " VAL");
  253.                         // Bila Metoda
  254.                         //metoda("", "", rowBuff);
  255.                     }
  256.                 } else {
  257.                     try {
  258.                         if (rowIte.getRowNum() >= gornjaGranica) {
  259.                             if (rowIte.getCell(1).getCellType() == Cell.CELL_TYPE_BLANK)
  260.                                 return;
  261.                             if (rowIte.getCell(1).getCellType() == Cell.CELL_TYPE_STRING) {
  262.                                 if (rowIte.getCell(1).getStringCellValue().contains("blank"))
  263.                                     return;
  264.                             }
  265.                             if (rowIte.getCell(3).getCellType() == Cell.CELL_TYPE_STRING)
  266.                                 rowBuff += rowIte.getCell(3).getStringCellValue();
  267.                             else if (rowIte.getCell(3).getCellType() == Cell.CELL_TYPE_NUMERIC)
  268.                                 rowBuff += rowIte.getCell(3).getNumericCellValue();
  269.                             rowBuff += "#";
  270.                             if (rowIte.getCell(4).getCellType() == Cell.CELL_TYPE_STRING)
  271.                                 rowBuff += rowIte.getCell(4).getStringCellValue();
  272.                             else if (rowIte.getCell(4).getCellType() == Cell.CELL_TYPE_NUMERIC)
  273.                                 rowBuff += rowIte.getCell(4).getNumericCellValue();
  274.                             rowBuff += "#";
  275.                             if (rowIte.getCell(6).getCellType() == Cell.CELL_TYPE_STRING)
  276.                                 rowBuff += rowIte.getCell(6).getStringCellValue();
  277.                             else if (rowIte.getCell(6).getCellType() == Cell.CELL_TYPE_NUMERIC)
  278.                                 rowBuff += rowIte.getCell(6).getNumericCellValue();
  279.                             rowBuff += "#";
  280.  
  281.                             if (rowIte.getCell(11).getCellType() == Cell.CELL_TYPE_NUMERIC)
  282.                                 rowBuff += String.format("%.0f", rowIte.getCell(11).getNumericCellValue());
  283.                             else
  284.                                 rowBuff += rowIte.getCell(11).getStringCellValue();
  285.                             rowBuff += "#";
  286. //                            3266
  287. //                            if (rowIte.getCell(12  ) != null && rowIte.getCell(12  ).getCellType() == Cell.CELL_TYPE_NUMERIC)
  288. //                                rowBuff += String.format("%.0f", rowIte.getCell(12  ).getNumericCellValue());
  289. //                            else if (rowIte.getCell(12  ) != null)
  290. //                                rowBuff += rowIte.getCell(12  ).getStringCellValue();
  291. //                            rowBuff += "#";
  292.  
  293.  
  294.                             if (rowIte.getCell(8) != null && evaluator.evaluate(rowIte.getCell(8)).getCellType() == Cell.CELL_TYPE_NUMERIC) {
  295.                                 double n = evaluator.evaluate(rowIte.getCell(8)).getNumberValue();
  296.                                 String s = String.format("%.0f", n);
  297.                                 int length = s.length();
  298.                                 for (int i = 0; i < (12 - length); i++) {
  299.                                     s += "0";
  300.                                 }
  301.                                 rowBuff += s;
  302.                             } else if (rowIte.getCell(8) != null) {
  303.                                 String s = evaluator.evaluate(rowIte.getCell(8)).getStringValue();
  304.                                 int length = s.length();
  305.                                 for (int i = 0; i < (12 - length); i++) {
  306.                                     s += "0";
  307.                                 }
  308.                                 rowBuff += s;
  309.                             } else rowBuff += "";
  310.  
  311. //                            rowBuff += "#";
  312. //
  313. //                            if (rowIte.getCell(14  ) != null && evaluator.evaluate(rowIte.getCell(14  )).getCellType() == Cell.CELL_TYPE_NUMERIC)
  314. //                                rowBuff += String.format("%.0f", evaluator.evaluate(rowIte.getCell(14  )).getNumberValue());
  315. //                            else if (rowIte.getCell(14  ) != null)
  316. //                                rowBuff += evaluator.evaluate(rowIte.getCell(14  )).getStringValue();
  317. //
  318. //                            else
  319. //                                System.err.println("\nGRESKA:Neispravna celija u 14. koloni " + rowIte.getRowNum() + ". reda");
  320.  
  321.                         }
  322.                     } catch (NullPointerException e) {
  323.                         e.printStackTrace();
  324.                         System.err.println(f.getAbsolutePath());
  325.                     }
  326.                     if (!rowBuff.isEmpty() && !rowBuff.contains("####")) {
  327.                         printToFile(ID + "#" + rowBuff, true);
  328.                         System.out.print("\n" + ID + "#" + rowBuff);
  329.                         // System.out.println(Folder1 + " ~ " + Fajl1 + " VAL");
  330.                         // Bila Metoda
  331.                         //metoda("", "", rowBuff);
  332.                     }
  333.  
  334.                 }
  335.             }
  336.             } catch(Exception e){
  337.                 e.printStackTrace();
  338.                 System.err.println(f.getAbsolutePath());
  339.             }
  340.     }
  341.  
  342.     private static void handleOther(Workbook workbook, File f, int ID){
  343.         int gornjaGranica = 4;
  344.  
  345.         try {
  346.             Sheet sheet = workbook.getSheetAt(0);
  347.             Iterator<Row> rowIterator = sheet.iterator();
  348.  
  349.             String tmp = "";
  350.             while (rowIterator.hasNext()) {
  351.  
  352.                 Row rowIte = rowIterator.next();
  353.                 String rowBuff = "";
  354.  
  355.                 try {
  356.                     if (rowIte.getRowNum() >= gornjaGranica) {
  357.                         if(rowIte.getCell(0) == null)
  358.                             continue;
  359.                         if(rowIte.getCell(0).getCellType() == Cell.CELL_TYPE_STRING){
  360.                             if(rowIte.getCell(0).getStringCellValue().contains("ID"))
  361.                                 continue;
  362.                             if(rowIte.getCell(0).getStringCellValue().contains("Codice"))
  363.                                 continue;
  364.                             if(rowIte.getCell(0).getStringCellValue().contains("Deposito")){
  365.                                 tmp = rowIte.getCell(1).getStringCellValue().trim();
  366.                                 continue;
  367.                             }
  368.                         }
  369.                         if(rowIte.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC)
  370.                             rowBuff += String.format("%.0f",rowIte.getCell(0).getNumericCellValue());
  371.                         else
  372.                             rowBuff +=rowIte.getCell(0).getStringCellValue();
  373.                         rowBuff += "#";
  374.                         rowBuff += tmp;
  375.                         rowBuff += "#";
  376.                         if(rowIte.getCell(11) != null &&rowIte.getCell(11).getCellType()!=Cell.CELL_TYPE_BLANK){
  377.                             if(rowIte.getCell(11).getCellType() == Cell.CELL_TYPE_NUMERIC)
  378.                                 rowBuff += String.format("%d",((int) rowIte.getCell(11).getNumericCellValue()));
  379.                             else if (rowIte.getCell(11).getCellType() == Cell.CELL_TYPE_STRING)
  380.                                 rowBuff += rowIte.getCell(11).getStringCellValue();
  381.                         }
  382.  
  383.                     }
  384.                 }catch (NullPointerException e) {
  385.                     e.printStackTrace();
  386.                     System.err.println(f.getAbsolutePath());
  387.                 }
  388.                 if(!rowBuff.isEmpty() && !rowBuff.contains("####")) {
  389.                     printToFile(ID+"#"+rowBuff, false);
  390.                     System.out.print("\n" + ID+"#"+rowBuff);
  391.                 }
  392.             }
  393.         } catch (Exception e){
  394.             e.printStackTrace();
  395.             System.err.println(f.getAbsolutePath());
  396.         }
  397.     }
  398.  
  399.     private static void listFilesForFolder(String path, ArrayList<File> files) {
  400.         File directory = new File(path);
  401.         File[] flist = directory.listFiles();
  402.         if(flist!=null) {
  403.             for (final File file : flist) {
  404.                 if (file.isFile()) {
  405.                     files.add(file);
  406.                 } else if (file.isDirectory()) {
  407.                     listFilesForFolder(file.getPath(), files);
  408.                 }
  409.             }
  410.         }
  411.     }
  412.     // Stampanje u tekstualni fajl
  413.  
  414.     private static void printToFile(String xlsxBuff, boolean pl ) throws IOException {
  415.         String fileName;
  416.         if(pl)
  417.             fileName = "packlist.txt";
  418.         else fileName = "other.txt";
  419.         try {
  420.             PrintWriter outStream = new PrintWriter(new FileWriter(fileName, true));
  421.             outStream.println(xlsxBuff );
  422.             outStream.close();
  423.  
  424.         } catch (FileNotFoundException e) {
  425.             e.printStackTrace();
  426.         }
  427.     }
  428.  
  429.     private static void printID(String line) throws IOException{
  430.         String fileName = "packlistID.txt";
  431.         try {
  432.             PrintWriter outStream = new PrintWriter(new FileWriter(fileName, true));
  433.             outStream.println(line );
  434.             outStream.close();
  435.  
  436.         } catch (FileNotFoundException e) {
  437.             e.printStackTrace();
  438.         }
  439.     }
  440.  
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement