用jxl从数据库导出的excel文件是导出到本地还是导出到服务器

问题描述

如题,代码如下:package test;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream; import java.io.IOException; import java.io.OutputStream; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.List; import javax.servlet.*; import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class TestExcel {private String driverClass = "net.sourceforge.jtds.jdbc.Driver"; private String url ="jdbc:jtds:sqlserver:/localhost:1433/demo"; private String user = "sa"; private String password = ""; private Connection connection;public void exportClassroom(OutputStream os) { try { WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件 WritableSheet wsheet = wbook.createSheet("报警记录表", 0); //工作表名称 //设置Excel字体 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = { "报警记录编号", "报警时间", "报警设备", "报警设备编号","报警事件名称","报警编号"}; //设置Excel表头 for (int i = 0; i < title.length; i++) { Label excelTitle = new Label(i, 0, title[i], titleFormat); wsheet.addCell(excelTitle); } int c = 1; //用于循环时Excel的行号 Connection con=openConnection(); Statement st=con.createStatement(); String sql="select * from jlbjsj where BJJLSJ between '2010-09-01 08:19:25' and '2010-12-02 08:19:25' and BJSBMC like '%'";ResultSet rs=st.executeQuery(sql); //这个是从数据库中取得要导出的数据 while (rs.next()){Label content1 = new Label(0, c, (String)rs.getString("BJJLBH")); Label content2 = new Label(1, c, (String)rs.getString("BJJLSJ")); Label content3 = new Label(2, c, (String)rs.getString("BJSBMC")); Label content4 = new Label(3, c, (String)rs.getString("BJSBBH"));Label content5 = new Label(4, c, (String)rs.getString("BJBLMC"));Label content6 = new Label(5, c, (String)rs.getString("BJBLBH"));//Label content7 = new Label(3, c, (String)rs.getString("BJSBBH"));wsheet.addCell(content1); wsheet.addCell(content2); wsheet.addCell(content3); wsheet.addCell(content4);wsheet.addCell(content5);wsheet.addCell(content6);c++; } wbook.write(); //写入文件 wbook.close(); os.close(); System.out.println("导入成功!");} catch (Exception e) { e.printStackTrace(); } }public Connection openConnection() throws SQLException { try { Class.forName(driverClass).newInstance(); connection = DriverManager.getConnection(url, user, password); return connection; } catch (Exception e) { throw new SQLException(e.getMessage()); } } public void closeConnection() { try { if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args){ TestExcel te=new TestExcel(); File f=new File("c:\kk.xls"); try{ f.createNewFile(); OutputStream os=new FileOutputStream(f); te.exportClassroom(os); }catch(Exception e){ e.printStackTrace(); } }} 问题补充:梦中有你 写道

解决方案

文件下载例子:[url]http://yaofeng911.iteye.com/blog/472492[/url]把相应的流、文件替换成你的就OK了
解决方案二:
# File f=new File("c:\kk.xls"); # try{ # f.createNewFile(); # OutputStream os=new FileOutputStream(f); # te.exportClassroom(os); # }catch(Exception e){ # e.printStackTrace(); # } 你自己写的代码,你是用File来构建OutputStream的 你说它最后写入到哪里去了?c:/kk.xls啊 如果你的项目发布到了服务器上,那就在服务器的C盘 你想弄到本地的话你就别写入文件,用装载了数据的流传输给客户端,让客户保存,即可弄到本地(相当于文件下载)

时间: 2024-08-02 00:49:59

用jxl从数据库导出的excel文件是导出到本地还是导出到服务器的相关文章

jsp mysql excel-在jsp中使用jxl.jar包来将mysql搜索语句sql结果集导出到excel文件

问题描述 在jsp中使用jxl.jar包来将mysql搜索语句sql结果集导出到excel文件 整体构思是:连接数据库由DBManager.java,创建excel样式由Excel.java:最后在excel.jsp实现!但现在数据库数据并不能显示,求指教,我是java小白! Excel.java代码 package com; import java.io.*; import java.sql.*; import javax.sql.rowset.CachedRowSet; import com

link环境下如何用codefirst将数据库导出成excel文件?codefirst可以导出么?

问题描述 link环境下如何用codefirst将数据库导出成excel文件?codefirst可以导出么? link环境下如何用codefirst将数据库导出成excel文件?codefirst可以导出么? 解决方案 这个和codefirst没关系,直接用npoi导出就可以了.

asp代码实现access数据导出到excel文件

  asp代码实现access数据导出到excel文件: 一,下面是导出XLS格式 <%  dim referer  referer = Request.ServerVariables("HTTP_REFERER")  Dim CName,action  action=request.Form("action")  CName="../../Excel/"  dim daytime  'daytime=year(now())&&qu

birt 导出excel时,有38个字段,导出的excel文件中数据都叠在一起,格式非常难看

问题描述 birt导出excel时,有38个字段,导出的excel文件中数据都叠在一起,格式非常难看,该问题怎么解决.thx 解决方案 解决方案二:给你看看我以前servlet导出Excel的例子publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("gbk");Employ

ASP.NET2.0中将GridView导出到Excel文件中

下面代码实现将 GridView 导出到 Excel文件中. 值得注意的是VerifyRenderingInServerForm重载方法: MSDN上的 VerifyRenderingInServerForm 方法的描述: 必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息.发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法.呈现服务器窗体元素的方式不同的页可以重写此方法以在不同

将DW数据窗口导出为EXCEL文件的方法(整理)

excel|数据 本文为摘自CSDN论坛帖子收集整理后汇总版本: ---2004年9月3日整理 ---原贴见: http://community.csdn.net/Expert/topic/3328/3328715.xml?temp=8.050799E-04 鉴于现在很多朋友询问 pb 数据导出到excel 中的问题,导出去后格式和数据类型不对了,自己写了几个用户对象,希望能抛砖引玉,加强技术交流,得到大家的支持. 1. nvo_excel 只要是两个接口函数导出数据存储的数据,可以定义 exc

tableExport.js导出Excel表格如何给导出的Excel文件赋名字?

问题描述 tableExport.js导出Excel表格如何给导出的Excel文件赋名字? 其中该JS中有个TableName属性,并没有起作用或者我的使用方式不对,求大神解答? 解决方案 http://www.cnblogs.com/qqflying/archive/2012/03/21/2409484.html 解决方案二: http://blog.csdn.net/kalision/article/details/8350506 解决方案三: js前端导出excel表格js 将table表

有谁做个gridview 导出到Excel文件.

问题描述 有谁做个gridview导出到Excel文件.必须导出到officeexcel2007中,请大侠们给个例子参考参考!谢谢! 解决方案 解决方案二:usingSystem.IO;usingSystem.Text;//页面增加一个按钮,单击事件添加如下方法:protectedvoidButton1_Click(objectsender,EventArgse){Export("application/ms-excel","学生成绩报表.xls");}privat

如何将datagridview显示内容导出为excel文件?在另外的机器上使用时有什么注意事项?

问题描述 将dataGridview中显示的数据导出生成EXCEL文件,已经编译成功,但是放到别的机器上使用的时候又不行,提示版本不对,有什么解决方法吗?运行的机器上已经装有excel.源程序如下:///<summary>///zjjzdq.com.cn///</summary>///<paramname="dt"></param>protectedvoidExportExcel(DataTabledt){if(dt==null||dt.