csv 导出Excel
Posted TT_DUL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csv 导出Excel相关的知识,希望对你有一定的参考价值。
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.servlet.http.HttpServletResponse;
public class A
public void exportToCSV(HttpServletResponse response)
response.setContentType("application/csv;charset=gb18030");
response.setHeader("Content-Disposition","attachment; filename=\\"" + "数据导出" + ".csv" + "\\"");
PrintWriter print = null;
print = response.getWriter();
StringBuilder buffer = new StringBuilder(500);
Connection conn = getConnection();
PreparedStatement st =null;
ResultSet rs=null;
buffer.setLength(0);
buffer.append("select id,name,password from users");
try
st = conn.prepareStatement(buffer.toString());
rs=st.executeQuery();
ResultSetMetaData rsMetaData = rs.getMetaData();
int colCou = rsMetaData.getColumnCount(); //计算查询出来结果的列数,本次查询查出3列
StringBuffer linedata = new StringBuffer();
int rscount = 0;
linedata.append("ID");
linedata.append(",");
linedata.append("用户名称");
linedata.append(",");
linedata.append("用户密码");
linedata.append("\\n");
while (rs.next())
rscount++;
for (int i = 1; i <= colCou; i++)
linedata.append(rs.getString(i));
if (i != colCou)
linedata.append(",");
linedata.append("\\n");
if (rscount % 5000 == 0) //每5000条写一次
print.write(linedata.toString());
linedata.setLength(0);
if (linedata.length() > 0)
print.write(linedata.toString());
rs.close();
st.close();
catch (Exception ex)
ex.printStackTrace();
finally
try
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
if (print != null)
print.close();
catch (SQLException e)
e.printStackTrace();
return;
以上是关于csv 导出Excel的主要内容,如果未能解决你的问题,请参考以下文章
excel2007打开导出的.csv文件出现中文乱码,求高手指点