Java 使用POI导出Excel文件
2017-12-16 16:07
453 查看
导出为Excel文件,所需要引入的包与导入Excel相同,如下(可能有些不太必要):
demo的详细代码如下:
ExcelUtil类如下:
UserData类
public class UserData {
private String userno;
private String extname;
private String posindex;
public String getUserno() {
return userno;
}
public void setUserno(String userno) {
this.userno = userno;
}
public String getExtname() {
return extname;
}
public void setExtname(String extname) {
this.extname = extname;
}
public String getPosindex() {
return posindex;
}
public void setPosindex(String posindex) {
this.posindex = posindex;
}
}
以上代码是简单导出Excel的demo,应该都能看得懂,这里不再详细解释,有问题留言。
demo的详细代码如下:
import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelExportTest { /*excel column formate:column_#_width, excel中每一列的名称*/ public static final String[] RECORES_COLUMNS = new String[]{ "UserNumber_#_5000", "ExtensionName_#_5000", "PositionIndex_#_5000" }; private static final String ORIGINAL_NUM = "1000"; /*the column will display on xls files. must the same as the entity fields.对应上面的字段.*/ public static final String[] RECORES_FIELDS = new String[]{ "Userno","Extname","Posindex" }; public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException { List<UserData> users = new ArrayList<UserData>(); for(int i=0; i<10;i++){ UserData u = new UserData(); u.setUserno(ORIGINAL_NUM+i); u.setExtname(ORIGINAL_NUM+i); u.setPosindex(String.valueOf(i)); users.add(u); } //实际项目中,这个list 估计是从数据库中得到的 HSSFWorkbook workbook = new HSSFWorkbook(); ExcelUtil<UserData> userSheet = new ExcelUtil<UserData>(); for(int j = 0;j<3;j++){ userSheet.creatAuditSheet(workbook, "sheet"+(j+1), users, RECORES_COLUMNS, RECORES_FIELDS); } FileOutputStream fileOut = new FileOutputStream("D:/test1.xls"); workbook.write(fileOut); fileOut.flush(); fileOut.close(); } }
ExcelUtil类如下:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; 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.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class ExcelUtil <UserData>{ public HSSFCellStyle getCellStyle(HSSFWorkbook workbook,boolean isHeader){ HSSFCellStyle style = workbook.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setLocked(true); if (isHeader) { style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.BLACK.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); style.setFont(font); } return style; } public void generateHeader(HSSFWorkbook workbook,HSSFSheet sheet,String[] headerColumns){ HSSFCellStyle style = getCellStyle(workbook,true); Row row = sheet.createRow(0); row.setHeightInPoints(30); for(int i=0;i<headerColumns.length;i++){ Cell cell = row.createCell(i); String[] column = headerColumns[i].split("_#_"); sheet.setColumnWidth(i, Integer.valueOf(column[1])); cell.setCellValue(column[0]); cell.setCellStyle(style); } } @SuppressWarnings({ "rawtypes", "unchecked" }) public HSSFSheet creatAuditSheet(HSSFWorkbook workbook,String sheetName, List<UserData> dataset,String[] headerColumns,String[] fieldColumns) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { HSSFSheet sheet = workbook.createSheet(sheetName); // sheet.protectSheet("1234");//设置Excel保护密码 generateHeader(workbook,sheet,headerColumns); HSSFCellStyle style = getCellStyle(workbook,false); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); int rowNum = 0; for(UserData t:dataset){ rowNum++ ; Row row = sheet.createRow(rowNum); row.setHeightInPoints(25); for(int i = 0; i < fieldColumns.length; i++){ String fieldName = fieldColumns[i] ; String getMethodName = "get" + fieldName; try { Class clazz = t.getClass(); Method getMethod; getMethod = clazz.getMethod(getMethodName, new Class[]{} ); Object value = getMethod.invoke(t, new Object[]{}); String cellValue = ""; if (value instanceof Date){ Date date = (Date)value; cellValue = sd.format(date); }else{ cellValue = null != value ? value.toString() : ""; } Cell cell = row.createCell(i); cell.setCellStyle(style); cell.setCellValue(cellValue); } catch (Exception e) { } } } return sheet; } }
UserData类
public class UserData {
private String userno;
private String extname;
private String posindex;
public String getUserno() {
return userno;
}
public void setUserno(String userno) {
this.userno = userno;
}
public String getExtname() {
return extname;
}
public void setExtname(String extname) {
this.extname = extname;
}
public String getPosindex() {
return posindex;
}
public void setPosindex(String posindex) {
this.posindex = posindex;
}
}
以上代码是简单导出Excel的demo,应该都能看得懂,这里不再详细解释,有问题留言。
相关文章推荐
- 使用POI实现在java程序中导入导出Excel文件数据
- 【JavaWeb开发】使用java实现简单的Excel文件的导入与导出(POI)
- Java生成excel导出文件(使用poi+JXL)
- java学习之--导入导出excel文件 通过poi
- Java使用POI实现数据导出excel报表
- 使用Java POI导入导出Excel数据
- java中使用JXL对Excel文件进行数据的写入、导出操作
- POI导出大量数据的简单解决方案(附源码)-Java-POI导出大量数据,导出Excel文件,压缩ZIP(转载自iteye.com)
- java中使用poi导出Excel详解
- java中使用poi导出Excel详解
- web中使用POI导入导出EXCEL文件的例子
- java poi导出excel文件
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- Java使用POI实现数据导出excel报表
- Java使用POI将数据导出到Excel的工具方法
- jsp(servlet)使用poi导出excel数据文件
- Java使用POI导出到Excel。
- Java使用POI实现数据导出excel报表
- java中使用poi导出Excel详解
- java使用poi读取ppt文件和poi读取excel、word示例