java web app 导出csv excel 文件
2014-07-19 09:05
369 查看
here is a class you can using to export to CSV:
Working the MVC way
Here is how your code should be written:
Let's say you have a class called. User.java inside of which there is a static function
called get all users
Then let's say you have a servlet called UsersServlet which get these users:
in your jsp, for example, you will have a simple anchor tag which calls the servlet (the servlets calls User.java, get data, forms them into a CSV and then outputs it to the browser...). Something like this would work:
but please note that you have to link the servlet to the url using web.xml:
EDIT: Writing to disk instead of sending to browser
here is a class you can using to export to CSV:
import java.io.FileWriter; import java.io.IOException; import User; public class GenerateCsv { private static void generateCsvFile(ArrayList<User> users) { String output = "Email, Name\n"; for (User user in users) { output += user.getEmail() + ", " + user.getName() + "\n"; } return output; } }
Working the MVC way
Here is how your code should be written:
Let's say you have a class called. User.java inside of which there is a static function
called get all users
public class User { String name; String email; public static ArrayList<User> getAllUsers() { // returns all users ... } }
Then let's say you have a servlet called UsersServlet which get these users:
import javax.servlet.*; import javax.servlet.http.*; public class UsersServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("application/csv"); PrintWriter w = res.getWriter(); ArrayList<User> users = Users.getAllUsers(); w.prinln(GenerateCsv.generateCsvFile(users)); w.flush(); w.close(); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ... } }
in your jsp, for example, you will have a simple anchor tag which calls the servlet (the servlets calls User.java, get data, forms them into a CSV and then outputs it to the browser...). Something like this would work:
<a href='/getCSV' > Export CSV </a>
but please note that you have to link the servlet to the url using web.xml:
<web-app> <servlet> <servlet-name>UsersServlet</servlet-name> <servlet-class>__package__.UsersServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UsersServlet</servlet-name> <url-pattern>getCSV</url-pattern> </servlet-mapping> </web-app>
EDIT: Writing to disk instead of sending to browser
import java.io.FileWriter; import java.io.IOException; import User; public class GenerateCsv { private static void generateCsvFile(String fileName, ArrayList<User> users) { try { FileWriter writer = new FileWriter(fileName); writer.append("Email"); writer.append(','); writer.append("Name"); writer.append('\n'); for (User user in users) { writer.append(user.getEmail()); writer.append(','); writer.append(user.getName()); writer.append('\n'); } writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } }
相关文章推荐
- php数据导入导出之excel(csv文件)
- php数据导入导出之excel(csv文件)
- 导出excel、word、csv文件方法汇总
- 用excel打开从系统导出的csv文件时未分列
- mysql导出csv文件excel打开后数字用科学计数法显示且低位变0的解决方法
- php数据导入导出之excel(csv文件)
- datagrid数据导出到excel文件给客户端下载的几种方法 (xls,csv,html)
- PHP导出MySQL数据到Excel或者CSV文件
- DataTable导出到Excel的解决方案(.csv)文件
- 导出excel、word、csv文件方法汇总
- C#导出数据到CSV和EXCEL文件时,过长的数值被转义的解决方法
- DataTable 对xml、excel、csv 对文件的导入和导出功能
- Excel打开从PL/SQL Deleveloper导出的csv文件乱码问题
- 数据库中导出CSV文件与EXCEL文件数据对比可能遇到的问题与解决方法
- datagrid数据导出到excel文件给客户端下载的几种方法 (xls,csv,html)
- BS系统导出csv文件用excel打开的乱码问题
- C#导出数据到CSV和EXCEL文件时,过长的数值被转义的解决方法
- silverlight开发:将datagrid中的数据导出为Excel和CSV文件
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- 文件、导出-在Mac系统下Excel转csv文件中文乱码问题解决-by小雨