使用jsp以表格格式显示csv文件数据

时间:2015-03-04 04:07:48

标签: java jsp csv

这是我的代码,

<table cellspacing='0' class="tablesorter" >
<thead><tr>
    <th>Name</th>   <th>Description</th>  <th>Instance-id</th> <th>Region</th> <th>Public_DNS</th>
    </tr></thead><tbody>
<% 
 String fName = "c://list_win_instances.csv";
 String thisLine; 
 int count=0; 
 FileInputStream fis = new FileInputStream(fName);
 DataInputStream myInput = new DataInputStream(fis);
 int i=0; 

while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
 {
if(i!=0) 
 {
out.print(" " +strar[j]+ " ");
 }
else
{
out.print(" <b>" +strar[j]+ "</b> ");
}
} 
out.println("<br>");
i++;
} 
 %>

我想以表格格式打印它的结果。我使用了表格分类器,但它以表格格式显示标题,但如何在此表格中打印csv的详细信息?

1 个答案:

答案 0 :(得分:2)

<table cellspacing='0' border="1" >
<% 
 String fName = "c://list_win_instances.csv";
 String thisLine; 
 int count=0; 
 FileInputStream fis = new FileInputStream(fName);
 DataInputStream myInput = new DataInputStream(fis);
 int i=0; 

while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
%><tr><%
for(int j=0;j<strar.length;j++)
 {
if(i!=0) 
 {
out.print("<td> " +strar[j]+ "</td> ");
 }
else
{
out.print(" <td> <b>" +strar[j]+ "</b> </td> ");
}
i++;
} 
%></tr><%
} 
 %>
</table>

尝试以上代码..