Android 生成Excel文件保存到本地

本文用来记录在安卓中生成Excel文件并保存到本地操作,在网上找了好久,终于找到一个可以用的,虽然代码已经很老的,但亲测可用!

项目地址:https://github.com/wanganan/AndroidExcel

可以下载下来修改直接用,该项目主要是依赖一个叫jxl.jar的包,导到项目中libs文件下加即可。

关键代码:

java 复制代码
public class ExcelUtil {
	//内存地址
	public static String root = Environment.getExternalStorageDirectory()
			.getPath();

	public static void writeExcel(Context context, List<Order> exportOrder,
			String fileName) throws Exception {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&&getAvailableStorage()>1000000) {
			Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show();
			return;
		}
		String[] title = { "订单", "店名", "电话", "地址" };
		File file;
//		File dir = new File(context.getExternalFilesDir(null).getPath());
		File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
		file = new File(dir, fileName + ".xls");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// 创建Excel工作表
		WritableWorkbook wwb;
		OutputStream os = new FileOutputStream(file);
		wwb = Workbook.createWorkbook(os);
		// 添加第一个工作表并设置第一个Sheet的名字
		WritableSheet sheet = wwb.createSheet("订单", 0);
		Label label;
		for (int i = 0; i < title.length; i++) {
			// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
						// 在Label对象的子对象中指明单元格的位置和内容
			label = new Label(i, 0, title[i], getHeader());
			// 将定义好的单元格添加到工作表中
			sheet.addCell(label);
		}

		for (int i = 0; i < exportOrder.size(); i++) {
			Order order = exportOrder.get(i);

			Label orderNum = new Label(0, i + 1, order.id);
			Label restaurant = new Label(1, i + 1, order.restName);
			Label nameLabel = new Label(2,i+1,order.restPhone);
			Label address = new Label(3, i + 1, order.receiverAddr);
			
			sheet.addCell(orderNum);
			sheet.addCell(restaurant);
			sheet.addCell(nameLabel);
			sheet.addCell(address);
			Toast.makeText(context, "写入成功", Toast.LENGTH_LONG).show();
			
		}
		// 写入数据
		wwb.write();
		// 关闭文件
		wwb.close();
	}

	public static WritableCellFormat getHeader() {
		WritableFont font = new WritableFont(WritableFont.TIMES, 10,
				WritableFont.BOLD);// 定义字体
		try {
			font.setColour(Colour.BLUE);// 蓝色字体
		} catch (WriteException e1) {
			e1.printStackTrace();
		}
		WritableCellFormat format = new WritableCellFormat(font);
		try {
			format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中
			format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中
			// format.setBorder(Border.ALL, BorderLineStyle.THIN,
			// Colour.BLACK);// 黑色边框
			// format.setBackground(Colour.YELLOW);// 黄色背景
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return format;
	}
	
	/** 获取SD可用容量 */
	private static long getAvailableStorage() {

		StatFs statFs = new StatFs(root);
		long blockSize = statFs.getBlockSize();
		long availableBlocks = statFs.getAvailableBlocks();
		long availableSize = blockSize * availableBlocks;
		// Formatter.formatFileSize(context, availableSize);
		return availableSize;
	}
}

代码很简单没什么解释的,关键点就是创建WorkbookSheet,和每一个表格的Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z,及表格样式。

需要注意下原项目传的fileName格式有问题,直接用的话会导致文件生成不成功,记得修改一下!

相关推荐
xxjj998a3 分钟前
Laravel4.x核心特性全解析
android·mysql·laravel
JoshRen31 分钟前
2026教程:在Android Termux中集成Gemini 3镜像站实现移动端文档自动处理与摘要生成(附国内免费方案)
android
诸神黄昏EX1 小时前
Android Google KEY
android
一起搞IT吧1 小时前
Android性能系列专题理论之十一:block IO问题分析思路
android·嵌入式硬件·智能手机·性能优化
小妖6662 小时前
怎么用 tauri 创建编译 android 应用程序
android·tauri
鸟儿不吃草3 小时前
安卓实现左右布局聊天界面
android·开发语言·python
xxjj998a5 小时前
Laravel 1.x:PHP框架的原始魅力
android·php·laravel
formula100005 小时前
在iOS/安卓上远程连接任何 Agent!Claude、Codex、Copilot、Gemini、OpenCode 等
android·copilot
该用户可能存在5 小时前
Blbl-android 更新至 v0.1.24,体验更流畅、更稳定
android·哔哩哔哩·电视app·androidtv·bbll·blbl·bilibilitv
lKWO OMET5 小时前
mysql之字符串函数
android·数据库·mysql