Android操作excel功能实例代码

学习app对excel的读写控制

成都一家集口碑和实力的网站建设服务商,拥有专业的企业建站团队和靠谱的建站技术,十年企业及个人网站建设经验 ,为成都1000+客户提供网页设计制作,网站开发,企业网站制作建设等服务,包括成都营销型网站建设,品牌网站制作,同时也为不同行业的客户提供成都网站建设、网站设计的服务,包括成都电商型网站制作建设,装修行业网站制作建设,传统机械行业网站建设,传统农业行业网站制作建设。在成都做网站,选网站制作建设服务商就选成都创新互联公司

1.界面设计

<?xml version="1.0" encoding="utf-8"?>

  
    
    
    
      
      
    
      
      
        
<?xml version="1.0" encoding="utf-8"?>

  
    
      
      
      
      
      
    
  

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 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();
    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 
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
{
  private List itemsData;
  private Context mContext;
  private LayoutInflater inflater;
  public EngineerRecycleViewAdapter(Context context, List 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功能实例代码
本文来源:http://scjbc.cn/article/jpcedg.html

其他资讯