android下读取excel信息,插入信息至excel中

来源:互联网 发布:golang kafka 编辑:程序博客网 时间:2024/05/19 09:12
//要事先加入库jxl.jar


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableWorkbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ScanResultActivity extends Activity{
 public ArrayList<HashMap<String,String>> arrayList;
 private ListView listView;
 private WritableWorkbook wwbWorkbook;
 private String excelPath;
 private File excelFile;
 private static Toast mToast;
 private TextView totalbar_title;
 private ImageView imgview_back,imgview_reset;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO 自动生成的方法存根
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);// 默认没有标题
  setContentView(R.layout.scanresult_infomation);
  listView=(ListView)super.findViewById(R.id.listView_scan);
  arrayList=new ArrayList<HashMap<String,String>>();
  mToast = Toast.makeText(ScanResultActivity.this, "", Toast.LENGTH_SHORT);
  excelPath=getExcelDir()+File.separator+"scandata.xls";//存写的数据库必须为2000或2003版本的
  excelFile=new File(excelPath);
  totalbar_title=(TextView)super.findViewById(R.id.topbar_title);
  totalbar_title.setText("扫描结果");
  imgview_back=(ImageView)super.findViewById(R.id.img_back);
  imgview_reset=(ImageView)super.findViewById(R.id.img_reset);
  
  imgview_back.setVisibility(View.VISIBLE);
  imgview_back.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent=null;
    intent = new Intent(ScanResultActivity.this, MainActivity.class);
    startActivity(intent);
   
   }
  });
  imgview_reset.setVisibility(View.VISIBLE);
  imgview_reset.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO 自动生成的方法存根
    
   }
  });
  
  InputStream inputStream=null;
  try {
   inputStream=new FileInputStream(excelPath);
   Workbook wb=Workbook.getWorkbook(inputStream);
   Sheet sheet=wb.getSheet(0);
   int row=sheet.getRows();
   HashMap<String, String>hashMap;
   Cell celltime1=sheet.getCell(0,0);
   Cell cellscaninfo1=sheet.getCell(1,0);
   hashMap=new HashMap<String, String>();
   hashMap.put("TIME",celltime1.getContents());
   hashMap.put("SCANINFO", cellscaninfo1.getContents());
   arrayList.add(hashMap);
   for (int i = row-1; i >0; i--) {
    Cell celltime=sheet.getCell(0,i);
    Cell cellscaninfo=sheet.getCell(1,i);
    hashMap=new HashMap<String, String>();
    hashMap.put("TIME",celltime.getContents());
    hashMap.put("SCANINFO", cellscaninfo.getContents());
    arrayList.add(hashMap);
   }
   SimpleAdapter simpleAdapter=new SimpleAdapter(this, arrayList, R.layout.scan_listview_information,
     new String[]{"TIME","SCANINFO"},new int[]{R.id.tv_scantime,R.id.tv_scaninfo});
   listView.setAdapter(simpleAdapter);
  } catch (Exception e) {
   // TODO: handle exception
  }
 }

 //获取excel文件夹
 private String getExcelDir() {
  String sdcardPath=Environment.getExternalStorageDirectory().toString();
  File dir=new File(sdcardPath+File.separator+"AgvControlExcel");
  if (dir.exists()) {
   showTip("保存路径存在");  
   return dir.toString();
  }
  else {
   dir.mkdirs();
   showTip("保存路径不存在"+sdcardPath+"   "+dir.toString());
   Log.e("error", "保存路径不存在"+sdcardPath+"   "+dir.toString());
   return dir.toString();
  }
 }

 public static void showTip(String str) {
  mToast.setText(str);
  mToast.show();
 }
}

layout中 scanresult_information.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_add"
        android:text="插入"
        />
    <ListView
        android:layout_marginTop="10dp"
        android:id="@+id/listView_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

0 0