最近项目中需要用到在excel中插入图片的功能。百度了好久,终于找到了。感谢某位大佬,比较懒,就不贴谁了。直接上代码:
首先下载jxl.jar,这个网上有。
1、布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/createExcel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="createExcel"/> <Button android:id="@+id/readExcel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="readExcel"/> <Button android:id="@+id/updateExcel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="updateExcel"/> <Button android:id="@+id/writeExcel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="writeExcel"/> </LinearLayout>
2、MainActivity代码:
package com.example.administrator.myexceldemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableCell; import jxl.write.WritableImage; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends AppCompatActivity { String path = "mnt/sdcard/test.xls"; private Button createExcel; private Button readExcel; private Button updateExcel; private Button writeExcel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mOnClickListener(); } private void initView() { createExcel = (Button) findViewById(R.id.createExcel); readExcel = (Button) findViewById(R.id.readExcel); updateExcel = (Button) findViewById(R.id.updateExcel); writeExcel = (Button) findViewById(R.id.writeExcel); } private void mOnClickListener() { createExcel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { createExcel(); } }); readExcel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { readExcel(); } }); updateExcel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { updateExcel(path); } }); writeExcel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { writeExcel(path); } }); } public void readExcel() { try { InputStream is = new FileInputStream("mnt/sdcard/test.xls"); Workbook book = Workbook .getWorkbook(new File("mnt/sdcard/test.xls")); book.getNumberOfSheets(); // 获得第一个工作表对象 Sheet sheet = book.getSheet(0); int Rows = sheet.getRows(); int Cols = sheet.getColumns(); System.out.println("当前工作表的名字:" + sheet.getName()); System.out.println("总行数:" + Rows); System.out.println("总列数:" + Cols); // 得到第一列第一行的单元格 Cell cell1 = sheet.getCell(0, 0); String result = cell1.getContents(); System.out.println(result); book.close(); } catch (Exception e) { System.out.println(e); } } public void createExcel() { try { // 创建或打开Excel文件,并插入数据 WritableWorkbook book = Workbook.createWorkbook(new File("mnt/sdcard/test.xls")); // 生成工作表,参数0表示这是第一页 WritableSheet sheet1 = book.createSheet("第一页", 0); WritableSheet sheet2 = book.createSheet("第三页", 2); // 在Label对象的构造函数中,元格位置是第一列第一行(0,0)以及单元格内容为test Label label = new Label(0, 0, "test"); // 将定义好的单元格添加到工作表中 sheet1.addCell(label); /* * 生成一个保存数字的单元格.必须使用Number的完整包路径,否则有语法歧义 */ jxl.write.Number number = new jxl.write.Number(1, 0, 555.12541); sheet2.addCell(number); // 写入数据并关闭文件 book.write(); book.close(); } catch (Exception e) { System.out.println(e); } } /** * jxl暂时不提供修改已经存在的数据表,这里通过一个小办法来达到这个目的,不适合大型数据更新! 这里是通过覆盖原文件来更新的. * * @param filePath */ public void updateExcel(String filePath) { try { Workbook rwb = Workbook.getWorkbook(new File(filePath)); WritableWorkbook wwb = Workbook.createWorkbook(new File( "d:/new.xls"), rwb);// copy WritableSheet ws = wwb.getSheet(0); WritableCell wc = ws.getWritableCell(0, 0); // 判断单元格的类型,做出相应的转换 Label label = (Label) wc; label.setString("更改"); wwb.write(); wwb.close(); rwb.close(); } catch (Exception e) { e.printStackTrace(); } } public static void writeExcel(String filePath) { try { // 创建工作薄 WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath)); // 创建工作表 WritableSheet ws = wwb.createSheet("Sheet1", 0); // 添加图片(注意此处jxl暂时只支持png格式的图片) // 0,1分别代表x,y 2,5代表宽和高占的单元格数 ws.addImage(new WritableImage(5, 5, 2, 5, new File( "/storage/emulated/0/Tencent/QQ_Images/-1a12814ec0ba85d6.png"))); wwb.write(); wwb.close(); } catch (Exception e) { System.out.println(e.toString()); } } }
代码很简单,有需要的自行复制。
后来又看到某位大佬的博客:Android通过POI架包生成Excle表格,方法更完整
https://blog.csdn.net/hexingen/article/details/64129592