当前位置:   article > 正文

android实战项目开发七---完美解决excel文件导出功能,并分享到微信

android实战项目开发七---完美解决excel文件导出功能,并分享到微信

由于近期项目的需求,需要导出excel文件并分享给微信好友。基本上查询网上好多方案,单都没解决到位,调试的时候都有问题。

关键代码

  1. private void exportExcel() {
  2. Workbook workbook = new XSSFWorkbook();
  3. Sheet sheet = workbook.createSheet("Sheet1");
  4. //设置列宽
  5. sheet.setColumnWidth(0,2000);
  6. sheet.setColumnWidth(1,3000);
  7. sheet.setColumnWidth(2,3000);
  8. sheet.setColumnWidth(3,3000);
  9. sheet.setColumnWidth(4,5000);
  10. sheet.setColumnWidth(5,7000);
  11. //=================================定义表头属性===============================================
  12. Font font = workbook.createFont(); // 生成字体格式设置对象
  13. font.setFontName("黑体"); // 设置字体黑体
  14. font.setBold(true); // 字体加粗
  15. font.setFontHeightInPoints(( short ) 16 ); // 设置字体大小
  16. font.setColor(Font.COLOR_NORMAL);//字体颜色
  17. CellStyle cellStyle = workbook.createCellStyle(); // 生成行格式设置对象
  18. cellStyle.setBorderBottom(BorderStyle.THIN);// 下边框
  19. cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
  20. cellStyle.setBorderRight(BorderStyle.THIN);// 右边框
  21. cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
  22. cellStyle.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
  23. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
  24. cellStyle.setFont(font);
  25. //=================================定义内容属性===============================================
  26. Font txtContent = workbook.createFont(); // 生成字体格式设置对象
  27. txtContent.setFontName("黑体"); // 设置字体黑体
  28. txtContent.setBold(false); // 字体加粗
  29. txtContent.setFontHeightInPoints(( short ) 12 ); // 设置字体大小
  30. txtContent.setColor(Font.COLOR_NORMAL);//字体颜色
  31. CellStyle cellStyleContent = workbook.createCellStyle(); // 生成行格式设置对象
  32. cellStyleContent.setBorderBottom(BorderStyle.THIN);// 下边框
  33. cellStyleContent.setBorderLeft(BorderStyle.THIN);// 左边框
  34. cellStyleContent.setBorderRight(BorderStyle.THIN);// 右边框
  35. cellStyleContent.setBorderTop(BorderStyle.THIN);// 上边框
  36. cellStyleContent.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
  37. cellStyleContent.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
  38. cellStyleContent.setFont(txtContent);
  39. //Font font = workbook.createFont();
  40. font.setBold(true);
  41. // 创建CellStyle并设置字体和对齐方式
  42. CellStyle style = workbook.createCellStyle();
  43. style.setFont(font);
  44. style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
  45. style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
  46. for (int k=0;k<list.size()+1;k++) {
  47. Row row = sheet.createRow(k);
  48. if (k==0){
  49. Cell cell0=row.createCell(0);
  50. Cell cell1=row.createCell(1);
  51. Cell cell2=row.createCell(2);
  52. Cell cell3=row.createCell(3);
  53. Cell cell4=row.createCell(4);
  54. row.setHeight((short) 500);
  55. cell0.setCellValue("姓名");
  56. cell1.setCellValue("类型");
  57. cell2.setCellValue("事由");
  58. cell3.setCellValue("金额");
  59. cell4.setCellValue("日期");
  60. cell0.setCellStyle(cellStyle);
  61. cell1.setCellStyle(cellStyle);
  62. cell2.setCellStyle(cellStyle);
  63. cell3.setCellStyle(cellStyle);
  64. cell4.setCellStyle(cellStyle);
  65. }else{
  66. Cell cell0=row.createCell(0);
  67. Cell cell1=row.createCell(1);
  68. Cell cell2=row.createCell(2);
  69. Cell cell3=row.createCell(3);
  70. Cell cell4=row.createCell(4);
  71. row.setHeight((short) 500);
  72. cell0.setCellValue(list.get(k-1).getName());
  73. cell1.setCellValue(list.get(k-1).getType());
  74. cell2.setCellValue(list.get(k-1).getEvent());
  75. cell3.setCellValue(list.get(k-1).getAmount());
  76. cell4.setCellValue(list.get(k-1).getRemembertime());
  77. cell0.setCellStyle(cellStyleContent);
  78. cell1.setCellStyle(cellStyleContent);
  79. cell2.setCellStyle(cellStyleContent);
  80. cell3.setCellStyle(cellStyleContent);
  81. cell4.setCellStyle(cellStyleContent);
  82. }
  83. }
  84. try {
  85. String fileName = Excel_File_Name;
  86. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  87. ContentValues values = new ContentValues();
  88. values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
  89. values.put(MediaStore.Downloads.MIME_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  90. values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
  91. Uri uri = getActivity().getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
  92. if (uri != null) {
  93. OutputStream outputStream = getActivity().getContentResolver().openOutputStream(uri);
  94. if (outputStream != null) {
  95. workbook.write(outputStream);
  96. outputStream.close();
  97. //Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
  98. ToastUtils.getInstance().showToast("导出成功");
  99. // 分享Excel文件到微信
  100. Intent intent = new Intent();
  101. intent.setAction(Intent.ACTION_SEND);
  102. intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
  103. intent.putExtra(Intent.EXTRA_STREAM, uri); // 设置文件路径为Uri
  104. startActivity(intent); // 启动分享意图
  105. }
  106. }
  107. } else {
  108. String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + fileName;
  109. OutputStream outputStream = new FileOutputStream(filePath);
  110. workbook.write(outputStream);
  111. outputStream.close();
  112. //Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
  113. ToastUtils.getInstance().showToast("导出成功");
  114. // 分享Excel文件到微信
  115. Intent intent = new Intent();
  116. intent.setAction(Intent.ACTION_SEND);
  117. intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
  118. intent.putExtra(Intent.EXTRA_STREAM, filePath); // 设置文件路径为Uri
  119. startActivity(intent); // 启动分享意图
  120. }
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/318786
推荐阅读
相关标签
  

闽ICP备14008679号