繁体   English   中英

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

[英]How to print the values after reading a csv file?

我已经使用此类阅读了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' 我应该如何从清单中检索PFNO的值?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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