欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java转接sqlite,导出成excel 博客分类: 生活 SQLiteJavaExcelJDBCSQL 

程序员文章站 2024-02-12 20:03:58
...
java 代码
 
  1. package com.taihee.sql;  
  2.   
  3. import java.io.File;  
  4. import java.sql.*;  
  5.   
  6. import jxl.Workbook;  
  7. import jxl.write.Label;  
  8. import jxl.write.WritableSheet;  
  9. import jxl.write.WritableWorkbook;  
  10.   
  11. import org.sqlite.JDBC;  
  12.   
  13. import com.taihee.swt.constant.GBISO;  
  14. public class SQLitetoXLS {  
  15.     public void proccess(ResultSet rs,String file_name){  
  16.        rstoXls(rs,file_name);  
  17.     }  
  18.     public void rstoXls(ResultSet rs,String bm) {  
  19.           
  20.         try {  
  21.             int i=1;  
  22.              WritableWorkbook book=Workbook.createWorkbook(
  23.                               new File("D:\\jxpj\\"+bm+".xls")); 
  24.              WritableSheet sheet=book.createSheet(bm,0); 
  25.              ResultSetMetaData rsmt=rs.getMetaData(); 
  26.                for(int z=0;z<rsmt.getColumnCount();z++){
  27.                  Label label1=new Label(z,0,rsmt.getColumnName(z+1)); 
  28.                  sheet.addCell(label1);  
  29.              } 
  30.  
  31.             while(rs.next()){ 
  32.                     for(int y=0;y<rsmt.getColumnCount();y++){
  33.                      Label label=new Label(y,i,GBISO.iso2gb(rs.getString(y+1))); 
  34.                      sheet.addCell(label); 
  35.                  } 
  36.                  i++; 
  37.                  
  38.             } 
  39.              book.write(); 
  40.              book.close(); 
  41.         } catch (Exception e) { 
  42.             e.printStackTrace(); 
  43.         } 
  44.          
  45.     } 
  46.     public static void main(String[] args) { 
  47.         try {
  48.             String fileName = "F:/SQLite/mydb"; 
  49.             Class.forName("org.sqlite.JDBC"); 
  50.             Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName); 
  51.             Statement stmt = conn.createStatement(); 
  52.             ResultSet rs = stmt.executeQuery("SELECT k_address 考试地点,count(k_address) 考场容量 from anpai group by k_address"); 
  53.             SQLitetoXLS sqlite=new SQLitetoXLS(); 
  54.             sqlite.rstoXls(rs,"ok");  
  55.             conn.close();  
  56.               
  57.         }  
  58.         catch (Exception e) {  
  59.             System.out.println(e.getMessage());  
  60.             System.out.println(e.toString());  
  61.         }  
  62.           
  63.     }  
  64.       
  65. }