Results 1 to 4 of 4
  1. #1
    ssikkander is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default Regarding EXCEL and POI API

    Hi,
    Hope everyone are fine. I am new to java servlet. My requirements is that
    have to upload excel through Flex and validate excel with database data. If there are error in the upload data then have to highlight those data and return an excel to user browser through java servlet. Here Is my code:

    package com.homedepot.mm.mf.cia.servlet;

    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.disk.DiskFileItemFac tory;
    import org.apache.commons.fileupload.servlet.ServletFileU pload;
    import org.apache.log4j.Logger;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import com.xx.mm.mf.cia.bo.StrategyBO;
    import com.xx.mm.mf.cia.dto.ImportStategyDTO;
    import com.xx.mm.mf.cia.excelgenerator.ExcelTemplateBean;
    import com.xx.mm.mf.cia.excelgenerator.StrategyExcelGener ator;
    import com.xx.mm.mf.cia.excelgenerator.StrategyExcelTempl ateGenerator;
    import com.xx.mm.mf.cia.resource.ImportStrategyResource;


    /**
    * Servlet implementation class ImportServlet
    */
    public class ImportServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final static Logger logger = Logger.getLogger(ImportServlet.class);
    private ImportStrategyResource resource = ImportStrategyResource.getInstance();
    private StrategyBO strategyBO = StrategyBO.getInstance();
    private ServletOutputStream result = null;


    public ImportServlet() {
    super();
    }

    @SuppressWarnings("unchecked")
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    DataInputStream in = null;
    String fileName = null;
    byte fileBytes[] = null;
    List<ImportStategyDTO> dtoList = null;

    try {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    if (item.isFormField()) {
    }else{
    fileName = item.getName();
    int formDataLength = (int) item.getSize();
    in = new DataInputStream(item.getInputStream());
    fileBytes = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    while (totalBytesRead < formDataLength) {
    byteRead = in.read(fileBytes, totalBytesRead,formDataLength);
    totalBytesRead += byteRead;
    }
    }
    }
    ByteArrayInputStream fs = new ByteArrayInputStream(fileBytes);
    try{
    dtoList = strategyBO.convertExcelDataDTO(fs,fileName);
    boolean isErrorsInDTOValidation = strategyBO.validateDTO(dtoList);
    if(!isErrorsInDTOValidation){
    //need to write logic to save DTO in the DB
    }else{
    List<ImportStategyDTO> importStategyDTOList = resource.getImportStategyDTOList();
    StrategyExcelTemplateGenerator templeGenerator = new StrategyExcelTemplateGenerator();
    ExcelTemplateBean excelTemplateBean = templeGenerator.generateExcelTemplate(importStateg yDTOList);
    StrategyExcelGenerator excelGenerator = new StrategyExcelGenerator();
    HSSFWorkbook workbook = excelGenerator.generateExcel(excelTemplateBean);

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition","attachment; filename="+fileName);
    result = response.getOutputStream();
    workbook.write(result);

    }
    }catch (Exception e) {
    logger.error(e.getMessage());
    }
    }

    }catch (Exception e) {
    logger.error(e.getMessage());
    }finally{
    if(in != null){
    in.close();
    }
    if(result != null)
    {
    result.flush();
    result.close();
    }


    }


    }

    }

    My expectation is taht after executed this dopost() method, In the browser, the user have option to open or save that excel file. But It did not happen. There is nothing. There is no error. No response.

    Could you please someone help me to solve this problem?

    Env;
    Flex
    Servlet
    DB2

    Thanks,
    Sidiq

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Welcome :)

    Put your code into code tags it is not clear :(

  3. #3
    ssikkander is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    package com.homedepot.mm.mf.cia.servlet;
    
    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.disk.DiskFileItemFac tory;
    import org.apache.commons.fileupload.servlet.ServletFileU pload;
    import org.apache.log4j.Logger;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    import com.xx.mm.mf.cia.bo.StrategyBO;
    import com.xx.mm.mf.cia.dto.ImportStategyDTO;
    import com.xx.mm.mf.cia.excelgenerator.ExcelTemplateBean;
    import com.xx.mm.mf.cia.excelgenerator.StrategyExcelGener ator;
    import com.xx.mm.mf.cia.excelgenerator.StrategyExcelTempl ateGenerator;
    import com.xx.mm.mf.cia.resource.ImportStrategyResource;
    
    
    /**
    * Servlet implementation class ImportServlet
    */
    public class ImportServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final static Logger logger = Logger.getLogger(ImportServlet.class); 
    private ImportStrategyResource resource = ImportStrategyResource.getInstance();
    private StrategyBO strategyBO = StrategyBO.getInstance(); 
    private ServletOutputStream result = null;
    
    
    public ImportServlet() {
    super(); 
    } 
    
    @SuppressWarnings("unchecked")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    {
         DataInputStream in = null;
         String fileName = null;
         byte fileBytes[] = null;
         List<ImportStategyDTO> dtoList = null;
    
        try { 
              boolean isMultipart = ServletFileUpload.isMultipartContent(request);
              if (isMultipart) { 
                 FileItemFactory factory = new DiskFileItemFactory();
                 ServletFileUpload upload = new ServletFileUpload(factory);
                 List<FileItem> items = upload.parseRequest(request); 
                 Iterator<FileItem> iter = items.iterator();
                 while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField()) 
                    { 
                    }else{
                     fileName = item.getName();
                     int formDataLength = (int) item.getSize(); 
                     in = new DataInputStream(item.getInputStream());
                     fileBytes = new byte[formDataLength];
                     int byteRead = 0;
                     int totalBytesRead = 0;
                    while (totalBytesRead < formDataLength) {
                            byteRead = in.read(fileBytes,  totalBytesRead,formDataLength);
                           totalBytesRead += byteRead;
                     } 
                  }
             } 
             ByteArrayInputStream fs = new ByteArrayInputStream(fileBytes); 
             try{
                 dtoList = strategyBO.convertExcelDataDTO(fs,fileName);
                 boolean isErrorsInDTOValidation = strategyBO.validateDTO(dtoList); 
                 if(!isErrorsInDTOValidation){ 
                           //need to write logic to save DTO in the DB 
                  }else{
                    List<ImportStategyDTO> importStategyDTOList = resource.getImportStategyDTOList();
                   StrategyExcelTemplateGenerator templeGenerator = new StrategyExcelTemplateGenerator();
                  ExcelTemplateBean excelTemplateBean = templeGenerator.generateExcelTemplate(importStateg yDTOList);
                 StrategyExcelGenerator excelGenerator = new StrategyExcelGenerator();
                HSSFWorkbook workbook = excelGenerator.generateExcel(excelTemplateBean);
    
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition","attachment; filename="+fileName); 
    result = response.getOutputStream();
    workbook.write(result); 
    
    }
    }catch (Exception e) {
    logger.error(e.getMessage());
    }
    }
    
    }catch (Exception e) {
    logger.error(e.getMessage()); 
    }finally{ 
    if(in != null){
    in.close();
    }
    if(result != null)
    { 
    result.flush();
    result.close();
    }
    
    }
    
    
    }
    
    }

  4. #4
    erdsingh is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: Regarding EXCEL and POI API

    please refer javabestinfo.com/excel-poi-utils.html to get understand excel apache poi utility methods

Similar Threads

  1. JSP To EXCEL
    By sureshsri1981 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-24-2009, 11:12 PM
  2. JSP and Excel
    By mohammedsiddig in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 09-25-2008, 12:03 PM
  3. Excel
    By little_polarbear in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 10:38 AM
  4. jsp excel
    By lak in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:53 AM
  5. ms excel
    By cool_gal in forum New To Java
    Replies: 9
    Last Post: 07-20-2007, 04:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •