Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package servlets; import DB.AccountingDB; import DB.InventoryDB; import DB.ReportDB; import DB.SalesDB; import Entities.Accounting; import Entities.Inventory; import Entities.Report; import Entities.Sales; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.sql.Blob; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import static org.apache.poi.hssf.usermodel.HeaderFooter.file; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; /** * * @author Klvn Flrs */ @MultipartConfig(maxFileSize = 16177215) public class UploadAccountingServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TEST"); InputStream inputStream = null; Part filePart = request.getPart("file"); inputStream = filePart.getInputStream(); POIFSFileSystem fs = new POIFSFileSystem(inputStream); // FileInputStream fichier = new FileInputStream(new File("countries.xls")); //create workbook instance that refers to xlsx file HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); ArrayList<Accounting> arrAccounting = new ArrayList<>(); ReportDB reportDB = new ReportDB(); reportDB.addReport(new Report("Accounting", "Pending")); ArrayList<Report> reportList = new ArrayList(); reportList = reportDB.getAllReports(); int ReportRef = reportList.size(); for (Row ligne : sheet) {//iterate rows Accounting accounting = new Accounting(); for (Cell cell : ligne) {//iterate columns //cell type /* Sales sales; (lagay sa sys out) sales.setSalesID(cell.getNumericCellValue()); --- WALA TO */ switch (formulaEvaluator.evaluateInCell(cell).getCellType()) { case Cell.CELL_TYPE_NUMERIC: // sales.setName(cell.getNumericCellValue()); System.out.print("cell 1 " + cell.getNumericCellValue() + " \t"); if (cell.getColumnIndex() == 1) { accounting.setPoNo((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 4) { accounting.setAgingDays((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 5) { accounting.setCustomerID((int) cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print("cell 2 " + cell.getStringCellValue() + " \t"); if (cell.getColumnIndex() == 0) { accounting.setStartDate(cell.getStringCellValue()); } if (cell.getColumnIndex() == 2) { accounting.setTerms(cell.getStringCellValue()); } if (cell.getColumnIndex() == 3) { accounting.setDueDate(cell.getStringCellValue()); } break; } } accounting.setReportRef(ReportRef); arrAccounting.add(accounting); System.out.println(); } request.setAttribute("arrAccounting", arrAccounting); AccountingDB accountingDB = new AccountingDB(); request.setAttribute("arrAccounting", arrAccounting); for (int j = 0; j < arrAccounting.size(); j++) { accountingDB.addAccounting(arrAccounting.get(j)); } ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher("/isrAfteruploadAccounting.jsp"); rd.forward(request, response); processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }