/**
* Excel模板导出
*
* @param bean
* @param templateFile
* @param pathfile2
*/
@Autowired
private static void outputExcel(Map<String, Object> bean, String templateFile, String targetFile) {
try {
createFile(targetFile);
OutputStream os = null;
InputStream fis = null;
XLSTransformer transformer = new XLSTransformer();
fis = new FileInputStream(templateFile);
Workbook wb = transformer.transformXLS(fis, bean);
//transformer.transformXLS(fis, bean);
FileOutputStream fop = new FileOutputStream(targetFile);
wb.write(fop);
// fop.flush();
fop.close();
wb.close();
} catch (Exception e) {
logger.debug("DownloadFileController:outputExcel ---" + e.getMessage());
e.printStackTrace();
}
}
/**
* 创建单个文件
*
* @param descFileName
* 文件名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createFile(String descFileName) {
File file = new File(descFileName);
if (file.exists()) {
file.delete();
} else {
file.getParentFile().mkdirs();
}
// 创建文件
try {
file.createNewFile();
logger.debug(descFileName + " 文件创建成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
logger.debug(descFileName + " 文件创建失败!");
return false;
}
}
public static void main(String[] args) {
//outputExcel(Map<String, Object> bean, String templateFile, String targetFile)
Map<String, Object> bean = new HashMap<String, Object>();
bean.put("titl1e", "早安");
bean.put("content", "晚安");
String templateFile = "E:\\zip\\excel\\temple\\az.xls";
String targetFile = "E:\\zip\\excel\\target\\za.xls";
outputExcel(bean,templateFile,targetFile);
}
* Excel模板导出
*
* @param bean
* @param templateFile
* @param pathfile2
*/
@Autowired
private static void outputExcel(Map<String, Object> bean, String templateFile, String targetFile) {
try {
createFile(targetFile);
OutputStream os = null;
InputStream fis = null;
XLSTransformer transformer = new XLSTransformer();
fis = new FileInputStream(templateFile);
Workbook wb = transformer.transformXLS(fis, bean);
//transformer.transformXLS(fis, bean);
FileOutputStream fop = new FileOutputStream(targetFile);
wb.write(fop);
// fop.flush();
fop.close();
wb.close();
} catch (Exception e) {
logger.debug("DownloadFileController:outputExcel ---" + e.getMessage());
e.printStackTrace();
}
}
/**
* 创建单个文件
*
* @param descFileName
* 文件名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createFile(String descFileName) {
File file = new File(descFileName);
if (file.exists()) {
file.delete();
} else {
file.getParentFile().mkdirs();
}
// 创建文件
try {
file.createNewFile();
logger.debug(descFileName + " 文件创建成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
logger.debug(descFileName + " 文件创建失败!");
return false;
}
}
public static void main(String[] args) {
//outputExcel(Map<String, Object> bean, String templateFile, String targetFile)
Map<String, Object> bean = new HashMap<String, Object>();
bean.put("titl1e", "早安");
bean.put("content", "晚安");
String templateFile = "E:\\zip\\excel\\temple\\az.xls";
String targetFile = "E:\\zip\\excel\\target\\za.xls";
outputExcel(bean,templateFile,targetFile);
}