如何在读取csv文件后打印值?

时间:2015-01-16 16:44:21

标签: java file csv

我已经使用这个类阅读了csv文件:

public class readCsvFile {

public ArrayList<Person> getData() throws FileNotFoundException 
{
    ArrayList<Person> arrlist = new ArrayList<Person>();

    Scanner f = new Scanner(new File("../uan_site1.csv")).useDelimiter(",");
    while (f.hasNext()) {
        arrlist.add(new Person(f.next(), f.next(), f.next(), f.nextLine()));
    }
    f.close();
    return arrlist;
}

}

存储数据的主要类:

public class Person 
{
    public final String pfno ,name ,empcode ,fhname;

    Person(String pfno, String name, String empcode, String fhname) 
   {
        super();
        this.pfno = pfno;
        this.name = name;
        this.empcode = empcode;
        this.fhname = fhname;
    }

    /* ...Getters starts here... */ 
            public String getpfno()
            {
                return pfno;
            }

            public String getname()
            {
                return name;
            }

            public String getempcode()
            {
                return empcode;
            }

            public String getfhname()
            {
                return fhname;
            }
             /* ...Getters ends here... */

}   /* ...End of class... */

输出csv:

内容的类
public class csv_print extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ByteArrayOutputStream bstream = new ByteArrayOutputStream();
        ServletOutputStream SOS = response.getOutputStream();
        StringBuilder StrBld = new StringBuilder();
        Document document = new Document(PageSize.A4, 40, 40, 45, 25);
        PdfWriter pdfWriter = null;
         ArrayList<Person> alist = new ArrayList<Person> ();

        try {
         String yname = null  ;     
     int intLogin=0;
     int check_user=0; 
     alist =(ArrayList) readCsvFile.getData();
     }

/////////////////////////////////
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment; filename= pf-" + mlogin + ".pdf");
 String path = getServletContext().getRealPath("");         


             /*****Printing module starts****/

            pdfWriter = PdfWriter.getInstance(document, bstream);
            document.open();

            HTMLWorker htmlWorker = new HTMLWorker(document);
            StrBld.append("<html>");
            StrBld.append("<body style='font-size:7px;font-family:Calibri;font-weight:normal'>");
            // StrBld.append("<table width='100' height='100' border='5' align='left' style='font-size:6px;font-family:Calibri;font-weight:normal'>");
            StrBld.append("<table  frame='box' style='text-align:left;padding-top:0'>");
            StrBld.append("<tr><td>");
            StrBld.append("<img src='"+path +"/img/jplogo.bmp' align='left'  width='55' height='55' />");
            StrBld.append("</td></tr>");
            StrBld.append("</table>");

            //Print file generation starts start
            StrBld.append("<table width='200' border='0' align='center' style='font-size:6px;font-family:Calibri;font-weight:normal'>");
            StrBld.append("<tr><td colspan='6'>");
            StrBld.append("<p>");
            StrBld.append("<strong><font face='Calibri' size='20px'>Hospital Trust</strong>")
            StrBld.append("New Delhi");
            StrBld.append("<br/>");
            StrBld.append("<br>");
            StrBld.append("-----------------------------");

            StrBld.append("</p>");
            StrBld.append("</td></tr>");

            StrBld.append("</table>"); // First table ends.

            StrBld.append("<table width='540' border='0' align='center' style='font-size:6px;font-family:Calibri;font-weight:normal'>"); // Second table starts.
            StrBld.append("<tr><td colspan='2' style='text-align:left;' ><font face='Calibri' size='10px'><b>PFNO : ").append(opfno).append(" </b></font></td>");

            StrBld.append("</table>");

            StrBld.append("</body>");
            StrBld.append("</html>");

            htmlWorker.parse(new StringReader(StrBld.toString()));

            document.close(); //document instance closed
            pdfWriter.close();
            bstream.writeTo(SOS);
            SOS.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            StrBld = null;
            System.gc();
        }
    } 
}

该文件的内容已被读入'alist'。我应该如何从alist中检索PFNO的值。

1 个答案:

答案 0 :(得分:0)

添加到Byron所说的内容中,你没有一个循环来添加ArrayList中的所有项目,但是你可能想要一个。同时你可以使用:

 (Person) alist.get(0).getpfno();