关于eclipse jar导入以及Android的excel的使用(小白)

妈呀这玩意浪费了半天琢磨,从Qt过渡Android还真不容易。各种傻瓜式的错误。

使用EXCEL之前需要往工程添加jxl.jar(excel)的API 导入方法 项目工程目录libs,直接复制jxl.jar到这里

其它地方转来的example:

package com.example.xl;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

//详细参考     http://www.ibm.com/developerworks/cn/java/l-javaExcel/
public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView textView = (TextView) findViewById(R.id.myTextView02); // mTextView01
																		// =
																		// (TextView)
																		// findViewById(R.id.myTextView01);
		String path = "mnt/sdcard/test.xls";
		writeExcel(path);
		// textView.setText(readExcel(path,5,5)+readExcel(path,10,10));
	}

	public void writeExcel(String fileName) {
		WritableWorkbook wwb = null;
		try {
			// 创建一个可写入的工作薄(Workbook)对象
			wwb = Workbook.createWorkbook(new File(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (wwb != null) {
			// 第一个参数是工作表的名称,第二个是工作表在工作薄中的位置
			WritableSheet ws = wwb.createSheet("sheet1", 0);
			// 在指定单元格插入数据
			Label lbl1 = new Label(5, 5, "Excel");
			Label bll2 = new Label(10, 10, "的操作");
			try {
				ws.addCell(lbl1);
				ws.addCell(bll2);
			} catch (RowsExceededException e1) {
				e1.printStackTrace();
			} catch (WriteException e1) {
				e1.printStackTrace();
			}
			try {
				// 从内存中写入文件中
				wwb.write();
				wwb.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			}
		}
	}

	public String readExcel(String path, int x, int y) {
		String content = "";
		try {
			Workbook book = Workbook.getWorkbook(new File(path));
			Sheet sheet = book.getSheet(0);
			// 得到x行y列所在单元格的内容
			String cellStr = sheet.getRow(x)[y].getContents();
			content = cellStr;

		} catch (BiffException e) {
			content = "";
			e.printStackTrace();
		} catch (IOException e) {
			content = "";
			e.printStackTrace();
		}
		return content;

	}

}
activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:id="@+id/myTextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值