全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android 操作excel功能实例代码

学习app对excel的读写控制

1.界面设计

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.npmaster.myexcel.MainActivity">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/childlayout"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
    <TableLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#ffcccccc"
      android:layout_margin="1dp">
    <!-- line1 -->
    <TableRow
      android:layout_width="match_parent"
      android:layout_height="20dp"
      android:background="#ffcccccc"
      android:layout_margin="0.5dp">
      <TextView
        android:layout_width="0.0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="工参数据导入"
        android:id="@+id/tvtip" />
      <TextView
        android:text="默认路径"
        android:layout_width="0.0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:id="@+id/tvpath" />
    </TableRow>
      <!---->
      <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffcc99"
        android:layout_margin="0.5dp">
        <Button
          android:text="导入数据"
          android:layout_width="0.0dp"
          android:layout_height="match_parent"
          android:id="@+id/btImport"
          android:textSize="12dp"
          android:layout_weight="1"/>
        <Button
          android:text="清理数据"
          android:layout_width="0.0dp"
          android:layout_height="match_parent"
          android:id="@+id/btClear"
          android:textSize="12dp"
          android:layout_weight="1"/>
      </TableRow>
    </TableLayout>
    <android.support.v7.widget.RecyclerView
      android:scrollbars="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:divider="#ffff0000"
      android:dividerHeight="10dp"
      android:id="@+id/rcvEngineerParameters"/>
  </LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffcccccc"
    android:layout_margin="0dp">
    <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#ffffcc99"
      android:layout_margin="0.5dp">
      <TextView
        android:text="Ci"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android:id="@+id/tvCid" />
      <TextView
        android:text="CellName:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android:id="@+id/tvCellName" />
      <TextView
        android:text="Longtitude"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android:id="@+id/tvLongtitude" />
      <TextView
        android:text="Attitude:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android:id="@+id/tvAttitude" />
      <TextView
        android:text="Azimuth:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android:id="@+id/tvAzimuth" />
    </TableRow>
  </TableLayout>
</LinearLayout>

2. 代码设计

