Java导出Excel通用方法

原文地址:https://blog.csdn.net/j_love93/article/details/77152293

数据导出到Excel几乎是所有客户都会提出的一个需求。下面我就分享一下我的代码。

首先需要引入的jar包:


然后就是正式代码了。

[java]  view plain  copy
  1. package lcy._41_50;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.OutputStream;  
  5. import java.net.URLEncoder;  
  6.   
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  11. import org.apache.poi.hssf.usermodel.HSSFFont;  
  12. import org.apache.poi.hssf.usermodel.HSSFRow;  
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  15. import org.apache.poi.hssf.util.CellRangeAddress;  
  16. import org.apache.poi.hssf.util.HSSFColor;  
  17.   
  18. @SuppressWarnings( { "deprecation" })  
  19. public class Test46 {  
  20.   
  21.     public static void main(String[] args) throws Exception {  
  22.   
  23.         String sheetName = "用车统计表单";  
  24.         String titleName = "用车申请数据统计表";  
  25.         String fileName = "用车申请统计表单";  
  26.         int columnNumber = 3;  
  27.         int[] columnWidth = { 102030 };  
  28.         String[][] dataList = { { "001""2015-01-01""IT" },  
  29.                 { "002""2015-01-02""市场部" }, { "003""2015-01-03""测试" } };  
  30.         String[] columnName = { "单号""申请时间""申请部门" };  
  31.         new Test46().ExportNoResponse(sheetName, titleName, fileName,  
  32.                 columnNumber, columnWidth, columnName, dataList);  
  33.     }  
  34.   
  35.     public void ExportWithResponse(String sheetName, String titleName,  
  36.             String fileName, int columnNumber, int[] columnWidth,  
  37.             String[] columnName, String[][] dataList,  
  38.             HttpServletResponse response) throws Exception {  
  39.         if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {  
  40.             // 第一步,创建一个webbook,对应一个Excel文件  
  41.             HSSFWorkbook wb = new HSSFWorkbook();  
  42.             // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  43.             HSSFSheet sheet = wb.createSheet(sheetName);  
  44.             // sheet.setDefaultColumnWidth(15); //统一设置列宽  
  45.             for (int i = 0; i < columnNumber; i++)   
  46.             {  
  47.                 for (int j = 0; j <= i; j++)   
  48.                 {  
  49.                     if (i == j)   
  50.                     {  
  51.                         sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽  
  52.                     }  
  53.                 }  
  54.             }  
  55.             // 创建第0行 也就是标题  
  56.             HSSFRow row1 = sheet.createRow((int0);  
  57.             row1.setHeightInPoints(50);// 设备标题的高度  
  58.             // 第三步创建标题的单元格样式style2以及字体样式headerFont1  
  59.             HSSFCellStyle style2 = wb.createCellStyle();  
  60.             style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  61.             style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  62.             style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);  
  63.             style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  64.             HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式  
  65.             headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
  66.             headerFont1.setFontName("黑体"); // 设置字体类型  
  67.             headerFont1.setFontHeightInPoints((short15); // 设置字体大小  
  68.             style2.setFont(headerFont1); // 为标题样式设置字体样式  
  69.   
  70.             HSSFCell cell1 = row1.createCell(0);// 创建标题第一列  
  71.             sheet.addMergedRegion(new CellRangeAddress(000,  
  72.                     columnNumber - 1)); // 合并列标题  
  73.             cell1.setCellValue(titleName); // 设置值标题  
  74.             cell1.setCellStyle(style2); // 设置标题样式  
  75.   
  76.             // 创建第1行 也就是表头  
  77.             HSSFRow row = sheet.createRow((int1);  
  78.             row.setHeightInPoints(37);// 设置表头高度  
  79.   
  80.             // 第四步,创建表头单元格样式 以及表头的字体样式  
  81.             HSSFCellStyle style = wb.createCellStyle();  
  82.             style.setWrapText(true);// 设置自动换行  
  83.             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  84.             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
  85.   
  86.             style.setBottomBorderColor(HSSFColor.BLACK.index);  
  87.             style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  88.             style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  89.             style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  90.             style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  91.   
  92.             HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式  
  93.             headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
  94.             headerFont.setFontName("黑体"); // 设置字体类型  
  95.             headerFont.setFontHeightInPoints((short10); // 设置字体大小  
  96.             style.setFont(headerFont); // 为标题样式设置字体样式  
  97.   
  98.             // 第四.一步,创建表头的列  
  99.             for (int i = 0; i < columnNumber; i++)   
  100.             {  
  101.                 HSSFCell cell = row.createCell(i);  
  102.                 cell.setCellValue(columnName[i]);  
  103.                 cell.setCellStyle(style);  
  104.             }  
  105.   
  106.             // 第五步,创建单元格,并设置值  
  107.             for (int i = 0; i < dataList.length; i++)   
  108.             {  
  109.                 row = sheet.createRow((int) i + 2);  
  110.                 // 为数据内容设置特点新单元格样式1 自动换行 上下居中  
  111.                 HSSFCellStyle zidonghuanhang = wb.createCellStyle();  
  112.                 zidonghuanhang.setWrapText(true);// 设置自动换行  
  113.                 zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
  114.   
  115.                 // 设置边框  
  116.                 zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);  
  117.                 zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  118.                 zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  119.                 zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  120.                 zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  121.   
  122.                 // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中  
  123.                 HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();  
  124.                 zidonghuanhang2.setWrapText(true);// 设置自动换行  
  125.                 zidonghuanhang2  
  126.                         .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式  
  127.                 zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
  128.   
  129.                 // 设置边框  
  130.                 zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);  
  131.                 zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  132.                 zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  133.                 zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  134.                 zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  135.                 HSSFCell datacell = null;  
  136.                 for (int j = 0; j < columnNumber; j++)   
  137.                 {  
  138.                     datacell = row.createCell(j);  
  139.                     datacell.setCellValue(dataList[i][j]);  
  140.                     datacell.setCellStyle(zidonghuanhang2);  
  141.                 }  
  142.             }  
  143.   
  144.             // 第六步,将文件存到浏览器设置的下载位置  
  145.             String filename = fileName + ".xls";  
  146.             response.setContentType("application/ms-excel;charset=UTF-8");  
  147.             response.setHeader("Content-Disposition""attachment;filename="  
  148.                     .concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));  
  149.             OutputStream out = response.getOutputStream();  
  150.             try {  
  151.                 wb.write(out);// 将数据写出去  
  152.                 String str = "导出" + fileName + "成功!";  
  153.                 System.out.println(str);  
  154.             } catch (Exception e) {  
  155.                 e.printStackTrace();  
  156.                 String str1 = "导出" + fileName + "失败!";  
  157.                 System.out.println(str1);  
  158.             } finally {  
  159.                 out.close();  
  160.             }  
  161.   
  162.         } else {  
  163.             System.out.println("列数目长度名称三个数组长度要一致");  
  164.         }  
  165.   
  166.     }  
  167.   
  168.     public void ExportNoResponse(String sheetName, String titleName,  
  169.             String fileName, int columnNumber, int[] columnWidth,  
  170.             String[] columnName, String[][] dataList) throws Exception {  
  171.         if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {  
  172.             // 第一步,创建一个webbook,对应一个Excel文件  
  173.             HSSFWorkbook wb = new HSSFWorkbook();  
  174.             // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  175.             HSSFSheet sheet = wb.createSheet(sheetName);  
  176.             // sheet.setDefaultColumnWidth(15); //统一设置列宽  
  177.             for (int i = 0; i < columnNumber; i++)   
  178.             {  
  179.                 for (int j = 0; j <= i; j++)   
  180.                 {  
  181.                     if (i == j)   
  182.                     {  
  183.                         sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽  
  184.                     }  
  185.                 }  
  186.             }  
  187.             // 创建第0行 也就是标题  
  188.             HSSFRow row1 = sheet.createRow((int0);  
  189.             row1.setHeightInPoints(50);// 设备标题的高度  
  190.             // 第三步创建标题的单元格样式style2以及字体样式headerFont1  
  191.             HSSFCellStyle style2 = wb.createCellStyle();  
  192.             style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  193.             style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  194.             style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);  
  195.             style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  196.             HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式  
  197.             headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
  198.             headerFont1.setFontName("黑体"); // 设置字体类型  
  199.             headerFont1.setFontHeightInPoints((short15); // 设置字体大小  
  200.             style2.setFont(headerFont1); // 为标题样式设置字体样式  
  201.   
  202.             HSSFCell cell1 = row1.createCell(0);// 创建标题第一列  
  203.             sheet.addMergedRegion(new CellRangeAddress(000,  
  204.                     columnNumber - 1)); // 合并第0到第17列  
  205.             cell1.setCellValue(titleName); // 设置值标题  
  206.             cell1.setCellStyle(style2); // 设置标题样式  
  207.   
  208.             // 创建第1行 也就是表头  
  209.             HSSFRow row = sheet.createRow((int1);  
  210.             row.setHeightInPoints(37);// 设置表头高度  
  211.   
  212.             // 第四步,创建表头单元格样式 以及表头的字体样式  
  213.             HSSFCellStyle style = wb.createCellStyle();  
  214.             style.setWrapText(true);// 设置自动换行  
  215.             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  216.             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
  217.   
  218.             style.setBottomBorderColor(HSSFColor.BLACK.index);  
  219.             style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  220.             style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  221.             style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  222.             style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  223.   
  224.             HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式  
  225.             headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
  226.             headerFont.setFontName("黑体"); // 设置字体类型  
  227.             headerFont.setFontHeightInPoints((short10); // 设置字体大小  
  228.             style.setFont(headerFont); // 为标题样式设置字体样式  
  229.   
  230.             // 第四.一步,创建表头的列  
  231.             for (int i = 0; i < columnNumber; i++)   
  232.             {  
  233.                 HSSFCell cell = row.createCell(i);  
  234.                 cell.setCellValue(columnName[i]);  
  235.                 cell.setCellStyle(style);  
  236.             }  
  237.   
  238.             // 第五步,创建单元格,并设置值  
  239.             for (int i = 0; i < dataList.length; i++)   
  240.             {  
  241.                 row = sheet.createRow((int) i + 2);  
  242.                 // 为数据内容设置特点新单元格样式1 自动换行 上下居中  
  243.                 HSSFCellStyle zidonghuanhang = wb.createCellStyle();  
  244.                 zidonghuanhang.setWrapText(true);// 设置自动换行  
  245.                 zidonghuanhang  
  246.                         .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
  247.   
  248.                 // 设置边框  
  249.                 zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);  
  250.                 zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  251.                 zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  252.                 zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  253.                 zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  254.   
  255.                 // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中  
  256.                 HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();  
  257.                 zidonghuanhang2.setWrapText(true);// 设置自动换行  
  258.                 zidonghuanhang2  
  259.                         .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式  
  260.                 zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
  261.   
  262.                 // 设置边框  
  263.                 zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);  
  264.                 zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  265.                 zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  266.                 zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  267.                 zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  268.                 HSSFCell datacell = null;  
  269.                 for (int j = 0; j < columnNumber; j++)   
  270.                 {  
  271.                     datacell = row.createCell(j);  
  272.                     datacell.setCellValue(dataList[i][j]);  
  273.                     datacell.setCellStyle(zidonghuanhang2);  
  274.                 }  
  275.             }  
  276.   
  277.             // 第六步,将文件存到指定位置  
  278.             try {  
  279.                 FileOutputStream fout = new FileOutputStream("D:students.xls");  
  280.                 wb.write(fout);  
  281.                 String str = "导出" + fileName + "成功!";  
  282.                 System.out.println(str);  
  283.                 fout.close();  
  284.             } catch (Exception e) {  
  285.                 e.printStackTrace();  
  286.                 String str1 = "导出" + fileName + "失败!";  
  287.                 System.out.println(str1);  
  288.             }  
  289.         } else {  
  290.             System.out.println("列数目长度名称三个数组长度要一致");  
  291.         }  
  292.   
  293.     }  
  294.   
  295. }  

为了本地测试效果,单独写了一个无response参数的ExportNoResponse方法,直接将文件保存到指定目录D盘。
两个方法的不同就在于第六步中,有response参数的方法可以将文件存到浏览器设置的下载位置。
下面是导出效果截图:


刚刚入手博客,希望与大家共同学习,共同分享,共同进度。


猜你喜欢

转载自blog.csdn.net/tanga842428/article/details/80496309