今天就跟大家聊聊有关怎么在Java中导出Excel通用工具类,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
1、构建pom.xml
我们的工程是利用Maven来构建的,项目具体搭建过程大家可以参见网上其他资料,这里我们仅给出最核心的Maven配置
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.11-beta2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.11-beta2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.11-beta2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-excelant</artifactId><version>3.11-beta2</version></dependency>
2、编写ExportExcelUtil类
这个类使我们整个工具的核心,它实现了Excel文件的导出功能,具体代码如下:
package com.lyz.utils.excel.poi;import java.io.IOException;import java.io.OutputStream;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.Iterator;import java.util.regex.Matcher;import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFColor;import org.apache.poi.xssf.usermodel.XSSFFont;import org.apache.poi.xssf.usermodel.XSSFRichTextString;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExportExcelUtil<T>{// 2007 版本以上 最大支持1048576行public final static String EXCEl_FILE_2007 = "2007";// 2003 版本 最大支持65536 行public final static String EXCEL_FILE_2003 = "2003";public void exportExcel(String title, Collection<T> dataset, OutputStream out, String version) {if(StringUtils.isEmpty(version) || EXCEL_FILE_2003.equals(version.trim())){exportExcel2003(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss");}else{exportExcel2007(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss");}} public void exportExcel(String title,String[] headers, Collection<T> dataset, OutputStream out,String version) {if(StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())){exportExcel2003(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss");}else{exportExcel2007(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss");}} @SuppressWarnings({ "unchecked", "rawtypes" })public void exportExcel2007(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) {// 声明一个工作薄XSSFWorkbook workbook = new XSSFWorkbook();// 生成一个表格XSSFSheet sheet = workbook.createSheet(title);// 设置表格默认列宽度为15个字节sheet.setDefaultColumnWidth(20);// 生成一个样式XSSFCellStyle style = workbook.createCellStyle();// 设置这些样式style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray));style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);style.setBorderBottom(XSSFCellStyle.BORDER_THIN);style.setBorderLeft(XSSFCellStyle.BORDER_THIN);style.setBorderRight(XSSFCellStyle.BORDER_THIN);style.setBorderTop(XSSFCellStyle.BORDER_THIN);style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 生成一个字体XSSFFont font = workbook.createFont();font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);font.setFontName("宋体"); font.setColor(new XSSFColor(java.awt.Color.BLACK));font.setFontHeightInPoints((short) 11);// 把字体应用到当前的样式style.setFont(font);// 生成并设置另一个样式XSSFCellStyle style2 = workbook.createCellStyle();style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);style2.setBorderRight(XSSFCellStyle.BORDER_THIN);style2.setBorderTop(XSSFCellStyle.BORDER_THIN);style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 生成另一个字体XSSFFont font2 = workbook.createFont();font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);// 把字体应用到当前的样式style2.setFont(font2); // 产生表格标题行XSSFRow row = sheet.createRow(0);XSSFCell cellHeader;for (int i = 0; i < headers.length; i++) {cellHeader = row.createCell(i);cellHeader.setCellStyle(style);cellHeader.setCellValue(new XSSFRichTextString(headers[i]));} // 遍历集合数据,产生数据行Iterator<T> it = dataset.iterator();int index = 0;T t;Field[] fields;Field field;XSSFRichTextString richString;Pattern p = Pattern.compile("^//d+(//.//d+)?$");Matcher matcher;String fieldName;String getMethodName;XSSFCell cell;Class tCls;Method getMethod;Object value;String textValue;SimpleDateFormat sdf = new SimpleDateFormat(pattern);while (it.hasNext()) {index++;row = sheet.createRow(index);t = (T) it.next();// 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值fields = t.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {cell = row.createCell(i);cell.setCellStyle(style2);field = fields[i];fieldName = field.getName();getMethodName = "get" + fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);try {tCls = t.getClass();getMethod = tCls.getMethod(getMethodName, new Class[] {});value = getMethod.invoke(t, new Object[] {});// 判断值的类型后进行强制类型转换textValue = null;if (value instanceof Integer) {cell.setCellValue((Integer) value);} else if (value instanceof Float) {textValue = String.valueOf((Float) value);cell.setCellValue(textValue);} else if (value instanceof Double) {textValue = String.valueOf((Double) value);cell.setCellValue(textValue);} else if (value instanceof Long) {cell.setCellValue((Long) value);}if (value instanceof Boolean) {textValue = "是";if (!(Boolean) value) {textValue = "否";}} else if (value instanceof Date) {textValue = sdf.format((Date) value);} else {// 其它数据类型都当作字符串简单处理if (value != null) {textValue = value.toString();}}if (textValue != null) {matcher = p.matcher(textValue);if (matcher.matches()) {// 是数字当作double处理cell.setCellValue(Double.parseDouble(textValue));} else {richString = new XSSFRichTextString(textValue);cell.setCellValue(richString);}}} catch (SecurityException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} finally {// 清理资源}}}try {workbook.write(out);} catch (IOException e) {e.printStackTrace();}}@SuppressWarnings({ "unchecked", "rawtypes" })public void exportExcel2003(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) {// 声明一个工作薄HSSFWorkbook workbook = new HSSFWorkbook();// 生成一个表格HSSFSheet sheet = workbook.createSheet(title);// 设置表格默认列宽度为15个字节sheet.setDefaultColumnWidth(20);// 生成一个样式HSSFCellStyle style = workbook.createCellStyle();// 设置这些样式style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 生成一个字体HSSFFont font = workbook.createFont();font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);font.setFontName("宋体"); font.setColor(HSSFColor.WHITE.index);font.setFontHeightInPoints((short) 11);// 把字体应用到当前的样式style.setFont(font);// 生成并设置另一个样式HSSFCellStyle style2 = workbook.createCellStyle();style2.setFillForegroundColor(HSSFColor.WHITE.index);style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);style2.setBorderRight(HSSFCellStyle.BORDER_THIN);style2.setBorderTop(HSSFCellStyle.BORDER_THIN);style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 生成另一个字体HSSFFont font2 = workbook.createFont();font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 把字体应用到当前的样式style2.setFont(font2); // 产生表格标题行HSSFRow row = sheet.createRow(0);HSSFCell cellHeader;for (int i = 0; i < headers.length; i++) {cellHeader = row.createCell(i);cellHeader.setCellStyle(style);cellHeader.setCellValue(new HSSFRichTextString(headers[i]));} // 遍历集合数据,产生数据行Iterator<T> it = dataset.iterator();int index = 0;T t;Field[] fields;Field field;HSSFRichTextString richString;Pattern p = Pattern.compile("^//d+(//.//d+)?$");Matcher matcher;String fieldName;String getMethodName;HSSFCell cell;Class tCls;Method getMethod;Object value;String textValue;SimpleDateFormat sdf = new SimpleDateFormat(pattern);while (it.hasNext()) {index++;row = sheet.createRow(index);t = (T) it.next();// 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值fields = t.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {cell = row.createCell(i);cell.setCellStyle(style2);field = fields[i];fieldName = field.getName();getMethodName = "get" + fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);try {tCls = t.getClass();getMethod = tCls.getMethod(getMethodName, new Class[] {});value = getMethod.invoke(t, new Object[] {});// 判断值的类型后进行强制类型转换textValue = null;if (value instanceof Integer) {cell.setCellValue((Integer) value);} else if (value instanceof Float) {textValue = String.valueOf((Float) value);cell.setCellValue(textValue);} else if (value instanceof Double) {textValue = String.valueOf((Double) value);cell.setCellValue(textValue);} else if (value instanceof Long) {cell.setCellValue((Long) value);}if (value instanceof Boolean) {textValue = "是";if (!(Boolean) value) {textValue = "否";}} else if (value instanceof Date) {textValue = sdf.format((Date) value);} else {// 其它数据类型都当作字符串简单处理if (value != null) {textValue = value.toString();}}if (textValue != null) {matcher = p.matcher(textValue);if (matcher.matches()) {// 是数字当作double处理cell.setCellValue(Double.parseDouble(textValue));} else {richString = new HSSFRichTextString(textValue);cell.setCellValue(richString);}}} catch (SecurityException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} finally {// 清理资源}}}try {workbook.write(out);} catch (IOException e) {e.printStackTrace();}}}
为了演示导出功能,这里我们创建了一个Student学生类。
3、创建Student类
package com.lyz.utils.excel.poi; public class Student { private int id; private String name; private String sex; public Student(int id, String name, String sex) { this.id = id; this.name = name; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
4、创建测试类TestExportExcelUtil
这个类,主要是用来测试我们的工具类。具体代码如下:
package com.lyz.test; import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List; import com.lyz.utils.excel.poi.ExportExcelUtil;import com.lyz.utils.excel.poi.Student; public class TestExportExcelUtil {public static void main(String[] args) throws Exception{ExportExcelUtil<Student> util = new ExportExcelUtil<Student>(); // 准备数据 List<Student> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add(new Student(111,"张三asdf","男")); list.add(new Student(111,"李四asd","男")); list.add(new Student(111,"王五","女")); } String[] columnNames = { "ID", "姓名", "性别" }; util.exportExcel("用户导出", columnNames, list, new FileOutputStream("E:/test.xls"), ExportExcelUtil.EXCEL_FILE_2003);}}
5、测试
我们运行TestExportExcelUtil类,会发现在我们的E盘下生成了test.xls文件,具体如下:
三、项目扩展
以上实现均是在本地磁盘生成excel文件,但更多的时候,我们需要通过点击网页上的某个按钮来自动生成并下载Excel文档,那么,这又如何实现呢?下面我们就一起来实现这个功能。
1、扩展ExportExcelUtil类
根据Java的继承思想,我们不在ExportExcelUtil类上修改添加,我们创建一个ExportExcelUtil类的子类ExportExcelWrapper,这个类继承ExportExcelUtil的所有功能,同时,扩展了网页生成Excel的功能。具体代码如下:
package com.lyz.utils.excel.poi; import java.net.URLEncoder;import java.util.Collection; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; public class ExportExcelWrapper<T> extends ExportExcelUtil<T> {public void exportExcel(String fileName, String title, String[] headers, Collection<T> dataset, HttpServletResponse response,String version) {try {response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8") + ".xls");if(StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())){exportExcel2003(title, headers, dataset, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss");}else{exportExcel2007(title, headers, dataset, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss");}} catch (Exception e) {e.printStackTrace();}}}
这样,我们可以在Controller层调用ExportExcelWrapper将相关的数据和HttpServletResponse传递进来,即可实现通过网页生生并下载Excel文档了。
2、编写Controller类
@Controller("test")@RequestMapping("/test")public class TestController {@RequestMapping("/get/excel")public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {// 准备数据List<Student> list = new ArrayList<>();for (int i = 0; i < 10; i++) { list.add(new Student(111,"张三asdf","男")); list.add(new Student(111,"李四asd","男")); list.add(new Student(111,"王五","女"));}String[] columnNames = { "ID", "姓名", " 性别"};String fileName = "excel1";ExportExcelWrapper<Student> util = new ExportExcelWrapper<Student>();util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtil.EXCEL_FILE_2003);}}
3、编写index.html
这个网页很简单,具体实现如下:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>导出Excel</title></head><body><form id="form_login" action="http://127.0.0.1:8080/test/get/excel" method="post"> </form><button id="btn-submit" onclick="beforeSubmit()">Submit</button><script type="text/javascript"> var loginForm = document.getElementById('form_login'); function beforeSubmit() { loginForm.submit(); }</script></body></html>
4、测试
我们将程序发布到Tomcat,并点击网上的按钮,效果如下:
看完上述内容,你们对怎么在Java中导出Excel通用工具类有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。