io

来源:互联网 发布:逆战天梯怎么卡数据 编辑:程序博客网 时间:2024/06/06 00:14

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

[java] view plain copy
  1. import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.OutputStream;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9.   
  10. import jxl.Workbook;  
  11. import jxl.write.Label;  
  12. import jxl.write.WritableCellFormat;  
  13. import jxl.write.WritableFont;  
  14. import jxl.write.WritableSheet;  
  15. import jxl.write.WritableWorkbook;  
  16.   
  17. public class TestExcel {  
  18.     private String driverClass = "net.sourceforge.jtds.jdbc.Driver";  
  19.   
  20.     private String url ="jdbc:jtds:sqlserver:/localhost:1433/demo";  
  21.   
  22.     private String user = "sa";  
  23.   
  24.     private String password = "";  
  25.   
  26.     private Connection connection;  
  27.     public void exportClassroom(OutputStream os) {    
  28.   
  29.         try {    
  30.         WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件    
  31.         WritableSheet wsheet = wbook.createSheet("报警记录表"0); //工作表名称    
  32.         //设置Excel字体    
  33.         WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,    
  34.         WritableFont.BOLD, false,    
  35.         jxl.format.UnderlineStyle.NO_UNDERLINE,    
  36.         jxl.format.Colour.BLACK);    
  37.         WritableCellFormat titleFormat = new WritableCellFormat(wfont);    
  38.         String[] title = { "报警记录编号""报警时间""报警设备""报警设备编号","报警事件名称","报警编号"};    
  39.         //设置Excel表头    
  40.         for (int i = 0; i < title.length; i++) {    
  41.         Label excelTitle = new Label(i, 0, title[i], titleFormat);    
  42.         wsheet.addCell(excelTitle);    
  43.         }    
  44.         int c = 1//用于循环时Excel的行号    
  45.         Connection con=openConnection();  
  46.         Statement st=con.createStatement();  
  47.         String sql="select * from jlbjsj where BJJLSJ between '2010-09-01 08:19:25' and '2010-12-02 08:19:25' and BJSBMC like '%'";  
  48.         ResultSet rs=st.executeQuery(sql); //这个是从数据库中取得要导出的数据    
  49.         while (rs.next()){  
  50.             Label content1 = new Label(0, c, (String)rs.getString("BJJLBH"));    
  51.             Label content2 = new Label(1, c, (String)rs.getString("BJJLSJ"));    
  52.             Label content3 = new Label(2, c, (String)rs.getString("BJSBMC"));     
  53.             Label content4 = new Label(3, c, (String)rs.getString("BJSBBH"));  
  54.             Label content5 = new Label(4, c, (String)rs.getString("BJBLMC"));  
  55.             Label content6 = new Label(5, c, (String)rs.getString("BJBLBH"));  
  56.             wsheet.addCell(content1);    
  57.             wsheet.addCell(content2);    
  58.             wsheet.addCell(content3);    
  59.             wsheet.addCell(content4);  
  60.             wsheet.addCell(content5);  
  61.             wsheet.addCell(content6);  
  62.             c++;    
  63.         }    
  64.             wbook.write(); //写入文件    
  65.             wbook.close();    
  66.             os.close();   
  67.             System.out.println("导入成功!");  
  68.         } catch (Exception e) {    
  69.             e.printStackTrace();   
  70.         }    
  71.   
  72.     }  
  73.       
  74.     public Connection openConnection() throws SQLException {  
  75.         try {  
  76.             Class.forName(driverClass).newInstance();  
  77.             connection = DriverManager.getConnection(url, user, password);   
  78.             return connection;  
  79.         } catch (Exception e) {  
  80.             throw new SQLException(e.getMessage());  
  81.         }  
  82.     }  
  83.       
  84.     public void closeConnection() {  
  85.         try {          
  86.             if (connection != null)  
  87.                 connection.close();  
  88.         } catch (Exception e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.       
  93.     //入口main方法  
  94.     public static void main(String[] args){  
  95.         TestExcel te=new TestExcel();  
  96.         File f=new File("d:\\kk.xls");  
  97.         try{  
  98.             f.createNewFile();  
  99.             OutputStream os=new FileOutputStream(f);  
  100.             te.exportClassroom(os);  
  101.         }catch(Exception e){  
  102.             e.printStackTrace();  
  103.         }  
  104.           
  105.     }  
  106.       
  107. }  

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