Last active
August 29, 2015 14:18
-
-
Save 0532/eee63f1a5a8888f6155b to your computer and use it in GitHub Desktop.
export data to excel using xls template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String onExpExcel() { | |
try { | |
if (dataList == null || dataList.size() == 0) { | |
MessageUtil.addWarn("未查询数据!"); | |
return null; | |
} | |
Map beansMap = new HashMap(); | |
String excelFilename = "SBS损益表" + ".xls"; | |
JxlsManager jxls = new JxlsManager(); | |
beansMap.put("records", dataList); | |
jxls.exportDataToXls(excelFilename, "/acctab.xls", beansMap); | |
} catch (Exception e) { | |
} | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package feb.service; | |
import net.sf.jxls.transformer.XLSTransformer; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import pub.platform.advance.utils.PropertyManager; | |
import javax.faces.context.FacesContext; | |
import javax.servlet.ServletOutputStream; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.BufferedInputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Map; | |
/** | |
* Created by Lichao.W At 2015/3/24 9:17 | |
* wanglichao@163.com | |
*/ | |
public class JxlsManager { | |
private static final Logger logger = LoggerFactory.getLogger(JxlsManager.class); | |
public String exportDataToXls(String filename, String templateFileName, Map beansMap) { | |
try { | |
//String reportPath = PropertyManager.getProperty("EXCEL_REPORT_ROOTPATH"); | |
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("report/templates/sbs/"); | |
String templateFileFullName = reportPath + templateFileName; | |
outputExcel(beansMap, templateFileFullName, filename); | |
} catch (Exception e) { | |
logger.error("报表处理错误!", e); | |
throw new RuntimeException("报表处理错误!", e); | |
} | |
return null; | |
} | |
private void outputExcel(Map beansMap, String templateFileName, String excelFilename) throws IOException { | |
ServletOutputStream os = null; | |
InputStream is = null; | |
try { | |
XLSTransformer transformer = new XLSTransformer(); | |
is = new BufferedInputStream(new FileInputStream(templateFileName)); | |
HSSFWorkbook wb = transformer.transformXLS(is, beansMap); | |
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); | |
os = response.getOutputStream(); | |
response.reset(); | |
response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(excelFilename, "UTF-8")); | |
response.setContentType("application/msexcel"); | |
wb.write(os); | |
} finally { | |
if (os != null) { | |
os.flush(); | |
os.close(); | |
} | |
if (is != null) { | |
is.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment