今天实现了Android利用jxl通过已有xls文件导出excel文件,并将数据添加至新的excel文件
效果图如下:
相关代码如下:(其中Look_excel为界面布局的一个按钮)
1 public class InformationActivity extends AppCompatActivity { 2 private List<persion> persionList=new ArrayList<persion>(); 3 //查看excel表按钮 4 private Button mLookExcel; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_information); 9 Dao dao=new Dao(InformationActivity.this); 10 persionList=dao.Query(username);//此处是Sqlite查询语句,通过username查询,此处不再作展示 11 //查看excel表格 12 mLookExcel=findViewById(R.id.Look_excel); 13 mLookExcel.setOnClickListener(new View.OnClickListener() { 14 @Override 15 public void onClick(View v) { 16 AssetManager assetManager = getAssets(); 17 WritableWorkbook wwb=null; 18 try{ 19 Workbook wb=Workbook.getWorkbook(assetManager.open("体温.xls")); 20 Sheet sheet=wb.getSheet(0); 21 wwb=Workbook.createWorkbook(new File("/sdcard/ExportExcel/mine.xls"),wb); 22 Toast.makeText(InformationActivity.this,"体温表复制成功",Toast.LENGTH_SHORT).show(); 23 //获取一个sheet 24 WritableSheet writableSheet=wwb.getSheet(0); 25 26 Label lable1 = new Label(1, 1, "username"); 27 Label lable2 = new Label(5, 1, "userid"); 28 Label lable3 = new Label(1, 2, "userclass"); 29 Label lable4 = new Label(5, 2, "userphone"); 30 writableSheet.addCell(lable1); 31 writableSheet.addCell(lable2); 32 writableSheet.addCell(lable3); 33 writableSheet.addCell(lable4); 34 35 int i=0; 36 for(persion list:persionList){ 37 Label lable5 = new Label(0, 5+i, list.date); 38 Label lable6 = new Label(1, 5+i, list.time); 39 Label lable7 = new Label(2, 5+i, list.temperature); 40 Label lable8 = new Label(3, 5+i, list.instruction); 41 Label lable9 = new Label(5, 5+i, list.local); 42 writableSheet.addCell(lable5); 43 writableSheet.addCell(lable6); 44 writableSheet.addCell(lable7); 45 writableSheet.addCell(lable8); 46 writableSheet.addCell(lable9); 47 i++; 48 } 49 wwb.write(); 50 wwb.close(); 51 wb.close(); 52 }catch (Exception e){ 53 e.printStackTrace(); 54 } 55 } 56 }); 57 } 58 }