一、导入jxls的jar包
二、配置Struts的xml文件
<action name="exportKhxxExportAction" class="com.crm.action.KhxxExportAction" >
<result name="success" type="stream">
<param name="contentType">
application/text/plain
</param>
<param name="contentDisposition">
attachment;filename=${khxxExcelFileName}
</param>
<param name="inputName">khxxExcel</param>
</result>
</action>
三、配置模板Excel
四、写代码传数据
public class KhxxExportAction extends BaseStruts2Action {
private static final long serialVersionUID = 1L;
private final static Log log = LogFactory.getLog(KhxxExportAction.class);
public InputStream getKhxxExcel() {
String xlsTemplateFileName = getSession().getServletContext()
.getRealPath("/resources/khxx/khxx.xls");
String tempFileName = getSession().getServletContext().getRealPath(
"/resources/khxx/" + System.currentTimeMillis());
Map beans = new HashMap();
Map report = new HashMap();
List rows = new ArrayList();
List<Map<String, Object>> datas = null;
String sql = "SELECT * from jpas_jznx_khexport_temp where 1=1 ";
try {
datas = JdbcUtils.queryToMapList(sql);
if (datas != null && datas.size() != 0) {
rows.addAll(datas);
report.put("rows", rows);
beans.put("report", report);
XLSTransformer transformer = new XLSTransformer();
transformer.transformXLS(xlsTemplateFileName, beans,
tempFileName);
InputStream io = new FileInputStream(tempFileName);
return io;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return null;
}
public String getKhxxExcelFileName()
throws UnsupportedEncodingException, SQLException {
String filename = new String(("客户信息表.xls").getBytes(), "ISO8859-1");
return filename;
}
}
这样就OK了。