web导出csv java,如何使用Java导出CSV格式的数据?

The reason may be the the short of knowledge in java :( ,so I'm asking this question,

Here in this piece of code I'm getting dynamic value(from a jsp page) :

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/apps","root","root");

Statement stmt = con.createStatement();

String sql = "select * from info;";

ResultSet rs = stmt.executeQuery(sql);

System.out.println(sql);

System.out.println("hi Tirtha");

%>

Information of User's

User NameEmail Id

Now I want to save this data in a csv file(having an export option in it).

Any inputs will be appreciated.

解决方案

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

Export CSV

but please note that you have to link the servlet to the url using web.xml:

UsersServlet

__package__.UsersServlet

UsersServlet

getCSV

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 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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值