jsp生成excel文档 本应该到资料库去查这个问题的,但是每次我跑过来,总是说资料库在维护,没办法,只能请各位大哥帮帮我了,一个查询页面,查询出的结果,希望能提供对方一个excel表下载.请问,怎么生成excel表?最好能给个代码,这两天朋友介绍了我几个工具,都不是很理想 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用jxcell吧,纯java的excel报表组件,它里面有示例代码,参考它你可以迅速作出功能强大的excel报表下载连接:http://www.jxcell.net/jxcell.rar 如果不想用第三方库,可直接将查询结果输出为csv格式,或者是一个html table, contentType设置为excel格式即可 cheng_young(古道西风瘦马) 能给个例子吗 在jsp中用html语言生成excel是禁止的. 我是使用poi的,相对简单,刚处理过,以下是我的代码希望能帮到你<meta http-equiv="Content-Type" content="text/html; charset=gb2312" charset="GBK"><%@ page contentType="text/html; charset=GBK" %><%@ page import="java.sql.*,org.apache.poi.hssf.usermodel.*,java.io.*,java.util.*"%><%@ include file="db.jsp"%><%java.sql.Connection conn; //数据库连接对象java.sql.Statement stmt; //SQL语句对象java.sql.ResultSet res; //结果集对象String sqlText="";Random rnd=new Random();int x = rnd.nextInt(99999) + 1;String path="保存文件的路径"+ x +".xls";HSSFRow row;HSSFCell cell;int i=1;//数据库连接Class.forName("org.gjt.mm.mysql.Driver").newInstance();conn = java.sql.DriverManager.getConnection(url);stmt = conn.createStatement();res = stmt.executeQuery(sqlText);//生成EXCEL文件HSSFWorkbook wb = new HSSFWorkbook(); //建立新HSSFWorkbook对象FileOutputStream fileOut = new FileOutputStream(path);HSSFSheet sheet = wb.createSheet("sheet名"); //建立新的sheet对象row = sheet.createRow( (short) 0); cell = row.createCell((short) 0); cell.setCellValue("name1"); cell = row.createCell((short) 1); cell.setCellValue("name2"); cell = row.createCell((short) 2); cell.setCellValue("name3"); cell = row.createCell((short) 3); cell.setCellValue("name4"); cell = row.createCell((short) 4); cell.setCellValue("name5"); cell = row.createCell((short) 5); while (res.next()) { row = sheet.createRow( (short) (i++)); cell = row.createCell( (short) 0); cell.setCellValue(res.getString("called_number")); cell = row.createCell( (short) 1); cell.setCellValue(res.getString(2)); cell = row.createCell( (short) 2); cell.setCellValue(res.getInt(3)); cell = row.createCell( (short) 4); cell.setCellValue(res.getString(1)); cell = row.createCell( (short) 5); cell.setCellValue(res.getString(1));}wb.write(fileOut); fileOut.close();stmt.close();conn.close();%><html><body onunload="openwin()">请使用‘目标另存为’下载EXCEL文件<br><br><p align="center"><a href="xls/cdr<%=x%>.xls">CDR记录Excel文件</a></p></body></HTML> 到apache下载一个 POI 这个一个测试代码,供参考:package POI;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.*;import java.io.FileOutputStream;import java.io.IOException;public class SecondExcel { /** * @param args */ public static void main(String[] args)throws IOException { // TODO Auto-generated method stub HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象 HSSFSheet sheet = wb.createSheet("newsheet");//建立新的sheet对象 HSSFRow row = sheet.createRow((short)0);//建立新行 HSSFCell cell = row.createCell((short)0);//建立新cell cell.setCellValue(1);//设置cell的整数类型的值 row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值 row.createCell((short)2).setCellValue("only a test");//设置cell字符类型的值 row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值 HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式 //cellStyle.setDataFormat(HSSFDataFormat.getFormat("yyyy/MM/dd hh:mm:ss.ms EEE"));//设置cell样式为定制的日期格式 HSSFCell dCell =row.createCell((short)4); dCell.setCellValue(new java.util.Date());//设置cell为日期类型的值 dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式 HSSFCell csCell =row.createCell((short)5); csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断 csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串 row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell FileOutputStream fileOut = new FileOutputStream("D:\\Excel20060415.xls"); wb.write(fileOut); fileOut.close(); System.out.println(">>> D:\\Excel20060415.xls"); }} servlet中的示意性代码,csv格式(我机器上没有excel,没办法测试):response.setContentType("application/vnd.ms-excel");out = response.getPrintStream();out.println("a\tb\t\c");out.println("a\tb\t\c");out.println("a\tb\t\c");对html格式,你可以写一个简单的包含table的html网页将扩展名改为xls,使用excel打开即可看到效果 org.hibernate.hql.ast.QuerySyntaxException,请达人进来看看 java web中画有向图 覆盖导出工程的.class finereport 的 公用数据字典问题 JSP 滚动条 js当中innerHTML出现问题 jsf 中的配置问题 搞一两天,还不行,数据库的连接~~~~~~~~~~`在线等待 jsp能否直接用A标签实现下载? 如何设置缓存在jsp或是servlet上 jsp运行问题 空对象变空子符串在jsp里有什么方法:初学者
下载连接:http://www.jxcell.net/jxcell.rar
或者是一个html table, contentType设置为excel格式即可
能给个例子吗
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" charset="GBK">
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*,org.apache.poi.hssf.usermodel.*,java.io.*,java.util.*"%>
<%@ include file="db.jsp"%>
<%
java.sql.Connection conn; //数据库连接对象
java.sql.Statement stmt; //SQL语句对象
java.sql.ResultSet res; //结果集对象
String sqlText="";
Random rnd=new Random();
int x = rnd.nextInt(99999) + 1;
String path="保存文件的路径"+ x +".xls";
HSSFRow row;
HSSFCell cell;
int i=1;
//数据库连接
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = java.sql.DriverManager.getConnection(url);
stmt = conn.createStatement();
res = stmt.executeQuery(sqlText);
//生成EXCEL文件
HSSFWorkbook wb = new HSSFWorkbook(); //建立新HSSFWorkbook对象
FileOutputStream fileOut = new FileOutputStream(path);
HSSFSheet sheet = wb.createSheet("sheet名"); //建立新的sheet对象row = sheet.createRow( (short) 0);
cell = row.createCell((short) 0);
cell.setCellValue("name1");
cell = row.createCell((short) 1);
cell.setCellValue("name2");
cell = row.createCell((short) 2);
cell.setCellValue("name3");
cell = row.createCell((short) 3);
cell.setCellValue("name4");
cell = row.createCell((short) 4);
cell.setCellValue("name5");
cell = row.createCell((short) 5);
while (res.next()) {
row = sheet.createRow( (short) (i++));
cell = row.createCell( (short) 0);
cell.setCellValue(res.getString("called_number"));
cell = row.createCell( (short) 1);
cell.setCellValue(res.getString(2));
cell = row.createCell( (short) 2);
cell.setCellValue(res.getInt(3));
cell = row.createCell( (short) 4);
cell.setCellValue(res.getString(1));
cell = row.createCell( (short) 5);
cell.setCellValue(res.getString(1));
}
wb.write(fileOut);
fileOut.close();
stmt.close();
conn.close();
%><html>
<body onunload="openwin()">
请使用‘目标另存为’下载EXCEL文件<br><br>
<p align="center"><a href="xls/cdr<%=x%>.xls">CDR记录Excel文件</a></p>
</body>
</HTML>
package POI;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class SecondExcel {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("newsheet");//建立新的sheet对象
HSSFRow row = sheet.createRow((short)0);//建立新行
HSSFCell cell = row.createCell((short)0);//建立新cell
cell.setCellValue(1);//设置cell的整数类型的值
row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值 row.createCell((short)2).setCellValue("only a test");//设置cell字符类型的值 row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值 HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式 //cellStyle.setDataFormat(HSSFDataFormat.getFormat("yyyy/MM/dd hh:mm:ss.ms EEE"));//设置cell样式为定制的日期格式 HSSFCell dCell =row.createCell((short)4); dCell.setCellValue(new java.util.Date());//设置cell为日期类型的值 dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式 HSSFCell csCell =row.createCell((short)5); csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断 csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串 row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell FileOutputStream fileOut = new FileOutputStream("D:\\Excel20060415.xls"); wb.write(fileOut); fileOut.close();
System.out.println(">>> D:\\Excel20060415.xls");
}
}
response.setContentType("application/vnd.ms-excel");
out = response.getPrintStream();
out.println("a\tb\t\c");
out.println("a\tb\t\c");
out.println("a\tb\t\c");对html格式,你可以写一个简单的包含table的html网页
将扩展名改为xls,使用excel打开即可看到效果