打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
将SQLite中的数据转换为Excel表

此程序能将指定SQlite数据库中的数据转换为Excel表格形式

  1. package zhaoxing.android.tool;  
  2.   
  3.   
  4. import java.io.File;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import jxl.Workbook;  
  9. import jxl.write.Label;  
  10. import jxl.write.WritableSheet;  
  11. import jxl.write.WritableWorkbook;  
  12. import jxl.write.WriteException;  
  13. import jxl.write.biff.RowsExceededException;  
  14. import android.database.Cursor;  
  15. import android.database.sqlite.SQLiteDatabase;  
  16.   
  17. public class DatabaseDump {  
  18.     private String mDestXmlFilename;  
  19.     private SQLiteDatabase mDb;  
  20.   
  21.   
  22.     public DatabaseDump(SQLiteDatabase db, String destXml) {  
  23.         mDb = db;  
  24.         mDestXmlFilename = destXml;  
  25.     }  
  26.   
  27.     public void exportData() {  
  28.   
  29.         try {  
  30.   
  31.         //  Log.i("mdb", mDb.getPath());  
  32.             // get the tables out of the given sqlite database  
  33.             String sql = "SELECT * FROM sqlite_master";  
  34.   
  35.             Cursor cur = mDb.rawQuery(sql, new String[0]);  
  36.             cur.moveToFirst();  
  37.   
  38.             String tableName;  
  39.             while (cur.getPosition() < cur.getCount()) {  
  40.                 tableName = cur.getString(cur.getColumnIndex("name"));  
  41.   
  42.                 // don't process these two tables since they are used  
  43.                 // for metadata  
  44.                 if (!tableName.equals("android_metadata")  
  45.                         && !tableName.equals("sqlite_sequence")) {  
  46.                     writeExcel(tableName);  
  47.                 }  
  48.   
  49.                 cur.moveToNext();  
  50.             }  
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.   
  56.     /** 
  57.      * 生成一个Excel文件 
  58.      *  
  59.      * @param fileName 
  60.      *            要生成的Excel文件名 
  61.      */  
  62.     public void writeExcel(String tableName) {  
  63.         WritableWorkbook wwb = null;  
  64.         String fileName;  
  65.         fileName = "/sdcard/QuestionData/" + tableName + ".xls";  
  66.         int r = 0;  
  67.   
  68.         String sql = "select * from " + tableName;  
  69.         Cursor cur = mDb.rawQuery(sql, new String[0]);  
  70.         int numcols = cur.getColumnCount();  
  71.         int numrows = cur.getCount();  
  72.         // Log.i("row", numrows + "");  
  73.         // Log.i("col", numcols + "");  
  74.   
  75.         String records[][] = new String[numrows + 1][numcols];// 存放答案,多一行标题行  
  76.   
  77.         if (cur.moveToFirst()) {  
  78.             while (cur.getPosition() < cur.getCount()) {  
  79.                 for (int c = 0; c < numcols; c++) {  
  80.                     if (r == 0) {  
  81.                         records[r][c] = cur.getColumnName(c);  
  82.                         records[r + 1][c] = cur.getString(c);  
  83.                     } else {  
  84.                         records[r + 1][c] = cur.getString(c);  
  85.                     }  
  86.                 //  Log.i("value" + r + " " + c, records[r][c]);  
  87.                 }  
  88.                 cur.moveToNext();  
  89.                 r++;  
  90.             }  
  91.   
  92.             cur.close();  
  93.         }  
  94.         try {  
  95.             // 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象  
  96.             wwb = Workbook.createWorkbook(new File(fileName));  
  97.         } catch (IOException e) {  
  98.             e.printStackTrace();  
  99.         }  
  100.         if (wwb != null) {  
  101.             // 创建一个可写入的工作表  
  102.             // Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置  
  103.             WritableSheet ws = wwb.createSheet("sheet1"0);  
  104.   
  105.             // 下面开始添加单元格  
  106.             for (int i = 0; i < numrows + 1; i++) {  
  107.                 for (int j = 0; j < numcols; j++) {  
  108.                     // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行  
  109.                     Label labelC = new Label(j, i, records[i][j]);  
  110.             //      Log.i("Newvalue" + i + " " + j, records[i][j]);  
  111.                     try {  
  112.                         // 将生成的单元格添加到工作表中  
  113.                         ws.addCell(labelC);  
  114.                     } catch (RowsExceededException e) {  
  115.                         e.printStackTrace();  
  116.                     } catch (WriteException e) {  
  117.                         e.printStackTrace();  
  118.                     }  
  119.   
  120.                 }  
  121.             }  
  122.   
  123.             try {  
  124.                 // 从内存中写入文件中  
  125.                 wwb.write();  
  126.                 // 关闭资源,释放内存  
  127.                 wwb.close();  
  128.             } catch (IOException e) {  
  129.                 e.printStackTrace();  
  130.             } catch (WriteException e) {  
  131.                 e.printStackTrace();  
  132.             }  
  133.         }  
  134.     }  
  135. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
sqlite常量的定义
python中常用的各种数据库操作模块和连接实例
又一个 SQLite 的增强神器
5秒完成两个excel表格的核对,想不到会这么快!
Excel透视表数据更新技巧,数据源同步更新,轻松不操心
EXCEL中把一列数据复制到另一列合并单元格中,怎么快速操作?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服