传递servlet值列表

时间:2014-04-15 14:06:26

标签: java servlets

使用apache poi,我正在读取像这样的的excel文件的第一行值

try
{
FileInputStream file = new FileInputStream(uploadedFile);
XSSFWorkbook workbook = new XSSFWorkbook(file);
for (int i =0; i < workbook.getNumberOfSheets(); i++)
{
   XSSFSheet sheet = workbook.getSheetAt(i);
   Iterator<Row> rowIterator = sheet.iterator();

   String SheetName = "<span class='blue'><b>" +sheet.getSheetName()+ "<b></span><br>";
       request.setAttribute("SheetName", SheetName);
   Row row = rowIterator.next();
      if(row.getRowNum() == 0) 
    { 
       Iterator<Cell> cellIterator = row.cellIterator();
       while(cellIterator.hasNext()) 
         {
        Cell cell1 = cellIterator.next();

        switch(cell1.getCellType()) 
           {
             case Cell.CELL_TYPE_STRING:
             String strval = cell1.getStringCellValue();
             request.setAttribute("Values2", strval);
             break;
           }
                      }
             }
        }
            file.close();
    }catch(NoSuchElementException e)
    {}

现在,我想传递一个值列表strval只发送一个值,我该如何发送多个值? 如何将一系列项目发送到我的jsp页面?

1 个答案:

答案 0 :(得分:1)

您可以将List作为属性值发送。例如:

List<String> cellValues = new ArrayList<String>();
while(cellIterator.hasNext()) 
     {
    Cell cell1 = cellIterator.next();

    switch(cell1.getCellType()) 
       {
         case Cell.CELL_TYPE_STRING:
         String strval = cell1.getStringCellValue();
         cellValues.add(strval);
         break;
       }
                  }
         }
    }
request.settAttribute("Values2", cellValues);