Android 读取Excel内容

AS 读取Excel内容

prep


allprojects {
                repositories {
                        ...
                        maven { url 'https://jitpack.io' }
                }
        }

implementation 'com.github.zhouzhuo810:ZzExcelCreator:1.0.6'  //封装好的方法
// 需要使用到的权限
<!--读取SDCard数据权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

impl

              WritableCellFormat format = null;
              try {    
                    format = ZzFormatCreator            
                          .getInstance()            
                          .createCellFont(WritableFont.ARIAL)  //设置字体            
                          .setAlignment(Alignment.CENTRE, VerticalAlignment.CENTRE)  //设置对齐方式(水平和垂直) 
                          .setFontSize(14)                    //设置字体大小           
                          .setWrapContent(true, 100)          //设置是否自适应宽高,如果自适应,必须设置最大列宽            
                          .getCellFormat();
                  } catch (WriteException e) {   
                        e.printStackTrace();
                  }


              
              // 获取到android的系统路径 (sd卡)
               String path = Environment.getExternalStorageDirectory().getAbsolutePath();
               //自定义的文件夹和文件名称
                String dir = "/imeis";
                String fileName = "/imei.xls";
                
              
                WritableCellFormat finalFormat = format;
                // 读取的操作最好放在子线程中做
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File file = new File(path + dir + fileName);
                        ZzExcelCreator zzExcelCreator = null;
                        try {
                            zzExcelCreator = ZzExcelCreator
                                    .getInstance()//对象
                                    .openExcel(file)//读取文件
                                    .openSheet(0);//第一页
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (BiffException e) {
                            e.printStackTrace();
                        }
                        //读取单元格内容
                       //(0,0) 表示 0列0行
                        String content = zzExcelCreator.getCellContent(0, 0); //(col,row)
                        }
                      
                        try {
                           // 使用完要 close掉
                            zzExcelCreator.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (WriteException e) {
                            e.printStackTrace();
                        }
                        
               }
               }).start();

others

创建Excel文件和工作表


ZzExcelCreator
                .getInstance()
                .createExcel(filePath, fileName)  //生成excel文件
                .createSheet(sheetName)        //生成sheet工作表
                .close();

添加工作表


ZzExcelCreator
                .getInstance()
                .openExcel(new File(filePath + fileName + ".xls"))  //如果不想覆盖文件,注意是openExcel
                .createSheet(sheetName)
                .close();

打开Excel文件和工作表


 ZzExcelCreator
                .getInstance()
                .openExcel(new File(filePath + fileName + ".xls"))  //打开Excel文件
                .openSheet(0)                                   //打开Sheet工作表
                ... ...
                .close();

设置单元格内容格式


 //设置单元格内容格式
        WritableCellFormat format = ZzFormatCreator
                .getInstance()
                .createCellFont(WritableFont.ARIAL)  //设置字体
                .setAlignment(Alignment.CENTRE, VerticalAlignment.CENTRE)  //设置对齐方式(水平和垂直)
                .setFontSize(14)                    //设置字体大小
                .setFontColor(Colour.ROSE)          //设置字体颜色
                .setFontBold(true)                  //设置是否加粗,默认false
                .setUnderline(true)                 //设置是否画下划线,默认false
                //.setDoubleUnderline(true)         //设置是否画双重下划线,默认false,和setUnderline只有一个生效
                .setItalic(true)                    //设置是否斜体
                .setWrapContent(true, 100)          //设置是否自适应宽高,如果自适应,必须设置最大列宽(不能太大,否则可能无效)。
                .setBackgroundColor(ColourUtil.getCustomColor1("#99cc00"))  //设置单元格背景颜色,如果不设置边框,边框色会和背景色一致。
                .setBorder(Border.ALL, BorderLineStyle.THIN, ColourUtil.getCustomColor2("#dddddd"))  //设置边框样式
                .getCellFormat();

设置行高、列宽和写入字符串或数字


ZzExcelCreator
                .getInstance()
                .openExcel(new File(PATH + fileName + ".xls"))
                .openSheet(0)
                .setColumnWidth(colInt, 25)   //设置列宽(如果自适应宽度,代表内容字节的长度,即str.getBytes().length)
                .setRowHeight(rowInt, 400)    //设置行高
                .fillContent(colInt, rowInt, str, format)  //填入字符串
                .fillNumber(colInt, rowInt, Double.parseDouble(str), format)  //填入数字
                .close();

读取单元格内容


ZzExcelCreator zzExcelCreator = ZzExcelCreator
                .getInstance()
                .openExcel(new File(filePath + fileName + ".xls"))
                .openSheet(0);
        //读取单元格内容
        String content =  zzExcelCreator.getCellContent(colInt, rowInt);
        //别忘了close
        zzExcelCreator.close();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值