工具方法:java导出Excel表格(简单示例)
2017-04-27 17:56
513 查看
http://blog.csdn.net/xuanzhangran/article/details/54709221
http://blog.csdn.net/szwangdf/article/details/39053859
用的springmvc注解方式:
1.导jar包(excel工具类和支持的包)
2.ExcelUtil类 (工具类)
3.UserVO实体类
4.ExportExcelController.java
http://blog.csdn.net/szwangdf/article/details/39053859
用的springmvc注解方式:
1.导jar包(excel工具类和支持的包)
2.ExcelUtil类 (工具类)
import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; 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.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; public class ExcelUtil<T> { //exportExcel.java Myview.java 879行 public void exportExcel(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { // 声明一个工作薄 SXSSFWorkbook workbook = new SXSSFWorkbook(1000); // 生成一个表格 SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 15); // 产生表格标题行 SXSSFRow row = (SXSSFRow) sheet.createRow(0); for (short i = 0; i < headers.length; i++) { SXSSFCell cell = (SXSSFCell) row.createCell(i); cell.setCellValue(headers[i]); } // 遍历集合数据,产生数据行 Iterator<T> it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; row = (SXSSFRow) sheet.createRow(index); T t = (T) it.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { SXSSFCell cell = (SXSSFCell) row.createCell(i); Field field = fields[i]; String fieldName = field.getName(); String getMethodName = "get"+ fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1); try { Class<? extends Object> tclass = t.getClass(); Method getMethod = tclass.getMethod(getMethodName,new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); String textValue = null; if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat(pattern); textValue = sdf.format(date); row.setHeightInPoints(20); } else { // 其它数据类型都当作字符串简单处理 textValue = value!=null?value.toString():""; row.setHeightInPoints(20); } // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成 if (textValue != null) { Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher = p.matcher(textValue); if (matcher.matches()) { // 是数字当作double处理 cell.setCellValue(Double.parseDouble(textValue)); } else { cell.setCellValue(textValue); } } } catch (Exception e) { e.printStackTrace(); } finally { // 清理资源 } } } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }
3.UserVO实体类
public class UserVO { private String name; private int grade; private int age; private String address; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
4.ExportExcelController.java
import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.taray.pojo.UserVO; import com.taray.util.ExcelUtil; @Controller @Scope("prototype") public class ExportExcelController { @RequestMapping("export") public void exportInof(HttpServletRequest request,HttpServletResponse response) throws Exception{ response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=EXCELNAME.xls"); OutputStream out = response.getOutputStream(); ExcelUtil<UserVO> t=new ExcelUtil<UserVO>(); String [] headers={"姓名","年级","年龄","地址","电话"}; //数据集合开始 List<UserVO> list=new ArrayList<UserVO>(); UserVO u1=new UserVO(); u1.setName("张三"); u1.setGrade(3); u1.setAge(12); u1.setAddress("湖中"); u1.setPhone("123321321"); UserVO u2=new UserVO(); u2.setName("李四"); u2.setGrade(2); u2.setAge(16); u2.setAddress("湖北"); u2.setPhone("54324231"); UserVO u3=new UserVO(); u3.setName("王五"); u3.setGrade(4); u3.setAge(13); u3.setAddress("湖南"); u3.setPhone("125435342"); list.add(u1); list.add(u2); list.add(u3); //数据集合结束 t.exportExcel("文件名词", headers, list, out, "yyyy-MM-dd HH:mm:ss"); out.close(); } }
相关文章推荐
- Java使用POI将数据导出到Excel的工具方法
- java导出生成word的简单方法
- java-RSA算法加密工具的简单使用方法
- java导入导出excel常用操作小结及简单示例
- Java使用excel工具类导出对象功能示例
- Java简单实现session保存到redis的方法示例
- JavaWeb中导出excel文件的简单方法
- 最简单的Java导出excel表格(poi中的XSSFWorkbook方式)
- Java二维数组简单定义与使用方法示例
- 简单的java的导出excel的方法
- java生成csv文件乱码的解决方法示例 java导出csv乱码
- (简单)C#导出Excel表格的方法
- java简单解析xls文件的方法示例【读取和写入】
- Java数据结构之简单的连接点(link)实现方法示例
- java 导出excel封装了一个简单的导出方法。
- Java-Maven-POI 简单导入导出Excel通用工具,默认使用基于poi实现
- java的excel导出的简单示例
- Java实现Excel导入导出数据库的方法示例
- java中使用jxl导出excel表格的工具类(全网唯一亲测可用,在原来基础上扩展)
- Java利用POI实现导入导出Excel表格示例代码