首页 > 代码库 > java Export Excel POI 转
java Export Excel POI 转
最终选择用POI成功导出excel。总之很有用。
http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html
http://poi.apache.org/download.html
Student.java
package org.leno.export.util;import java.util.Date;public class Student { private long id; private String name; private int age; private boolean sex; private Date birthday; public Student() { super(); // TODO Auto-generated constructor stub } public Student(long id, String name, int age, boolean sex, Date birthday) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.birthday = birthday; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean getSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }}
ExportExcel.java
package myExcel;import java.awt.List;import java.io.FileNotFoundException;import java.io.FileOutputStream;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.*;import java.sql.ResultSet;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.mail.Header;import javax.swing.JOptionPane;import javax.swing.plaf.synth.Region;import org.apache.poi.hssf.usermodel.*;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.formula.functions.T;import classman.class_operation;import util.stringUtil;public class myExcelExcel<T> { public void exportExcel(Collection<T> dataset, OutputStream out) { exportExcel("testPOI", null, dataset, out, "yyyy-MM-dd"); } public void exportExcel(String[] headers, Collection<T> dataset, OutputStream out) { exportExcel("testPOI", headers, dataset, out, "yyyy-MM-dd"); } public void exportExcel(String[] headers, Collection<T> dataset, OutputStream out, String pattern) { exportExcel("testPOI", headers, dataset, out, pattern); } public void exportExcel(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(title); sheet.setDefaultColumnWidth((short) 15); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.SKY_BLUE.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.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); style.setFont(font); HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.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); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); comment.setAuthor("leno"); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // 遍历集合数据,产生数据行 Iterator<T> it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); Field field = fields[i]; String fileName = field.getName(); String getMethodName = "get" + fileName.substring(0, 1).toUpperCase() + fileName.substring(1); try { Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] { }); // 判断值的类型后进行强制类型转换 String textValue = http://www.mamicode.com/null; if (value instanceof Boolean) { boolean bValue =http://www.mamicode.com/ (Boolean) value; textValue = "男"; if (!bValue) textValue = "女"; } else if (value instanceof Date) { Date date=(Date) value; SimpleDateFormat sdf=new SimpleDateFormat(pattern); textValue=sdf.format(date); } else if (value instanceof Byte[]) { } else { textValue=value.toString(); } System.out.println(textValue); if (textValue != null) { Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher = p.matcher(textValue); if (matcher.matches()) { cell.setCellValue(Double.parseDouble(textValue)); } else { HSSFRichTextString richTextString = new HSSFRichTextString( textValue); HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); richTextString.applyFont(font3); cell.setCellValue(richTextString); } } } catch (SecurityException e) { // TODO: handle exception e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO: handle exception e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO: handle exception e.printStackTrace(); } catch (IllegalAccessException e) { // TODO: handle exception e.printStackTrace(); } catch (InvocationTargetException e) { // TODO: handle exception e.printStackTrace(); } finally { // 清理资源 } } } try { workbook.write(out); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } public void testMain() { String[] headers = { "学号", "姓名", "年龄", "性别", "出生日期" }; myExcelExcel<Student> ex=new myExcelExcel<Student>(); java.util.List<Student> dataset = new ArrayList<Student>(); dataset.add(new Student(100001, "aa", 20, true, new java.util.Date())); dataset.add(new Student(2002, "bbb", 24, false, new java.util.Date())); dataset.add(new Student(3003, "ccc", 22, true, new java.util.Date())); try { OutputStream out = new FileOutputStream("c://a.xls"); ex.exportExcel(headers, dataset,out); out.close(); JOptionPane.showMessageDialog(null, "export OK!"); System.out.println("export OK"); } catch (FileNotFoundException e) { e.printStackTrace(); // TODO: handle exception } catch (IOException e) { e.printStackTrace(); // TODO: handle exception } }}
这样写完已经可以导出了。
明天弄一个导出到浏览器客户端电脑上的功能。
java Export Excel POI 转
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。