简体   繁体   中英

Reading excel to list of Hashtable in Java

I have a code to read an excel file and place it in a list Hash tables. The code that I have is:

  package tests;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.List;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    public class ReadExcelTest {
        public static Hashtable htable=new Hashtable(); 
        public static List<Hashtable<String,String>> exceldata  = new ArrayList<Hashtable<String,String>>();
        public static void main() throws IOException{
            readexcel(System.getProperty("user.dir")+"//src//config//TestData.xls");
        }
    @Test
        public static void readexcel() throws IOException  
        {
        String inputFile=System.getProperty("user.dir")+"//src//config//TestData.xls";
            File inputWorkbook = new File(inputFile);
            Workbook w;
            Cell firstrowelement = null;
            try 
            {
                w = Workbook.getWorkbook(inputWorkbook);
                Sheet sheet = w.getSheet(0);
                for (int j = 0; j <sheet.getRows(); j++) 
                {
                    for (int i = 0; i < sheet.getColumns(); i++) 
                    {
                        firstrowelement = sheet.getCell(i, 0);
                        Cell cell = sheet.getCell(i, j);
    htable.put(firstrowelement.getContents(),cell.getContents());
                        System.out.print(firstrowelement.getContents()+"->"+cell.getContents());
                    }
                    System.out.println(firstrowelement.getContents());
                    exceldata.add(j,htable);
                }
    //printing the list 
                for(Hashtable hash :exceldata)
                {
                    Enumeration e = hash.keys();
                    while (e.hasMoreElements()) {
                      String key = (String) e.nextElement();
                      System.out.println(key + " : " + hash.get(key));
                    }
                }

           } 
            catch (BiffException e) 
            {
                e.printStackTrace();
            }
        }
    }

The output I am getting is all the hash tables in the list are displaying the same data instead of displaying each rows data.

Not exactly sure how to over come this issue. Any help is much appreciated.

You need to make a new htable for every row.

As it is, you keep re-using and updating a single HashTable.

(btw. prefer HashMap to HashTable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM