CSV to HTML tables

CSV to HTML tables

本文关键字:tables HTML to CSV      更新时间:2024-01-10

我很难将CSV文件显示为HTML。如何使用循环将csv文件中的下一行变成HTML表中的另一行?我还是这个jsp的新手。。。请给我一些想法让它发挥作用。

<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>

Q:您不也需要在每一新行之前打印<tr>,然后打印</tr>吗?

还有:

  1. 您的<table>元素太多,请删除第一个。

  2. 我认为<tbody>没有必要。。。但是如果你有它,你应该用</tbody>关闭它。

建议的改变(我实际上还没有尝试过…):

  <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>