Struts2框架中 怎么把从数据库中读取的数据生成文件,并在客户端保存
2014-03-05 23:11
260 查看
TestXL.jsp <%@page contentType="text/html; charset=GBK"%> <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <html:html> <html:button property="button" onclick="printAll()">DownLoad</html:button> </html:html> <script language='javascript' type="text/javascript"> function printAll(){ location.href="<%=request.getContextPath()%>/downloadAction.do"; } </script> ExcelBean: import java.io.*; import jxl.*; import jxl.write.*; import jxl.format.*; import java.util.*; import javax.servlet.http.HttpServletResponse; /* 用于Struts结构中的Excel文件写操作,当但击“导出按钮”时可以直接在客户端导出Excel文件并提示保存 使用方法: 在action类中加入: ExcelBean eb = new ExcelBean(); if(vect.size()>0){ eb.exportToExcel(response, fname, sheetname, titles, vect); //调用生成excel文件bean } 参数: response:action类中的HttpServletResponse; filename:要生成的保存的缺省的Excel文件名 sheetname:要保存的工作表的名称 titles:工作表中的表格横向标题 vect:数据行 */ public class ExcelBean { public ExcelBean() {} public boolean exportToExcel(HttpServletResponse response,String filename,String sheetname,String[] titles,Vector vect) throws Exception { OutputStream os = null; boolean b1 = true ; try { os = response.getOutputStream(); //取得输出流 response.reset(); //清空输出流 response.setHeader("Content-disposition","attachment; filename=" + filename + ".xls"); //设定输出文件头 response.setContentType("application/msexcel"); //定义输出类型 } catch (IOException ex) { b1= false; System.out.println("流操作错误:"+ex.getMessage()); } WritableWorkbook workbook = null; try { //创建新的Excel 工作簿 workbook = Workbook.createWorkbook(os); //在Excel工作簿中建一工作表,其名为:第一页 jxl.write.WritableSheet wsheet = workbook.createSheet(sheetname, 0); //sheet(); WritableFont font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, jxl.format.UnderlineStyle. NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat format = new WritableCellFormat(font); for (int i = 0; i < titles.length; i++) { Label wlabel1 = new Label(i, 0, titles[i], format); //行、列、单元格中的文本、文本格式 wsheet.addCell(wlabel1); } font = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle. NO_UNDERLINE, jxl.format.Colour.BLACK); format = new jxl.write.WritableCellFormat(font); for (int i = 0; i < vect.size(); i++) { // 在索引0的位置创建行(最顶端的行) String[] sdata = (String[]) vect.elementAt(i); for (int j = 0; j < sdata.length; j++) { //在索引0的位置创建单元格(左上端) Label wlabel1 = new Label(j, i, sdata[j], format); //行、列、单元格中的文本、文本格式 wsheet.addCell(wlabel1); System.out.println(sdata[j] + ":::" + sdata[j]); } } workbook.write(); //写入文件 } catch (WriteException ex1) { b1 = false; System.out.println("WriteException:"+ex1.getMessage()); } catch (IOException ex2) { b1 = false; System.out.println("IOException:"+ex2.getMessage()); } workbook.close(); os.close(); return b1; } } DownloadAction: import org.apache.struts.action.*; import javax.servlet.http.*; import com.segsec.base.ExcelBean; import com.segsec.data.basedata; import java.util.Vector; /** * <p>Title:DownloadAction </p> * <p>Description: QRRSMMS </p> * <p>Copyright: Copyright (c) 2004 jiahansoft</p> * <p>Company: jiahansoft</p> * @author wanghw * @version 1.0 */ public class DownloadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { basedata databean = new basedata(); Vector vect = new Vector(); boolean b1= false; try { String fname = "test"; //Excel文件名 String[] titles = {"车牌号", "车辆类型", "用户帐号", "车台号"}; //标题 String sheetname = "车辆列表"; ExcelBean eb = new ExcelBean(); vect = databean.QuerySimilarVehicleInfoByAdminer(); if(vect.size()>0){ b1 = eb.exportToExcel(response, fname, sheetname, titles, vect); //调用生成excel文件bean } } catch (Exception e) { System.out.println(e); } if(b1){ return mapping.findForward("display"); }else{ return mapping.findForward("failure"); } } } basedata: import java.sql.PreparedStatement; import java.util.Vector; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Connection; public class basedata { public Vector QuerySimilarVehicleInfoByAdminer(){ Vector vect = new Vector(); int i = 0; Connection conn = null; PreparedStatement pstam =null; ResultSet rs =null; String url = "jdbc:oracle:thin:@90.0.8.2:1521:ORCL"; String username = "gisap"; String password = "1"; String drivers = "oracle.jdbc.driver.OracleDriver"; String sqlStr = "SELECT V.NUMBER_PLATE,v.vehicletype,A.ADMINER,U.CALL_LETTER " +" FROM T_ADMINER_UNIT AU,T_ADMINER A,T_VEHICLE V,T_UNIT U " +" WHERE A.ADMINERID=AU.ADMINERID" +" AND A.ADMINER ='master' " +" AND AU.UNITID = U.UNITID " +" AND U.UNITID = V.UNITID " +" ORDER BY V.NUMBER_PLATE "; try{ Class.forName(drivers); conn = DriverManager.getConnection(url, username, password); pstam = conn.prepareStatement(sqlStr); rs = pstam.executeQuery(); int collength = rs.getMetaData().getColumnCount(); while (rs.next()) { collength = rs.getMetaData().getColumnCount(); String[] rowdata = new String[collength]; for (int k = 1; k <= collength; k++) { rowdata[k - 1] = rs.getString(k); //new String(rs.getString(k).getBytes("ISO-8859-1"), "GBK"); // } vect.add(rowdata); } }catch(Exception e){ System.out.println("QuerySimilarVehicleInfoByAdminer Error!"+e.getMessage()); } finally{ try{ rs.close(); pstam.close(); conn.close(); }catch(Exception e){} } return vect; } public static void main(String[] args) { basedata basedata = new basedata(); } } 在struts-config.xml文件的<action-mappings></action-mappings>段中加入: <action type="com.segsec.action.DownloadAction" path="/downloadAction"> <forward name="display" path="/welcome.jsp" /> </action>
相关文章推荐
- 读取含有json格式数据的txt文件 并且把数据保存到数据库
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 网络编程:上传文件, * 需求:客户端需定义输入流读取客户端本地磁盘中的数据,使用getOutputStream给服务端发送数据。 * 服务端需要定义输出流,将文件数据保存到服务端的本地磁盘中。
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- Asp.Net上传文件到Access数据中,并从数据库中读取文件并保存
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- SQL C# nvarchar类型转换为int类型 多表查询的问题,查询结果到新表,TXT数据读取到控件和数据库,生成在控件中的数据如何存到TXT文件中
- 插入数据读取数据库写入到Text文件保存
- asp.net怎么读取或保存xml文件里的数据范例
- Java实现头像截取裁剪后图片保存,获取的是base64数据,怎么转换为图片并保存到数据库。并读取出来。
- PHP将数据库中的数据输出到txt文件,怎么读取txt文件
- QX项目实战-3.读取数据、数据写入数据库、读出数据生成新文件
- java小程序从数据库中(表)中读取数据然后创建文件夹生成txt文件
- [VB.NET]为怎么用GetPrivateProfileInt无法读取INI文件数据,
- [ASP.net]从数据库中读取数据自动生成XML
- [ASP.net]从数据库中读取数据自动生成XML
- asp下拉列表框 怎么读取数据库中的数据
- 求助:怎么将excel文件导入datagridview显示 并保存到数据库?
- 如何将Excel和xml文件数据保存到数据库
- 几种从数据库读取数据生成excel文件的比较