/**
* 下载 导入错误信息表
*
* @param response
* @param data
* @throws Exception
*/
public void exportPublicity (HttpServletResponse response, ApiResponse <?> data) {
List <GysExcelErrorVo> list=
"将要导出的数据";
String base64=
"我这里将模板转成base64了";
File file1=base64ToFile ( base64 );
InputStream is=null;
XSSFWorkbook workbook=null;
XSSFSheet sheet=null;
try {
is=new FileInputStream ( file1 );// 将excel文件转为输入流
workbook=new XSSFWorkbook ( is );// 创建个workbook,
// 获取第一个sheet
sheet=workbook.getSheetAt ( 0 );
} catch (Exception e1) {
e1.printStackTrace ( );
}
if (sheet != null) {
try {
if (! CollectionUtils.isEmpty ( list )) {
// 写数据
FileOutputStream fos=new FileOutputStream ( file1 );
XSSFRow row=sheet.getRow ( 1 );
if (row == null) {
row=sheet.createRow ( 2 );
}
XSSFCell cell=row.getCell ( 0 );
if (cell == null) {
cell=row.createCell ( 0 );
}
for (int m=0; m < list.size ( ); m++) {
GysExcelErrorVo gysPrpLflehelperagent=list.get ( m );
row=sheet.createRow ( m + 1 );
List <Map> filedsInfo=getFiledsInfo ( gysPrpLflehelperagent );
for (int i=0; i < filedsInfo.size ( ); i++) {
Map map=filedsInfo.get ( i );
String value=String.valueOf ( map.get ( "value" ) );
String name=String.valueOf ( map.get ( "name" ) );
cell=row.createCell ( i );
cell.setCellValue ( value );
}
}
if (workbook != null) {
workbook.write ( fos );
}
fos.flush ( );
fos.close ( );
}
// 下载
InputStream fis=new BufferedInputStream ( new FileInputStream ( file1 ) );
byte[] buffer=new byte[fis.available ( )];
fis.read ( buffer );
fis.close ( );
response.reset ( );
// response.setContentType("text/html;charset=UTF-8");
response.setContentType ( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
response.setHeader ( "Content-Disposition", "attachment;filename=" + URLEncoder.encode ( "temp.xlsx", "utf-8" ) );
response.setHeader ( "Set-Cookie", "cookiename=cookievalue; path=/; Domain=domainvaule; Max-age=seconds; HttpOnly" );
OutputStream toClient=new BufferedOutputStream ( response.getOutputStream ( ) );
toClient.write ( buffer );
toClient.flush ( );
} catch (Exception e) {
throw new BusinessException ( "下载模板失败:" + e.getMessage ( ) );
} finally {
try {
if (null != is) {
is.close ( );
}
} catch (Exception e) {
e.printStackTrace ( );
}
}
}
}
/**
* 获取属性类型(type),属性名(name),属性值(value)的map组成的list
*/
private List <Map> getFiledsInfo (Object o) {
Field[] fields=o.getClass ( ).getDeclaredFields ( );
String[] fieldNames=new String[fields.length];
List <Map> list=new ArrayList ( );
Map infoMap;
for (int i=0; i < fields.length; i++) {
infoMap=new HashMap ( );
infoMap.put ( "type", fields[i].getType ( ).toString ( ) );
infoMap.put ( "name", fields[i].getName ( ) );
Object value=getFieldValueByName ( fields[i].getName ( ), o );
if (value == null || "null".equals ( value )) {
value="";
}
infoMap.put ( "value", value );
list.add ( infoMap );
}
return list;
}
/**
* 根据属性名获取属性值
*
* @param fieldName 属性名称
* @param o 对象
* @return
*/
private static Object getFieldValueByName (String fieldName, Object o) {
try {
String firstLetter=fieldName.substring ( 0, 1 ).toUpperCase ( );
String getter="get" + firstLetter + fieldName.substring ( 1 );
Method method=o.getClass ( ).getMethod ( getter );
return method.invoke ( o );
} catch (Exception e) {
log.info ( "根据属性名获取属性值异常:" + e.getMessage ( ), e );
return null;
}
}
/**
* base64转化为file流
*
* @param base64
* @return
*/
public static File base64ToFile (String base64) {
if (base64 == null || "".equals ( base64 )) {
return null;
}
byte[] buff=Base64.getDecoder ( ).decode ( base64 );
File file=null;
FileOutputStream out=null;
try {
file=File.createTempFile ( "tmp", null );
out=new FileOutputStream ( file );
out.write ( buff );
} catch (IOException e) {
e.printStackTrace ( );
} finally {
if (out != null) {
try {
out.close ( );
} catch (IOException e) {
e.printStackTrace ( );
}
}
}
return file;
}
导出数据vo类
public class GysExcelErrorVo {
/** 对应字段:user_name,备注:用户名称 */
@ApiModelProperty("用户名称")
private String userName;
/** 对应字段:mobile,备注:手机号码 */
@ApiModelProperty("手机号码")
private String mobile;
/** 对应字段:email,备注:邮箱 */
@ApiModelProperty("邮箱")
private String email;
}