有的时候可能会有这样的需求,就是我们想为生成的excel添加一些属性信息,其实在POI中提供了一种添加属性的方式
public void addExcelPropertys(InputStream in) throws Exception{
OutputStream out = null;
//为文件添加用户名,ip,时间,sql等信息
try {
HSSFWorkbook workbook = new HSSFWorkbook(in);
workbook.createInformationProperties();
SummaryInformation suminfoInformation = workbook.getSummaryInformation();
DocumentSummaryInformation docmentIfo = workbook.getDocumentSummaryInformation();
//添加备注信息
if(sql!=null){
suminfoInformation.setComments(sql);
} suminfoInformation.setAuthor(task.getCreatorName());
//添加自定义属性
CustomProperties customProperties = new CustomProperties();
customProperties.put("IP",ip);
docmentIfo.setCustomProperties(customProperties);
out = new FileOutputStream(file);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (out!=null) {
out.close();
}
}
}