怎么从jsp页面的表格导出到Excel
展开全部
无非是这样的一个过程。先通过数据库查询出数据,放到List里,然后把这个List发往页面,然后遍历这个List把数据显示到这个表格里。 要想把数据导出到execel,很简单,把页配做面接受的这个List用jxl写到Execel就行了。具体将List导出到Execel的类如下:
package cms.dao;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受两个参数,一个是list,这个地方我用了泛型。另一个参数是HttpServletResponse response
public static void toExcelBy(List<AccessLog> list,HttpServletResponse response) {
// 创建工作表
WritableWorkbook book=null;
response.reset();
// 创建工滚卖旦作流
OutputStream os =null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
// 设置工作表的标题
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//设置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下大扰是我做的导出日志的一个模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("访问日志", 0);
// 生成名为"商品信息"的工作表,参数0表示这是第一页
int nI = 1;
// 表字段名
sheet.addCell(new jxl.write.Label(0, 0, "日志编号"));
sheet.addCell(new jxl.write.Label(1, 0, "用户ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用户姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "访问日期"));
sheet.addCell(new jxl.write.Label(4, 0, "访问时间"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名称"));
sheet.addCell(new jxl.write.Label(7, 0, "创建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 将数据追加
for(int i=1;i<list.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大体思路就是这样的,别忘了在你的项目中导入JXL必要的jar包,这个包叫jxl.jar,你可以下载一个。希望能帮到你,欢迎追问。望采纳!
package cms.dao;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受两个参数,一个是list,这个地方我用了泛型。另一个参数是HttpServletResponse response
public static void toExcelBy(List<AccessLog> list,HttpServletResponse response) {
// 创建工作表
WritableWorkbook book=null;
response.reset();
// 创建工滚卖旦作流
OutputStream os =null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
// 设置工作表的标题
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//设置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下大扰是我做的导出日志的一个模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("访问日志", 0);
// 生成名为"商品信息"的工作表,参数0表示这是第一页
int nI = 1;
// 表字段名
sheet.addCell(new jxl.write.Label(0, 0, "日志编号"));
sheet.addCell(new jxl.write.Label(1, 0, "用户ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用户姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "访问日期"));
sheet.addCell(new jxl.write.Label(4, 0, "访问时间"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名称"));
sheet.addCell(new jxl.write.Label(7, 0, "创建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 将数据追加
for(int i=1;i<list.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大体思路就是这样的,别忘了在你的项目中导入JXL必要的jar包,这个包叫jxl.jar,你可以下载一个。希望能帮到你,欢迎追问。望采纳!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询