//获取zhidaoandroid根目录public String getSDPath(){ F" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>android</em>环境下,在项目的根目录下如何新建文件xls文件...

<em>android</em>环境下,在项目的根目录下如何新建文件xls文件...

作者:乔山办公网日期:

返回目录:excel表格制作


java可以用poi

//获取zhidaoandroid根目录
public String getSDPath(){ 
File sdDir = null; 
boolean sdCardExist = Environment.getExternalStorageState() 
.equals(Android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 
if (sdCardExist) { 
sdDir = Environment.getExternalStorageDirectory();//获取跟目录 

return sdDir.toString(); 
}

步骤:e68a84e799bee5baa6e997aee7ad94338

1、AndroidManifest.xml中配置SD卡写权限

 <!-- 往SDCard写入数据权限 --> 
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

2、java代码往SD卡写入图片信息

public static void saveBitmapToFile(Bitmap bitmap, String _file)  
        throws IOException {BufferedOutputStream os = null;  
    try {  
        File file = new File(_file);  //新建图片
        int end = _file.lastIndexOf(File.separator);  
        String _filePath = _file.substring(0, end); //获取图片路径 
        File filePath = new File(_filePath);  
        if (!filePath.exists()) {  //如果文件夹不存在,创建文件夹
            filePath.mkdirs();  
        }  
        file.createNewFile();  //创建图片文件
        os = new BufferedOutputStream(new FileOutputStream(file));  
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);  //图片存成png格式。
    } finally {  
        if (os != null) {  
            try {  
                os.close();  //关闭流
            } catch (IOException e) {  
                Log.e(TAG_ERROR, e.getMessage(), e);  
            }  
        }  
    }  
}


1、一般情况下我们会用第三方的jar包来帮助实现,比如 jxl.jar , poi.jar
点击下载开发需要的jar包

2、开发的时候需要注意加上读写权限,尤其在Android 6.0 的时候需要动态去申请读写的权限

[java] view plain copy
<span style="font-size:12px;">if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST);
} else {

saveToExcel.writeToExcel(name,sex,phone,address);
}</span>

权限回调

[java] view plain copy
<span style="font-size:12px;">@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if (requestCode == MY_PERMISSIONS_REQUEST) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveToExcel.writeToExcel(name,sex,phone,address);
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}</span>

3、指定Excel文件存放的文件夹,并为文件取名

[java] view plain copy
<span style="font-size:12px;">public static String getExcelDir() {
// SD卡指定文件夹
String sdcardPath = Environment.getExternalStorageDirectory()
.toString();
File dir = new File(sdcardPath + File.separator + "Excel"
+ File.separator + "Person");

if (dir.exists()) {
return dir.toString();

} else {
dir.mkdirs();
Log.e("BAG", "保存路径不存在,");
return dir.toString();
}
</span>
在activity 中进行调用 :

String excelPath = getExcelDir()+ File.separator+"demo.xls";

4、将数据存入e69da5e6ba90e799bee5baa6330到Excel表中,在这里写了一个工具类saveToExcel(),具体代码如下

[java] view plain copy
<span style="font-size:12px;"> public class SaveToExcelUtil {
private WritableWorkbook wwb;
private String excelPath;
private File excelFile;
private Activity activity;

public SaveToExcelUtil(Activity activity, String excelPath) {
this.excelPath = excelPath;
this.activity = activity;
excelFile = new File(excelPath);
createExcel(excelFile);

}

// 创建excel表.
public void createExcel(File file) {
WritableSheet ws = null;
try {
if (!file.exists()) {
wwb = Workbook.createWorkbook(file);

ws = wwb.createSheet("sheet1", 0);

// 在指定单元格插入数据
Label lbl1 = new Label(0, 0, "姓名");
Label lbl2 = new Label(1, 0, "性别");
Label lbl3 = new Label(2, 0, "电话");
Label lbl4 = new Label(3, 0, "地址");

ws.addCell(lbl1);
ws.addCell(lbl2);
ws.addCell(lbl3);
ws.addCell(lbl4);

// 从内存中写入文件中
wwb.write();
wwb.close();
}

} catch (Exception e) {
e.printStackTrace();
}
}

//将数据存入到Excel表中
public void writeToExcel(Object... args) {

try {
Workbook oldWwb = Workbook.getWorkbook(excelFile);
wwb = Workbook.createWorkbook(excelFile, oldWwb);
WritableSheet ws = wwb.getSheet(0);
// 当前行数
int row = ws.getRows();
Label lab1 = new Label(0, row, args[0] + "");
Label lab2 = new Label(1, row, args[1] + "");
Label lab3 = new Label(2, row, args[2] + "");
Label lab4 = new Label(3, row, args[3] + "");
ws.addCell(lab1);
ws.addCell(lab2);
ws.addCell(lab3);
ws.addCell(lab4);

// 从内存中写入文件中,只能刷一次.
wwb.write();
wwb.close();
Toast.makeText(activity, "保存成功", Toast.LENGTH_SHORT).show();

} catch (Exception e) {
e.printStackTrace();
}
}
}</span>

相关阅读

关键词不能为空
极力推荐
  • EXCEL变成了只读文件该怎么办?

  • 以excel2016为例:百1、首先在打开的excel表格中弹出只读文件的提示,如果点击“是”。2、在表格中的文件名会显示“只读”的字样。3、并且在度其中进行编辑后,点击保存按钮是无法进

ppt怎么做_excel表格制作_office365_word文档_365办公网