POI导出Excel表格

た 入场券 2023-06-06 12:14 100阅读 0赞

具体需要的jar包参考上篇文章POI解析Excel表格

创建表格的格式内容是List<String[]>字符串数组。

  1. package com.ftx.poi;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import org.apache.poi.hssf.usermodel.HSSFCell;
  9. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  10. import org.apache.poi.hssf.usermodel.HSSFFont;
  11. import org.apache.poi.hssf.usermodel.HSSFRow;
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  14. import org.apache.poi.hssf.util.HSSFColor;
  15. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  16. /** * 文件导出 * @author Administrator * */
  17. public class ExportExcel {
  18. //创建Excel表格
  19. public static void main(String[] args) throws IOException {
  20. ExportExcel export = new ExportExcel();
  21. List<String[]> resource=new ArrayList<String[]>();
  22. resource.add(new String[] { "性别","姓名","住址"});
  23. resource.add(new String[] { "男","樊江锋","河南郑州"});
  24. resource.add(new String[] { "女","袁梦阳","河南安阳"});
  25. //指定生成文件的路径和文件名
  26. OutputStream outputStream=new FileOutputStream("D:\\exportFile.xls");
  27. export.exportExcel(resource, outputStream);
  28. outputStream.flush();
  29. outputStream.close();
  30. }
  31. /** * 文件导出方法 * @param resource List<String[]> 集合类型,要导出的具体数据集合 * @param outputStream 输出流,输出的Excel文件保存的具体位置 * @throws IOException */
  32. public void exportExcel(List<String[]> resource,OutputStream outputStream) throws IOException {
  33. //创建一个内存Excel对象
  34. HSSFWorkbook workbook=new HSSFWorkbook();
  35. //创建一个表格
  36. HSSFSheet sheet = workbook.createSheet("sheet1");
  37. //创建表头
  38. //获取表头内容 也就是resource集合的第零个索引的字符串数组
  39. String[] headerStr = resource.get(0);
  40. HSSFRow headeRow = sheet.createRow(0);
  41. //设置列宽
  42. for (int i = 0; i < headerStr.length; i++) {
  43. sheet.setColumnWidth(i, 5000);
  44. }
  45. //设置头单元格样式
  46. HSSFCellStyle headerStyle = workbook.createCellStyle();
  47. headerStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
  48. //设置字体
  49. HSSFFont headerFont = workbook.createFont();
  50. //headerFont.setColor(HSSFColor.VIOLET.index);
  51. headerFont.setFontName("宋体");
  52. headerStyle.setFont(headerFont);
  53. //定义表头内容
  54. for (int i = 0; i < headerStr.length; i++) {
  55. //定义一个单元格
  56. HSSFCell headerCell = headeRow.createCell(i);
  57. headerCell.setCellStyle(headerStyle);
  58. headerCell.setCellValue(headerStr[i]);
  59. }
  60. //标题内容
  61. //样式
  62. HSSFCellStyle bodyStyle = workbook.createCellStyle();
  63. bodyStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
  64. //设置字体
  65. HSSFFont bodyFont = workbook.createFont();
  66. //bodyFont.setColor(HSSFColor.BLUE.index);
  67. bodyFont.setFontName("宋体");
  68. bodyStyle.setFont(bodyFont);
  69. //创建行,上面表头已经创建过第0行了,所以这里从1开始
  70. for (int row = 1; row < headerStr.length; row++) {
  71. //输出的行数据
  72. String[] temp = resource.get(row);
  73. //创建行
  74. HSSFRow bodyRow = sheet.createRow(row);
  75. //循环创建列
  76. for (int cell = 0; cell < temp.length; cell++) {
  77. HSSFCell bodyCell = bodyRow.createCell(cell);
  78. bodyCell.setCellStyle(bodyStyle);
  79. bodyCell.setCellValue(temp[cell]);
  80. }
  81. }
  82. //将内存创建的excel对象,输出到文件夹中
  83. workbook.write(outputStream);
  84. }
  85. }

运行测试类,生成Excel表格
在这里插入图片描述
导出结束,谢谢观赏!

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读