Skip to content
Advertisement

insert data stored in array list in excel using java

Its been a while i m trying to create a excel sheet to store the crawled data in a table format in a excel , the data is fetched from a url and stored in a array list , this data is needed to be stored in a array list `

import java.util.ArrayList;

import com.webscrap4j.WebScrap;
import com.webscrap4j.WebScrapException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFTable;


public class crawl 
{
    public static void main(String[] args) throws IOException {

        
    
        ArrayList<String> al = new ArrayList<String>();
        ArrayList<String> bl = new ArrayList<String>();
        ArrayList<String> cl = new ArrayList<String>();
        WebScrap ws = new WebScrap();
        
        ws.setUrl("https://www.pepperfry.com/hardware-electricals-power-storage-ups-inverters.html");
        try
        
        
        {
            
            ws.startWebScrap();
            //al = ws.getImageTagData("img", "title");
            al = ws.getSingleHTMLScriptData("<div class='card-body-title hidden-txt'>", "</div>");
            bl = ws.getSingleHTMLScriptData("<span class='strike'>", "</span>");
            cl = ws.getSingleHTMLScriptData("<p class='card-body-price txt-red'>", "</p>");
            
            
            
            
    /*      FileOutputStream fos=new FileOutputStream("/Users/parthpatil/Documents/11.xls");
            HSSFWorkbook workBook = new HSSFWorkbook();
            
            HSSFSheet Sheet = workBook.createSheet("products");
            //XSSFTable my_table = Sheet.createTable();
            HSSFRow row;
            HSSFCell cell;
            CreationHelper helper = workBook.getCreationHelper();
             
             
             Row header = Sheet.createRow(0);
             header.createCell(0).setCellValue("Product Name");
            header.createCell(1).setCellValue("Product Price");
             header.createCell(2).setCellValue("Product MRP");

            
        
              for(int i=0;i<al.size();i++){
                    row = Sheet.createRow((short) i);
                cell = row.createCell(i);
                System.out.println(al.get(i));
                cell.setCellValue(al.get(i).toString());
                }
                System.out.println("Done");
                workBook.write(fos);
                
                */
                
            for (String adata : al)
            {

                System.out.println("the product are:- " + adata);

            }
            for (String bdata : bl)
            {

                System.out.println("the MRp are:- " + bdata);

            }
            for (String cdata : cl)
            {

                System.out.println("the selling price is:- " + cdata);

            }
            
        } catch (WebScrapException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
          
}
    
    
}

Advertisement

Answer

Your code is globally correct it has only some little mistakes, here is how it could be done with your code:

// Use the try-with-resource statement to close all the resources properly
try (HSSFWorkbook workBook = new HSSFWorkbook();
     FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/11.xls")) {

    // Create the Sheet
    HSSFSheet Sheet = workBook.createSheet("products");

    // Create the first row corresponding to the header
    Row header = Sheet.createRow(0);
    header.createCell(0).setCellValue("Product Name");
    header.createCell(1).setCellValue("Product Price");
    header.createCell(2).setCellValue("Product MRP");

    // Ensure that all the List have the same size otherwise throw an exception
    if (al.size() != bl.size() || al.size() != cl.size())
        throw new IllegalStateException("Some data is missing");

    // Iterate over all the list an create the rows of data
    for(int i = 0; i < al.size(); i++){
        // Create the current starting from 1 to al.size()
        HSSFRow row = Sheet.createRow((short) i + 1);
        // Cell of the Product Name
        row.createCell(0).setCellValue(al.get(i));
        // Cell of the Product Price
        row.createCell(1).setCellValue(cl.get(i));
        // Cell of the Product MRP
        row.createCell(2).setCellValue(bl.get(i));
    }
    // Write the result into the file
    workBook.write(fos);
}
Advertisement