android-studio - 如何从 EditText 编写 Android 外部 Excel 文件?
问题描述
如何将编辑文本中的数据写入android中的excel文件?我想将数据从editText绕过到excel文件中,而不是从存储中打开它。我能知道怎么做吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
scanBtn = (Button) findViewById(R.id.BtnScn);
resultSC = (TextView) findViewById(R.id.SC_text);
ExelWrite = (Button) findViewById(R.id.ExelWrite);
SetTemp = (EditText) findViewById(R.id.SetTemp);
scanBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View view){
startActivity(new Intent(getApplicationContext(),ScanCodeActivity.class));
}
});
ExelWrite.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ExelWrite:
saveExcelFile(this,"Staff_Temperature.xls");
break;
}
}
private static boolean saveExcelFile(MainActivity context, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.e(TAG, "Storage not available or read only");
return false;
}
boolean success = false;
这是我为 excel 创建工作簿的地方
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row'
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("RiSE");
// Generate column headings
Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue("Staff Credential");
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue("Staff Temperature");
c.setCellStyle(cs);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
这是我为文件创建路径的地方。我想它应该在这里,但我不知道如何称呼它
// Create a path where we will place our List of objects on external storage
File file = new File(context.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
解决方案
推荐阅读
- crystal-reports - 如何查看和禁用业务对象企业版本 2 中的自动水晶报表?
- ruby-on-rails - Rails has_many 模型验证
- javascript - 具有大值的循环中的 JavaScript 问题
- c# - C# 获取具有特定属性参数的所有类的列表
- macos - 在mac上降级perl版本
- javascript - 对此的 Javascript 引用不起作用
- ms-access - 当我在 ACCESS 中运行代码 vba 时消除#Error
- html - CSS 图像的角落不会始终如一地四舍五入。有时他们会,有时他们不会
- rxjs - 使用 rxjs 进行长轮询
- java - 使用 Spring Boot 的 Swing 应用程序