MS2000使用MVC模式如何制作报表,Excel或XML导出
希望各位大虾能给予详细代码,不慎感激!!!
[解决办法]
[解决办法]
这是使用JXL,上面是使用POI导出Excel数据表
JXL导出Excel数据表
封装了一个简单易用、通用、动态的从数据库导出到Excel的方法,可以动态的指定导出那些列,显示什么名字,按什么顺序显示;支持本地文件存储和JSP/Servlet文件下载。
本方法包括两个类,Column是辅助类,Excel是主类:
/** * 用于Excel导出的辅助类,映射数据结果集(ResultSet)内列名的元数据和Excel内的显示列名 * Date: 2007-1-11 * Author: nescafe */public class Column { private int index; private String metaName; private String displayName; /** * 构造函数 * @param index 显示顺序,0 为显示的第一列 * @param meta 元列名,在ResultSet内的名字,必须大写 * @param display 显示列名,在Excel内的显示,可以是任何文字 */ public Column(int index, String meta, String display){ this.index = index; this.metaName = meta; this.displayName = display; } /** * 显示列名,在Excel内的显示,可以是任何文字 * @return */ public String getDisplayName() { return displayName; } /** * 显示顺序,0 为显示的第一列 * @return */ public int getIndex() { return index; } /** * 元列名,在ResultSet内的名字,必须大写 * @return */ public String getMetaName() { return metaName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public void setIndex(int index) { this.index = index; } public void setMetaName(String metaName) { this.metaName = metaName; }} /** * 简单的Excel操作,完成与数据库的动态导出 * Date: 2007-1-11 * Author: nescafe */public class Excel { /** * @param args */ public static void main(String[] args) { File f=new File("c:\\kk.xls"); try { f.createNewFile(); /*此处初始化一个数据库连接池,poolConnection可以用其他方式取得,不一定用pool*/ Connection conn = pool.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("Select t.*, t.rowid from mis2_personal_weekly_job_plan t Where Rownum Between 1 And 2"); HashMap map = new HashMap(); map.put("ID", new Column(0, "ID", "编号")); map.put("JOB_CONTENT", new Column(1, "JOB_CONTENT", "工作内容")); map.put("JOB_TARGET", new Column(2, "JOB_TARGET", "工作目标")); export(new FileOutputStream(f), null, rs, map); } catch (Exception e) { e.printStackTrace(); } } /** * 从数据库读数据,写入Excel * @param os 数据流,如果是写本地文件的话,可以是FileOutputStream; * 如果是写Web下载的话,可以是ServletOupputStream * @param title Excel工作簿的标题,如果不用的话,可以写null或者"" * @param rs 数据结果集 * @param map 数据结果集对应Excel表列名映射:key对应数据结果集的列名,必须是大写; * value,目前只能对应Column对象 * @throws Exception 方法内的父类异常有SQLException和IOException */ public static void export(OutputStream os, String title, ResultSet rs, Map map)throws Exception{ jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件 jxl.write.WritableSheet wsheet = wbook.createSheet("第一页", 0); // sheet名称 jxl.write.WritableFont wfont = null; // 字体 jxl.write.WritableCellFormat wcfFC = null; // 字体格式 jxl.write.Label wlabel = null; // Excel表格的Cell // 设置excel标题字体 wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); wcfFC = new jxl.write.WritableCellFormat(wfont); // 添加excel标题 jxl.write.Label wlabel1 = new jxl.write.Label(5, 0, title, wcfFC); wsheet.addCell(wlabel1); // 设置列名字体 // 如果有标题的话,要设置一下偏移 int offset = 2; if(title == null || title.trim().equals("")) offset = 0; else{ wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); wcfFC = new jxl.write.WritableCellFormat(wfont); } //根据原数据和map来创建Excel的列名 ResultSetMetaData rsmd = rs.getMetaData(); int count = rsmd.getColumnCount(); for(int i = 1; i <= count; i++){ String name = rsmd.getColumnName(i).toUpperCase(); if(map.containsKey(name)){ Column col = (Column)map.get(name); wlabel = new jxl.write.Label(col.getIndex(), offset, col.getDisplayName()); wsheet.addCell(wlabel); } } // 设置正文字体 wfont = new jxl.write.WritableFont(WritableFont.TIMES, 14, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); wcfFC = new jxl.write.WritableCellFormat(wfont); //往Excel输出数据 int rowIndex = 1 + offset; Collection array = map.values(); while(rs.next()){ Iterator it = array.iterator(); while(it.hasNext()){ Column col = (Column)it.next(); String value = rs.getString(col.getMetaName()); wlabel = new jxl.write.Label(col.getIndex(), rowIndex, value); wsheet.addCell(wlabel); } rowIndex++; } wbook.write(); // 写入文件 wbook.close(); os.flush(); os.close(); }}//JSP页面的下载如下,同样的代码也可以改成Servlet的<% ConnectionPool pool = ConnectionPool.getInstance(); Connection conn = null; ResultSet rs = null; Statement stmt = null; conn = pool.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("Select t.*, t.rowid from mis2_personal_weekly_job_plan t Where Rownum Between 1 And 2"); HashMap map = new HashMap(); map.put("ID", new Column(0, "ID", "编号")); map.put("JOB_CONTENT", new Column(1, "JOB_CONTENT", "工作内容")); map.put("JOB_TARGET", new Column(2, "JOB_TARGET", "工作目标")); String fileName = "周工作计划.xls"; response.reset(); response.setContentType("application/vnd.ms-excel"); //response.addHeader("Content-Disposition","attachment;filename=" + fileName); Excel.export(response.getOutputStream(), "", rs, map);%>
[解决办法]
我用过的,贴出来分享。需要有两个poi-3.0.1-FINAL-20070705.jar和poi-scratchpad-3.0.1-FINAL-20070705.jar放到lib下面
package com.hywebchina.rms;
import org.apache.poi.hssf.usermodel.*;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
public class ExcelCreate{
public byte[] createExcel(String[] userIds) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("excel导出时的默认名称");//建立新的sheet对象
for(int i=0;i<userIds.length;i++){
HSSFRow row = sheet.createRow(i);//建立新行
HSSFCell cell = row.createCell((short)0);//建立新cell
cell.setCellValue(userIds[i]);
}
ByteArrayOutputStream out=new ByteArrayOutputStream();
wb.write(out);
return out.toByteArray();
}
public static void main(String[] args)throws IOException
{
ExcelCreate ex=new ExcelCreate();
String ss[]={"1","2","3"};
for(int i=0;i<ss.length;i++){
// ex.createExcel(i,ss[i]);
System.out.println(ss[i]);
}
}
}
在页面点击”导出excel“按钮调用的servlet。需要在配置文件里配置一下
package com.hywebchina.rms;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ExcelServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 4588460160231054919L;
private static final Log log=LogFactory.getLog(ExcelServlet.class);
public void service(HttpServletRequest request,HttpServletResponse response){
try{
String[] userids=request.getParameterValues("checkboxlist");
ExcelCreate ec=new ExcelCreate();
byte[] result=ec.createExcel(userids);
response.reset();
String header="attachment;filename=userReport.xls";
response.addHeader("Content-Disposition",header);
response.setContentType("application/ms-excel");
response.setContentLength(result.length);
response.getOutputStream().write(result);
response.getOutputStream().flush();
response.getOutputStream().close();
}
catch(IOException e){
e.printStackTrace();
log.error("",e);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ExcelServlet es=new ExcelServlet();
}
}