java将查询数据导出成csv文件的问题
用java将查询出来的数据在后台生成.csv文件,转到前台的时候提示用户下载,具体应该怎么实现呢?小弟初学java,希望大虾们能不吝指教~只有这么点分了.....
用java将查询出来的数据在后台生成.csv文件,转到前台的时候提示用户下载,具体应该怎么实现呢?小弟初学java,希望大虾们能不吝指教~只有这么点分了..
展开
2个回答
展开全部
解释:csv文件实际上就是字符串,之间用“,”进行分割,之后进行的存储。
工具类如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
/**
*
* CSV文件导出工具类
*/
public class CSVUtils {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
}
工具类如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
/**
*
* CSV文件导出工具类
*/
public class CSVUtils {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
}
展开全部
生成.csv文件有第三方包javacsv.jar,例子网上找,很简单
提示用户下载,用流实现,网络上更多
提供个JSP的例子给你。
<%@ page language="java" pageEncoding="UTF-8"%>
<%
// example:
// <a href="download.jsp?p=img/test.gif">download image</a>
String path = request.getParameter("p");
String name = request.getParameter("name");
String root = getServletContext().getRealPath(path);
if (name == null) {
int index = path.lastIndexOf("/");
if (index >= 0) {
name = path.substring(index + 1);
} else {
name = path;
}
}
response.setContentType("unknown");
response.addHeader("content-disposition", "filename=\"" + name + "\"");
java.io.OutputStream os = response.getOutputStream();
try {
java.io.FileInputStream fis = new java.io.FileInputStream(root);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 ) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
out.clear();
out = pageContext.pushBody();
%>
提示用户下载,用流实现,网络上更多
提供个JSP的例子给你。
<%@ page language="java" pageEncoding="UTF-8"%>
<%
// example:
// <a href="download.jsp?p=img/test.gif">download image</a>
String path = request.getParameter("p");
String name = request.getParameter("name");
String root = getServletContext().getRealPath(path);
if (name == null) {
int index = path.lastIndexOf("/");
if (index >= 0) {
name = path.substring(index + 1);
} else {
name = path;
}
}
response.setContentType("unknown");
response.addHeader("content-disposition", "filename=\"" + name + "\"");
java.io.OutputStream os = response.getOutputStream();
try {
java.io.FileInputStream fis = new java.io.FileInputStream(root);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 ) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
out.clear();
out = pageContext.pushBody();
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询