最近需要用到把Android数据到处保存到Excel表格中,个人感觉也比较实用,所以就和大家分享一下。以前没接触过该怎么办呢?有问题找百度,只有你想不到的没有你找不到的。在网上也找到了一些例子,自己又加以完善。代码也比较容易,只需要用到一个jxl.jar包,举一反三android数据应该也能转为Word什么的,呵呵。

        Demo实现了一个简单的记账功能,点击导出账单就导出到了Excel表。点击导入账单就把Excel数据显示到Android手机上了。导出账单先把数据保存到Sqlite中然后再倒入Excel,这样做的好处是就算你把Excel表格误删了,还是能从Sqlite中读出以前的保存记录。效果图如下:




主要代码是ExcelUtils类,封装了把数据转化为Excel的工具类:

[html]  view plain  copy  
  1. public class ExcelUtils {  
  2.     public static WritableFont arial14font = null;  
  3.   
  4.     public static WritableCellFormat arial14format = null;  
  5.     public static WritableFont arial10font = null;  
  6.     public static WritableCellFormat arial10format = null;  
  7.     public static WritableFont arial12font = null;  
  8.     public static WritableCellFormat arial12format = null;  
  9.   
  10.     public final static String UTF8_ENCODING = "UTF-8";  
  11.     public final static String GBK_ENCODING = "GBK";  
  12.   
  13.     public static void format() {  
  14.         try {  
  15.             arial14font = new WritableFont(WritableFont.ARIAL, 14,  
  16.                     WritableFont.BOLD);  
  17.             arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);  
  18.             arial14format = new WritableCellFormat(arial14font);  
  19.             arial14format.setAlignment(jxl.format.Alignment.CENTRE);  
  20.             arial14format.setBorder(jxl.format.Border.ALL,  
  21.                     jxl.format.BorderLineStyle.THIN);  
  22.             arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);  
  23.             arial10font = new WritableFont(WritableFont.ARIAL, 10,  
  24.                     WritableFont.BOLD);  
  25.             arial10format = new WritableCellFormat(arial10font);  
  26.             arial10format.setAlignment(jxl.format.Alignment.CENTRE);  
  27.             arial10format.setBorder(jxl.format.Border.ALL,  
  28.                     jxl.format.BorderLineStyle.THIN);  
  29.             arial10format.setBackground(jxl.format.Colour.LIGHT_BLUE);  
  30.             arial12font = new WritableFont(WritableFont.ARIAL, 12);  
  31.             arial12format = new WritableCellFormat(arial12font);  
  32.             arial12format.setBorder(jxl.format.Border.ALL,  
  33.                     jxl.format.BorderLineStyle.THIN);  
  34.         } catch (WriteException e) {  
  35.   
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.   
  40.     public static void initExcel(String fileName, String[] colName) {  
  41.         format();  
  42.         WritableWorkbook workbook = null;  
  43.         try {  
  44.             File file = new File(fileName);  
  45.             if (!file.exists()) {  
  46.                 file.createNewFile();  
  47.             }  
  48.             workbook = Workbook.createWorkbook(file);  
  49.             WritableSheet sheet = workbook.createSheet("家庭帐务表", 0);  
  50.             sheet.addCell((WritableCell) new Label(0, 0, fileName,  
  51.                     arial14format));  
  52.             for (int col = 0; col < colName.length; col++) {  
  53.                 sheet.addCell(new Label(col, 0, colName[col], arial10format));  
  54.             }  
  55.             workbook.write();  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         } finally {  
  59.             if (workbook != null) {  
  60.                 try {  
  61.                     workbook.close();  
  62.                 } catch (Exception e) {  
  63.                     // TODO Auto-generated catch block  
  64.                     e.printStackTrace();  
  65.                 }  
  66.             }  
  67.         }  
  68.   
  69.     }  
  70.   
  71.     @SuppressWarnings("unchecked")  
  72.     public static <T> void writeObjListToExcel(List<T> objList,  
  73.             String fileName, Context c) {  
  74.         if (objList != null && objList.size() > 0) {  
  75.             WritableWorkbook writebook = null;  
  76.             InputStream in = null;  
  77.             try {  
  78.                 WorkbookSettings setEncode = new WorkbookSettings();  
  79.                 setEncode.setEncoding(UTF8_ENCODING);  
  80.                 in = new FileInputStream(new File(fileName));  
  81.                 Workbook workbook = Workbook.getWorkbook(in);  
  82.                 writebook = Workbook.createWorkbook(new File(fileName),  
  83.                         workbook);  
  84.                 WritableSheet sheet = writebook.getSheet(0);  
  85.                 for (int j = 0; j < objList.size(); j++) {  
  86.                     ArrayList<String> list = (ArrayList<String>) objList.get(j);  
  87.                     for (int i = 0; i < list.size(); i++) {  
  88.                         sheet.addCell(new Label(i, j + 1, list.get(i),  
  89.                                 arial12format));  
  90.                     }  
  91.                 }  
  92.                 writebook.write();  
  93.                 Toast.makeText(c, "导出到手机存储中文件夹Family成功", Toast.LENGTH_SHORT).show();  
  94.             } catch (Exception e) {  
  95.                 e.printStackTrace();  
  96.             } finally {  
  97.                 if (writebook != null) {  
  98.                     try {  
  99.                         writebook.close();  
  100.                     } catch (Exception e) {  
  101.                         e.printStackTrace();  
  102.                     }  
  103.   
  104.                 }  
  105.                 if (in != null) {  
  106.                     try {  
  107.                         in.close();  
  108.                     } catch (IOException e) {  
  109.                         e.printStackTrace();  
  110.                     }  
  111.                 }  
  112.             }  
  113.   
  114.         }  
  115.     }  
  116.   
  117.     public static  List<BillObject> read2DB(File f, Context con) {  
  118.         ArrayList<BillObject> billList = new ArrayList<BillObject>();  
  119.         try {  
  120.             Workbook course = null;  
  121.             course = Workbook.getWorkbook(f);  
  122.             Sheet sheet = course.getSheet(0);  
  123.               
  124.             Cell cell = null;  
  125.             for (int i = 1; i < sheet.getRows(); i++) {  
  126.                 BillObject tc = new BillObject();  
  127.                 cell = sheet.getCell(1, i);  
  128.                 tc.setFood(cell.getContents());  
  129.                 cell = sheet.getCell(2, i);  
  130.                 tc.setClothes(cell.getContents());  
  131.                 cell = sheet.getCell(3, i);  
  132.                 tc.setHouse(cell.getContents());  
  133.                 cell = sheet.getCell(4, i);  
  134.                 tc.setVehicle(cell.getContents());  
  135.                 Log.d("gaolei", "Row"+i+"---------"+tc.getFood() + tc.getClothes()  
  136.                         + tc.getHouse() + tc.getVehicle());  
  137.                 billList.add(tc);  
  138.                   
  139.             }  
  140.               
  141.         } catch (Exception e) {  
  142.             // TODO Auto-generated catch block  
  143.             e.printStackTrace();  
  144.         }  
  145.         return billList;  
  146.     }  
  147.   
  148.     public static Object getValueByRef(Class cls, String fieldName) {  
  149.         Object value = null;  
  150.         fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName  
  151.                 .substring(0, 1).toUpperCase());  
  152.         String getMethodName = "get" + fieldName;  
  153.         try {  
  154.             Method method = cls.getMethod(getMethodName);  
  155.             value = method.invoke(cls);  
  156.         } catch (Exception e) {  
  157.             e.printStackTrace();  
  158.         }  
  159.         return value;  
  160.     }  
  161. }  


             项目源码地址,点击下载......

更多相关文章

  1. Android 数据库存储之db4o
  2. android缓存数据 分析
  3. 阿里Android开发规范:文件与数据库
  4. 赤兔Android数据恢复专业版发布啦
  5. Android:复杂数据模型的传递
  6. android Activity线性布局和表格布局实例讲解
  7. Android逆向之旅---Android中如何获取在非Root设备中获取应用隐
  8. java for android sqlite数据的操作:说实话,我用了ADO.NET的思想,自

随机推荐

  1. 关于安卓中 WindowManager.LayoutParams(
  2. unity3d发布Android程序
  3. Cocos2d-x 新版本中android震动
  4. Android中,单位dp、sp、px互相转换工具类
  5. Android(安卓)仿微信朋友圈发动态功能(相
  6. android源码分析流程-init.c
  7. Android(安卓)Studio3.0升级gradle遇到的
  8. Android更新完ADT后遇到的问题
  9. android studio 更改 app 包名和包的长度
  10. OSChina 技术周刊第十期,每周技术抢先看!