由于近期项目的需求,需要导出excel文件并分享给微信好友。基本上查询网上好多方案,单都没解决到位,调试的时候都有问题。
关键代码
private void exportExcel() {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
//设置列宽
sheet.setColumnWidth(0,2000);
sheet.setColumnWidth(1,3000);
sheet.setColumnWidth(2,3000);
sheet.setColumnWidth(3,3000);
sheet.setColumnWidth(4,5000);
sheet.setColumnWidth(5,7000);
//=================================定义表头属性===============================================
Font font = workbook.createFont(); // 生成字体格式设置对象
font.setFontName("黑体"); // 设置字体黑体
font.setBold(true); // 字体加粗
font.setFontHeightInPoints(( short ) 16 ); // 设置字体大小
font.setColor(Font.COLOR_NORMAL);//字体颜色
CellStyle cellStyle = workbook.createCellStyle(); // 生成行格式设置对象
cellStyle.setBorderBottom(BorderStyle.THIN);// 下边框
cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
cellStyle.setBorderRight(BorderStyle.THIN);// 右边框
cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
cellStyle.setFont(font);
//=================================定义内容属性===============================================
Font txtContent = workbook.createFont(); // 生成字体格式设置对象
txtContent.setFontName("黑体"); // 设置字体黑体
txtContent.setBold(false); // 字体加粗
txtContent.setFontHeightInPoints(( short ) 12 ); // 设置字体大小
txtContent.setColor(Font.COLOR_NORMAL);//字体颜色
CellStyle cellStyleContent = workbook.createCellStyle(); // 生成行格式设置对象
cellStyleContent.setBorderBottom(BorderStyle.THIN);// 下边框
cellStyleContent.setBorderLeft(BorderStyle.THIN);// 左边框
cellStyleContent.setBorderRight(BorderStyle.THIN);// 右边框
cellStyleContent.setBorderTop(BorderStyle.THIN);// 上边框
cellStyleContent.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
cellStyleContent.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
cellStyleContent.setFont(txtContent);
//Font font = workbook.createFont();
font.setBold(true);
// 创建CellStyle并设置字体和对齐方式
CellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
for (int k=0;k<list.size()+1;k++) {
Row row = sheet.createRow(k);
if (k==0){
Cell cell0=row.createCell(0);
Cell cell1=row.createCell(1);
Cell cell2=row.createCell(2);
Cell cell3=row.createCell(3);
Cell cell4=row.createCell(4);
row.setHeight((short) 500);
cell0.setCellValue("姓名");
cell1.setCellValue("类型");
cell2.setCellValue("事由");
cell3.setCellValue("金额");
cell4.setCellValue("日期");
cell0.setCellStyle(cellStyle);
cell1.setCellStyle(cellStyle);
cell2.setCellStyle(cellStyle);
cell3.setCellStyle(cellStyle);
cell4.setCellStyle(cellStyle);
}else{
Cell cell0=row.createCell(0);
Cell cell1=row.createCell(1);
Cell cell2=row.createCell(2);
Cell cell3=row.createCell(3);
Cell cell4=row.createCell(4);
row.setHeight((short) 500);
cell0.setCellValue(list.get(k-1).getName());
cell1.setCellValue(list.get(k-1).getType());
cell2.setCellValue(list.get(k-1).getEvent());
cell3.setCellValue(list.get(k-1).getAmount());
cell4.setCellValue(list.get(k-1).getRemembertime());
cell0.setCellStyle(cellStyleContent);
cell1.setCellStyle(cellStyleContent);
cell2.setCellStyle(cellStyleContent);
cell3.setCellStyle(cellStyleContent);
cell4.setCellStyle(cellStyleContent);
}
}
try {
String fileName = Excel_File_Name;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
values.put(MediaStore.Downloads.MIME_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
Uri uri = getActivity().getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
if (uri != null) {
OutputStream outputStream = getActivity().getContentResolver().openOutputStream(uri);
if (outputStream != null) {
workbook.write(outputStream);
outputStream.close();
//Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
ToastUtils.getInstance().showToast("导出成功");
// 分享Excel文件到微信
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
intent.putExtra(Intent.EXTRA_STREAM, uri); // 设置文件路径为Uri
startActivity(intent); // 启动分享意图
}
}
} else {
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + fileName;
OutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
outputStream.close();
//Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
ToastUtils.getInstance().showToast("导出成功");
// 分享Excel文件到微信
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
intent.putExtra(Intent.EXTRA_STREAM, filePath); // 设置文件路径为Uri
startActivity(intent); // 启动分享意图
}
} catch (IOException e) {
e.printStackTrace();
}
}