android开发之输出excel文档实例
程序员文章站
2023-12-02 11:02:52
1,添加poi的jar包
2,建立workbook,并生成sheet,row以及单元格cell
private workbook workbook;
private sheet sheet;...
1,添加poi的jar包
2,建立workbook,并生成sheet,row以及单元格cell
private workbook workbook; private sheet sheet;
//创建工作簿 workbook = new hssfworkbook(); //创建execl中的一个表 sheet= workbook.createsheet(); //设置第一列的宽度是9个字符宽度 sheet.setcolumnwidth(1,10*256); sheet.setcolumnwidth(2,4*256); //创建第一行 row row=sheet.createrow(0); // 设置第一行:高为50个点 row.setheightinpoints(50); //创建第一行中第一单元格 row.createcell(0).setcellvalue("时间"); row.createcell(1).setcellvalue("rssi值");
//随着数据的不断传递,不断增加excel的行 private void createrows(int i,string date,int rssi){ //创建第二行 row row=sheet.createrow(i); // 设置第二行:高为50个点 row.setheightinpoints(50); //创建第一行中第一单元格 row.createcell(0).setcellvalue(date); row.createcell(1).setcellvalue(rssi); }
3,输出到手机内存中
writefile(environment.getexternalstoragedirectory() + "/" + getfilename("12:3b:6a:1a:95:56"));
private void writefile( string filename){ fileoutputstream outputstream=null; try{ outputstream=new fileoutputstream(filename); workbook.write(outputstream); }catch (exception e){ e.printstacktrace(); }finally { try{ if(outputstream!=null){ outputstream.close(); } if(workbook!=null){ workbook.close(); } }catch (exception e){ e.printstacktrace(); } } }
//去除蓝牙设备地址中的冒号,用来给保存的文档命名 private static string getfilename(string address) { char[] addresschar = address.tochararray(); char[] addresscharresult = new char[]{addresschar[0], addresschar[1], addresschar[3], addresschar[4], addresschar[6], addresschar[7], addresschar[9], addresschar[10], addresschar[12], addresschar[13], addresschar[15], addresschar[16]}; string filename = string.valueof(addresscharresult); return filename + ".xls"; }