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 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;
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 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;
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 自动生成的方法存根
}
});
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);
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
}
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();
}
}
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="插入"
/>
<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>
android:layout_marginTop="10dp"
android:id="@+id/listView_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
0 0
- android下读取excel信息,插入信息至excel中
- Java读取Excel表格信息。
- Excel中读取股票信息的自定义函数
- 从txt读取信息然后保存到excel中示例
- php读取excel文件的数据信息
- poi读取损坏excel的异常信息
- Workbook解析excel文件 读取信息
- 身份证信息读取并写入Excel模板
- Java读取excel表格的信息
- 读取excel文件中的信息实例1
- 读取excel文件信息实例2
- B/S下导出系统通讯录信息到Excel中
- 读取Excel中的信息 至DataSet,将DataSet中的资料导出到Excel
- Qt实现读取Excel表,并将读取的信息写进word文档中
- Excel中取得合并单元格相关信息
- 实现信息在excel中输出
- c#写入日期信息到excel中
- Android中读取短信信息
- 【IMWeb训练营作业】我的第一次作业---todoList
- GAN相关工作介绍
- 函数指针实例2
- Swift の Attribute
- setTimeout闭包问题
- android下读取excel信息,插入信息至excel中
- Spring-boot进行热部署
- STM32串口下载
- 汉诺塔系列2 (sdut oj)
- linux 结构目录
- todos
- [转]基于EasyPlayer和EasyPusher实现安卓平台的双向视频对讲
- 【LeetCode】Swap Nodes in Pairs
- python字符串拼接