package com.npmaster.myexcel;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
import android.widget.Toast;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import static android.support.v7.recyclerview.R.styleable.RecyclerView;
public class MainActivity extends AppCompatActivity {
  public static final int IMPORT_FILE_SUCCESS = 0;
  public static final int IMPORT_FILE_NOT_EXISTS= 1;
  public static final int IMPORT_FILE_FAIL = 2;
  private static final String LOG_TAG = "NPLOG";
  private String npmasterDir = "/npmaster/";
  private String filepName = "my.xls";
  private String absFullPath = "";
  private static final int DEFAULT_SHEET = 0;
  public List<EngineerCellInfo> cfgCelllist = null;
  public EngineerCellInfo defaultItem;
  private EngineerRecycleViewAdapter myRecycleViewAdapter;
  private RecyclerView recyclerView;
  Toast toast;
  Button btClear,btImport;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* 生成默认的title */
    initData();
    absFullPath =Environment.getExternalStorageDirectory().toString()+npmasterDir;
    createFolder(absFullPath);
    createEngineerFile(absFullPath+filepName);
    generateTestData();
    TextView tv = (TextView) findViewById(R.id.tvpath);
    tv.setText(absFullPath+filepName);
    recyclerView = (RecyclerView)findViewById(R.id.rcvEngineerParameters);
    //线式布局类似 listview
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(OrientationHelper.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    myRecycleViewAdapter = new EngineerRecycleViewAdapter(MainActivity.this,cfgCelllist);
    recyclerView.setAdapter(myRecycleViewAdapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    btClear = (Button)findViewById(R.id.btClear);
    btImport = (Button)findViewById(R.id.btImport);
    btClear.setOnClickListener(new MyClickListener());
    btImport.setOnClickListener(new MyClickListener());
  }
  class MyClickListener implements View.OnClickListener
  {
    @Override
    public void onClick(View v)
    {
      switch(v.getId())
      {
        case R.id.btImport:
          int result =readExcel(absFullPath+filepName);
          toast = Toast.makeText(MainActivity.this,String.valueOf(result),Toast.LENGTH_SHORT);
          toast.show();
          myRecycleViewAdapter.notifyDataSetChanged();
          break;
        case R.id.btClear:
          toast = Toast.makeText(MainActivity.this,"clear",Toast.LENGTH_SHORT);
          toast.show();
          break;
        default:
          break;
      }
    }
  }
  public void initData()
  {
    cfgCelllist = new ArrayList<EngineerCellInfo>();
    defaultItem = new EngineerCellInfo();
    defaultItem.item[0] = "Ci";
    defaultItem.item[1] = "Cellname";
    defaultItem.item[2] = "Longtitude";
    defaultItem.item[3] = "Attitude";
    defaultItem.item[4] = "Azimuth";
    cfgCelllist.add(defaultItem);
  }
  public void generateTestData()
  {
    EngineerCellInfo engineerCellInfo = new EngineerCellInfo();
    engineerCellInfo.item[0] = "134633";
    engineerCellInfo.item[1] = "xx小区";
    engineerCellInfo.item[2] = "103.89866";
    engineerCellInfo.item[3] = "30";
    engineerCellInfo.item[4] = "60";
    cfgCelllist.add(engineerCellInfo);
    writeToFile(absFullPath+filepName);
  }
  /* create folder if not exists*/
  public void createFolder(String strFolder)
  {
    File file = new File(strFolder);
    if (!file.exists())
    {
      if (file.mkdir())
      {
        return;
      }
    }
  }
  public void writeToFile(String cfgFileName)
  {
    File file = null;
    if (cfgCelllist.size()<=1)
    {
      return;
    }
    try {
      file = new File(cfgFileName);
      if(file.exists())
      {
        WritableWorkbook book = Workbook.createWorkbook(file);
        WritableSheet sheet = book.getSheet(DEFAULT_SHEET);
        for (int i = 1; i <cfgCelllist.size();i++)
        {
          for (int j=0;j<5;j++)
          {
            Label labelCi= new Label( j, i, cfgCelllist.get(i).item[j]);
            sheet.addCell(labelCi);
          }
        }
        book.write();
        book.close();
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /* 创建工参默认文件,如果文件不存在则创建*/
  public void createEngineerFile(String cfgFileName)
  {
    File file = null;
    try {
      file = new File(cfgFileName);
      if(file.exists()) {
        WritableWorkbook book = Workbook.createWorkbook(file);
        WritableSheet sheet = book.createSheet("task", DEFAULT_SHEET);
        for (int i = 0; i <cfgCelllist.size();i++)
        {
          for (int j=0;j<5;j++)
          {
            Label labelCi= new Label( j, i, cfgCelllist.get(i).item[j]);
            sheet.addCell(labelCi);
          }
        }
        book.write();
        book.close();
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /* read the cell file from engineer file */
  public int readExcel(String cfgFileName)
  {
    TextView tv = (TextView) findViewById(R.id.tvpath);
    try
    {
      File file = new File(cfgFileName);
      if (!file.exists())
      {
        return IMPORT_FILE_NOT_EXISTS;
      }
      InputStream inputStream = new FileInputStream(file);
      Workbook workbook = Workbook.getWorkbook(inputStream);
      Sheet sheet = workbook.getSheet(0);
      /* ignore the first row*/
      for (int i = 1; i < sheet.getRows();i++)
      {
        EngineerCellInfo engineerCellInfo = new EngineerCellInfo();
        for (int j = 0; j < 5;j++)
        {
          Cell cell = sheet.getCell(j,i);
          engineerCellInfo.item[j] = cell.getContents();
        }
        cfgCelllist.add(engineerCellInfo);
        //tv.setText(engineerCellInfo.toString());
      }
      workbook.close();
    }
    catch(Exception e)
    {
      Log.i(LOG_TAG,"file to find the file");
      return IMPORT_FILE_FAIL;
    }
    return IMPORT_FILE_SUCCESS;
  }
  public class EngineerCellInfo
  {
    public String item[]; //use array to store
    public EngineerCellInfo()
    {
      item = new String[5];
    }
    public String toString()
    {
      return item[0]+"-"+item[1]+"-"+item[2]+"-"+item[3]+"-"+item[4];
    }
  }
}
package com.npmaster.myexcel;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.npmaster.myexcel.MainActivity.EngineerCellInfo;
import java.util.List;
public class EngineerRecycleViewAdapter extends RecyclerView.Adapter<EngineerRecycleViewAdapter.MyViewHolder>
{
  private List<MainActivity.EngineerCellInfo> itemsData;
  private Context mContext;
  private LayoutInflater inflater;
  public EngineerRecycleViewAdapter(Context context, List<EngineerCellInfo> itemsData)
  {
    this.itemsData = itemsData;
    this.mContext=context;
    inflater=LayoutInflater.from(mContext);
  }
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    View view = inflater.inflate(R.layout.listitem, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
  }
  @Override
  public void onBindViewHolder(final MyViewHolder holder, final int position)
  {
    holder.tvCId.setText(String.valueOf(itemsData.get(position).item[0]));
    holder.tvCellName.setText(String.valueOf(itemsData.get(position).item[1]));
    holder.tvlongtitude.setText(String.valueOf(itemsData.get(position).item[2]));
    holder.tvAttitude.setText(String.valueOf(itemsData.get(position).item[3]));
    holder.tvAzimuth.setText(String.valueOf(itemsData.get(position).item[4]));
  }
  @Override
  public int getItemCount()
  {
    return itemsData.size();
  }
  public static class MyViewHolder extends RecyclerView.ViewHolder
  {
    public TextView tvCId;
    public TextView tvCellName;
    public TextView tvlongtitude;
    public TextView tvAttitude;
    public TextView tvAzimuth;
    public MyViewHolder(View view)
    {
      super(view);
      tvCId = (TextView)view.findViewById(R.id.tvCid);
      tvCellName = (TextView)view.findViewById(R.id.tvCellName);
      tvlongtitude = (TextView)view.findViewById(R.id.tvLongtitude);
      tvAttitude = (TextView) view.findViewById(R.id.tvAttitude);
      tvAzimuth = (TextView) view.findViewById(R.id.tvAzimuth);
    }
  }
}

3. 使用的jxl 开发包 和recyclerview

compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
testCompile 'junit:junit:4.12'
compile files('libs/jxl-2.6.jar')

以上所示是小编给大家介绍的Android 操作excel功能实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!


# android  # excel  # 操作  # Android应用读取Excel文件的方法  # Android实现仿excel数据表格效果  # Android 中读取Excel文件实例详解  # Android操作Excel文件的功能实现  # android通过jxl读excel存入sqlite3数据库  # Android开发实现生成excel的方法详解  # android读取assets中Excel表格并显示  # Android开发实现的导出数据库到Excel表格功能【附源码下载】  # Android开发实现读取excel数据并保存为xml的方法  # Android多国语言转换Excel及Excel转换为string详解  # 小编  # 给大家  # 不存在  # 所示  # 给我留言  # 有任何  # 开发包  # tvAttitude  # tvAzimuth  # Azimuth  # Longtitude  # tvCellName  # Attitude  # tvLongtitude  # Bundle  # AppCompatActivity  # View 


相关文章: 北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?  如何通过主机屋免费建站教程十分钟搭建网站?  如何快速辨别茅台真假?关键步骤解析  如何用已有域名快速搭建网站?  建站之星IIS配置教程:代码生成技巧与站点搭建指南  免费公司网站制作软件,如何申请免费主页空间做自己的网站?  已有域名和空间如何快速搭建网站?  宝盒自助建站智能生成技巧:SEO优化与关键词设置指南  建站之星ASP如何实现CMS高效搭建与安全管理?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  Avalonia如何实现跨窗口通信 Avalonia窗口间数据传递  如何在VPS电脑上快速搭建网站?  怀化网站制作公司,怀化新生儿上户网上办理流程?  全景视频制作网站有哪些,全景图怎么做成网页?  网站制作公司广州有几家,广州尚艺美发学校网站是多少?  如何高效完成自助建站业务培训?  为什么Go需要go mod文件_Go go mod文件作用说明  如何在Windows服务器上快速搭建网站?  贸易公司网站制作流程,出口贸易网站设计怎么做?  深圳网站制作案例,网页的相关名词有哪些?  如何在云主机上快速搭建网站?  如何挑选优质建站一级代理提升网站排名?  制作公司内部网站有哪些,内网如何建网站?  网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  定制建站模板如何实现SEO优化与智能系统配置?18字教程  相册网站制作软件,图片上的网址怎么复制?  如何制作一个表白网站视频,关于勇敢表白的小标题?  微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  网站制作软件有哪些,制图软件有哪些?  如何在IIS管理器中快速创建并配置网站?  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  制作网页的网站有哪些,电脑上怎么做网页?  焦点电影公司作品,电影焦点结局是什么?  h5网站制作工具有哪些,h5页面制作工具有哪些?  如何自定义建站之星网站的导航菜单样式?  如何用wdcp快速搭建高效网站?  最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  如何在建站之星绑定自定义域名?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  如何快速打造个性化非模板自助建站?  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  如何制作网站标识牌,动态网站如何制作(教程)?  如何用5美元大硬盘VPS安全高效搭建个人网站?  制作营销网站公司,淘特是干什么用的?  建站主机选哪种环境更利于SEO优化?  网站制作知乎推荐,想做自己的网站用什么工具比较好?  制作企业网站建设方案,怎样建设一个公司网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。