收藏——Java导出数据到excel保存在服务器及客户端下载

时间:2021-12-07 17:52:11

例子:(来自:http://www.iteye.com/problems/54701

import java.io.File;
import java.io.FileOutputStream;
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 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"));
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();
}
}

//入口main方法
public static void main(String[] args){
TestExcel te=new TestExcel();
File f=new File("d:\\kk.xls");
try{
f.createNewFile();
OutputStream os=new FileOutputStream(f);
te.exportClassroom(os);
}catch(Exception e){
e.printStackTrace();
}

}

}

Java文件下载的几种方式

http://yaofeng911.iteye.com/blog/472492

 

下载服务器上的文件-纯java处理

http://shuixian.iteye.com/blog/706870

 

jsp从服务器下载xls文件到客户端

http://yuanyuan7891.iteye.com/blog/723483

 

jsp从服务器下载xls文件到客户端

http://yuanyuan7891.iteye.com/blog/723483

 

java的路径全接触

http://www.360doc.com/content/08/0327/19/3123_1148156.shtml

 

从服务器上导出excel文件到本地

http://blog.csdn.net/xinguojava/article/details/6256538

 

linux系统中文件上传与下载(简单的命令,值得参考)

http://wangjinlongaisong-126-com.iteye.com/blog/1464708