java转接sqlite,导出成excel 博客分类: 生活 SQLiteJavaExcelJDBCSQL
程序员文章站
2024-02-12 20:03:58
...
java 代码
- package com.taihee.sql;
- import java.io.File;
- import java.sql.*;
- import jxl.Workbook;
- import jxl.write.Label;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import org.sqlite.JDBC;
- import com.taihee.swt.constant.GBISO;
- public class SQLitetoXLS {
- public void proccess(ResultSet rs,String file_name){
- rstoXls(rs,file_name);
- }
- public void rstoXls(ResultSet rs,String bm) {
- try {
- int i=1;
- WritableWorkbook book=Workbook.createWorkbook(
- new File("D:\\jxpj\\"+bm+".xls"));
- WritableSheet sheet=book.createSheet(bm,0);
- ResultSetMetaData rsmt=rs.getMetaData();
- for(int z=0;z<rsmt.getColumnCount();z++){
- Label label1=new Label(z,0,rsmt.getColumnName(z+1));
- sheet.addCell(label1);
- }
- while(rs.next()){
- for(int y=0;y<rsmt.getColumnCount();y++){
- Label label=new Label(y,i,GBISO.iso2gb(rs.getString(y+1)));
- sheet.addCell(label);
- }
- i++;
- }
- book.write();
- book.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- try {
- String fileName = "F:/SQLite/mydb";
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName);
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT k_address 考试地点,count(k_address) 考场容量 from anpai group by k_address");
- SQLitetoXLS sqlite=new SQLitetoXLS();
- sqlite.rstoXls(rs,"ok");
- conn.close();
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- System.out.println(e.toString());
- }
- }
- }
上一篇: String与Long类型比较的陷阱 博客分类: codes
下一篇: 常用命令备注