我尝试使用以下代码在android / java中创建和发送excel表,但是我无法完成将ByteArrayDataSource添加到intent中的正确语法的代码:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Your excel sheet");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello! I am sending the excel sheet");
Workbook xlsFile = new HSSFWorkbook();
CreationHelper helper = xlsFile.getCreationHelper();
Sheet sheet1 = xlsFile.createSheet("New worksheet");
/* fill excel sheet */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
xlsFile.write(bos); // write excel data to a byte array
DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/vnd.ms-excel");
bos.close();
sharingIntent.putExtra(Intent.EXTRA_STREAM, ??? /* how to add the ByteArrayDataSource here?? */);
} catch (IOException e) {
e.printStackTrace();
}
startActivity(Intent.createChooser(sharingIntent, "Send via"));
如何附加excel表而不将其存储在SD卡上?
谢谢!
祝你好运, 尔根
更新
首先通过存储到SD卡完成它:
try {
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "file.xls");
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xlsFile.write(bos);
fo.write(bos.toByteArray());
bos.close();
fo.flush();
fo.close();
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + file.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
如果有人知道某种方法而不存储它,请告诉我!谢谢!