没有用于将数据导出到Excel工作表的HTML代码的JSP页面 [英] JSP page without HTML code for exporting data to Excel Sheet

查看:96
本文介绍了没有用于将数据导出到Excel工作表的HTML代码的JSP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将数据导出到Excel表格时遇到问题,这是因为我的团队中的其他开发人员提供了一些代码。所以主要的问题是使用JSP页面导出数据到Excel或.cvs,但不使用任何HTML代码。
任何建议也将帮助我在我发展中的舞台上探索。感谢您的努力。

解决方案

更好地使用Servlet。原始Java代码不属于JSP文件,这仅仅是维护麻烦



首先,创建一个简单的Java实用程序类,例如 List< List< T& > List< Data> (其中 Data 表示一行) CSV内容和 OutputStream 作为数据复制任务的方法参数和写入逻辑。



工作,创建一个Servlet类,它需要一些CSV文件标识符作为请求参数或pathinfo(我建议使用pathinfo作为一个由Redmond的团队开发的某个webbrowser将失败检测filename / mimetype否则),使用标识符获得从某处列出< List< T>> 列表< Data> ,并将其写入<$ c $ c> c> OutputStream c>



基本启动示例:

  public static< T& void writeCsv(List< List< T> csv,char separator,OutputStream output)throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output,UTF-8))
for(List< T> row:csv){
for(Iterator< T> iter = row.iterator(); iter.hasNext();){
String field = String。 valueOf(iter.next())。replace(\,\\);
if(field.indexOf(separator)> -1 || field.indexOf ')> -1){
field =''+ field +'';
}
writer.append(field);
if(iter.hasNext()){
writer.append(separator);
}
}
writer.newLine();
}
writer.flush();
}

下面是一个使用它的例子:

  public static void main(String [] args)throws IOException {
List< List< String> csv = new ArrayList< List< String>>();
csv.add(Arrays.asList(field1,field2,field3));
csv.add(Arrays.asList(field1,field2,fie \ld3));
csv.add(Arrays.asList(\field1 \\ ,,field2,,,\,\,\));
writeCsv(csv,';',System.out);
}

在Servlet中你基本上可以做:

  protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
String filename = request.getPathInfo();
List< List< Object>> csv = someDAO()。list();
response.setHeader(content-type,text / csv);
response.setHeader(content-disposition,attachment; filename = \+ filename +\);
writeCsv(csv,',',response.getOutputStream());
}

将此servlet映射到 / csv / 并调用它作为 http://example.com/context/csv/filename.csv 。这基本上都是。 pathinfo中的文件名很重要,因为Redmond团队开发的某个webbrowser忽略了 Content-Disposition filename c>标头,并改为使用网址的最后一个路径部分。


I am facing a problem in exporting my data to excel sheet, this is because of some code which other developers in my team made. So the main problem is to export the data to Excel or .cvs using JSP page but without using any HTML code. Any suggestion would also help me to explore in my developing arena. Thanks for your efforts.

解决方案

Better use a Servlet for this. Raw Java code doesn't belong in a JSP file, that's simply recipe for maintenance trouble.

To start, create a simple Java utility class which takes for example a List<List<T>> or a List<Data> (wherein Data represents one row) representing the CSV contents and an OutputStream as method arguments and write logic which does the data copying task.

Once you get that to work, create a Servlet class which takes some CSV file identifier as request parameter or pathinfo (I recommend using pathinfo as a certain webbrowser developed by a team in Redmond would fail on detection of filename/mimetype otherwise), uses the identifier to get the List<List<T>> or List<Data> from somewhere and writes it to the OutputStream of the HttpServletResponse along a set of correct response headers.

Here's a basic kickoff example:

public static <T> void writeCsv (List<List<T>> csv, char separator, OutputStream output) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
    for (List<T> row : csv) {
        for (Iterator<T> iter = row.iterator(); iter.hasNext();) {
            String field = String.valueOf(iter.next()).replace("\"", "\"\"");
            if (field.indexOf(separator) > -1 || field.indexOf('"') > -1) {
                field = '"' + field + '"';
            }
            writer.append(field);
            if (iter.hasNext()) {
                writer.append(separator);
            }
        }
        writer.newLine();
    }
    writer.flush();
}

Here's an example how you could use it:

public static void main(String[] args) throws IOException {
    List<List<String>> csv = new ArrayList<List<String>>();
    csv.add(Arrays.asList("field1", "field2", "field3"));
    csv.add(Arrays.asList("field1,", "field2", "fie\"ld3"));
    csv.add(Arrays.asList("\"field1\"", ",field2,", ",\",\",\""));
    writeCsv(csv, ';', System.out);
}

And inside a Servlet you can basically do:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getPathInfo();
    List<List<Object>> csv = someDAO().list();
    response.setHeader("content-type", "text/csv");
    response.setHeader("content-disposition", "attachment;filename=\"" + filename + "\"");
    writeCsv(csv, ',', response.getOutputStream());
}

Map this servlet on something like /csv/* and invoke it as something like http://example.com/context/csv/filename.csv. That's basically all. The filename in the pathinfo is important because a certain webbrowser developed by a team in Redmond ignores the filename part of the Content-Disposition header and uses the last path part of the URL instead.

这篇关于没有用于将数据导出到Excel工作表的HTML代码的JSP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