
学习app对excel的读写控制
1.界面设计
@H_502_6@<?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>@H_502_6@
<?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. 代码设计
@H_502_6@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,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]; } }}@H_502_6@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
@H_502_6@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功能实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)