在GUI上显示csv文件

时间:2013-04-25 05:58:35

标签: jsp struts2 jboss

平台:JBOSS 5.x,Struts2,JSP 我在gui上显示一个csv文件。要这样做 1.首先我调用TestAction类的showFileData函数。 2. showFileData函数将转发控制转发给'Test.jsp'类。 我成功地能够在gui上显示csv文件。但如果行之间有空格,那么我就无法在gui上显示空格。

TestAction.java

package cdot.oss.cmsat.importMap.struts;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import org.apache.struts.util.LabelValueBean;
import com.opensymphony.xwork2.ActionContext;

public class TestAction 
{
public String showFileData() throws Exception 
{       
    Map session = (Map)ActionContext.getContext().getSession();

    String filePath=System.getProperty("jboss.server.home.dir")+File.separator+"x.csv";
    System.out.println("[][]filePath:*"+filePath);

    File drs = new File(filePath);
    ArrayList<String> fileRecords=new ArrayList();
    ArrayList<LabelValueBean> Recordnum=new ArrayList();

    try{

        if(drs.isFile())
        {
        FileInputStream fstream = new FileInputStream(filePath);

        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        int lines = 1;
        /*br.readLine() shall first line of file, and it shall assigned to strLine variable
         * first 10 lines of file shall be added to fileRecords arrayList*/
        //System.out.println("[][] br.readLine() "+br.readLine());
        while ((strLine = br.readLine()) != null)   
        {
            System.out.println("[][] strLine:"+strLine+":"+" length "+strLine.length());
            fileRecords.add(strLine);
            Recordnum.add(new LabelValueBean(Integer.toString(lines),Integer.toString(lines)));
            if(lines==10)
            {
                break;
            }
            lines++;
        }

        br.close();
        in.close();
        fstream.close();
        session.put("RecordNum",Recordnum);

        }
        else
            fileRecords.add("Not a valid file");
    }

    catch(Exception e){
        System.out.println("[][]exception");
        fileRecords.add("Not a valid file");

    }
    System.out.println("[][] fileRecords.size() "+fileRecords.size()+" fileRecords "+fileRecords);
    session.put("FileRecords",fileRecords);
    return "ForwardToJsp";
}

}

test.jsp的

<html>
<head>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:head />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:head />

<title>Test</title>
    </head>
    <body>
    <center>
    <br>
    <br></br>
    <div style="width: 1200px;height: auto;border: 2px solid steelblue">
    <s:form name="Test" enctype="multipart/form-data">
    <br></br>
    <s:if test="%{#session.FileRecords}">
    <table border="0" cellpadding="5" cellspacing="5" width="80%" class="input-table" >
    <tr>
    <td align="center">
    <b> First ten rows of file are :  </b>
    <br></br>
    </td>
    </tr>
    <tr>
    <td align="left">
    <div style="width: 950px;height: 100px;background-color: #FFFFFF;border: 1px solid steelblue;overflow: auto;">
    <dl>
    <s:iterator value="#session.FileRecords">
    <dt class="C_normal"><s:property/></dt>
    </s:iterator>
    </dl>
    </div>
    </td>
    </tr>
    </table>
    <br/>
    <br></br>
    </s:if>
    </s:form>
    </div>
    </center>
    </body>
    </html>

EG。 x.csv

a,b,c,d

e,f,g,h

i,j,k,l

m,n,o,p

q,r,s,t

u,v,w,x

y,z,ab,cd

如上面的文件中,行之间有空格,但是在gui上它们没有显示?我怎样才能准确显示前10行到gui?

0 个答案:

没有答案