2

I'm having trouble to display the CSV file into HTML. How can I make the next line in my csv file into another row in HTML table using loop? I'm still new to this jsp... Please give me some ideas to make it work.

<body>
        <% 
                String fName = "F:\\web\\Sales.csv";
                String thisLine; 
               int count=0; 
               FileInputStream fis = new FileInputStream(fName);
               DataInputStream myInput = new DataInputStream(fis);

         %>
        <table>
        <%
        out.print("<table border = 1><thead><tr><th>Customer</th><th>Customer Type</th><th>Purchase</th></tr></thead><tbody‌>");
        while ((thisLine = myInput.readLine()) != null){
        String strar[] = thisLine.split(";");
                  for(int j=0;j<strar.length;j++){
                                    out.print("<td>" +strar[j]+ "</td>");
                           } 
                  out.println("\n");
                  }
        %>
        </table>
    </body>
0

1 Answer 1

0

Q: Don't you also need to print <tr> before each new row, and </tr> after?

ALSO:

  1. You have one too many <table> elements - delete the first one.

  2. I don't think <tbody> is necessary ... but if you have it, you should close it with </tbody>.

SUGGESTED CHANGES (I haven't actually tried them...):

  <body>
    <table border = 1>
      <tr><th>Customer</th><th>Customer Type</th><th>Purchase</th></tr>
  <% 
     String fName = "F:\\web\\Sales.csv";
     String thisLine; 
     int count=0; 
     FileInputStream fis = new FileInputStream(fName);
     DataInputStream myInput = new DataInputStream(fis);

     while ((thisLine = myInput.readLine()) != null){
       String strar[] = thisLine.split(";");
       out.print("<tr>");
       for(int j=0;j<strar.length;j++){
         out.print("<td>" +strar[j]+ "</td>");
       } 
       out.println("</tr>");
     }
    %>
    </table>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the idea! I will try it! :) @paulsm4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.