您的位置:首页 > 其它

导出excel、pdf

2016-07-07 13:56 183 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">首先需要将ViewExcel.java和ViewPdf.java放入项目的公共类包下。</span>
1、.jsp

<input id="ArchivesExport" name="ArchivesExport" type="button" value=" 导出Excel " onClick="exportCarves('1');" class="button">

2、.js

//导出
function exportCarves(reportType){
//生成session
var documentExportReportCreateSessionData = documentExportReportCreateSessionJson();
var documentExportReportCreateSessionDataArr = documentExportReportCreateSessionData.split(split_semicolon);
if(jsSuccess == documentExportReportCreateSessionDataArr[0]){
if(reportType == "1"){
window.location.href = "<%=basePath%>viewExcel.shtml";
}else if(reportType == "2"){
window.location.href = "<%=basePath%>viewPDF.shtml";
}
}else{
alert(documentExportReportCreateSessionDataArr[1]);
}
//清除session
documentExportReportRemoveSessionJson();
}
/**
* 导出报表时生成session
*/
function documentExportReportCreateSessionJson(){
var resultMsg = "";
var carveName = $.trim($("#carveName").val());
$("#carveName").val(carveName);
var carveNumber = $.trim($("#carveNumber").val());
$("#carveNumber").val(carveNumber);
var cityid = $("#TCityList").val();
var status = $("#status").val();
var contact = $.trim($("#contact").val());
$("#contact").val(contact);
var address = $.trim($("#address").val());
$("#address").val(address);
var boroughid = $("#boroughid").val();
lockScreen("请等待…………");
$.ajax( {
type : "post",
url : "carvesExportReportCreateSessionJsonAction.action",
dataType : "json",
data : {"carveName":carveName,"carveNumber":carveNumber,"cityid":cityid,"status":status,"contact":contact,"address":address,"boroughid":boroughid},
async : false,
cache : false,
error : function (XMLHttpRequest, textStatus, errorThrown) {unblock();ajaxJsonError(XMLHttpRequest, textStatus, errorThrown,"导出报表时生成session");resultMsg = jsError + split_semicolon + "导出报表时生成session服务器错误";},
success : function(msg) {
unblock();
resultMsg = jsSuccess + split_semicolon + "导出报表时生成session成功";
}
});
return resultMsg;
}

/**
* 导出报表时清空session
*/
function documentExportReportRemoveSessionJson(){
var resultMsg = "";
lockScreen("请等待…………");
$.ajax( {
type : "post",
url : "carvesExportReportRemoveSessionJsonAction.action",
dataType : "json",
data : {},
async : false,
cache : false,
error : function (XMLHttpRequest, textStatus, errorThrown) {unblock();ajaxJsonError(XMLHttpRequest, textStatus, errorThrown,"导出报表时清空session");resultMsg = jsError + split_semicolon + "导出报表时生成session服务器错误";},
success : function(msg) {
unblock();
resultMsg = jsSuccess + split_semicolon + "导出报表时清空session成功";
}
});
return resultMsg;
}

3、structsItem.xml
<!-- 导出报表时生成session  -->
<action name="carvesExportReportCreateSessionJsonAction" class="CarvesAction" method="carvesExportReportCreateSessionJson">
<result type="json">
<param name="includeProperties">jsonResultMsg</param>
</result>
</action>
<!-- 导出报表时清空session  -->
<action name="carvesExportReportRemoveSessionJsonAction" class="CarvesAction" method="carvesExportReportRemoveSessionJson">
<result type="json">
<param name="includeProperties">jsonResultMsg</param>
</result>
</action>

4、XXaction.java

/**
* 出报表时生成session

* @return
*/
@SuppressWarnings("unchecked")
public String carvesPersonExportReportCreateSessionJson() {
try {

  getSession().removeAttribute("headList");
getSession().removeAttribute("tableList");
getSession().removeAttribute("tableTitle");
if (this.userName == null || this.userName.equals("")) {
this.userName = "%";
}
if (this.realName == null || this.realName.equals("")) {
this.realName = "%";
}
if (this.cityid == null || this.cityid.equals("")) {
this.cityid = "%";
}
if (this.boroughid == null || this.boroughid.equals("")) {
this.boroughid = "%";
}
else {
String hqlString= "from TCityList t where t.TCityList.id ="+cityid;
List<TCityList> boroughList = this.daoFactory.getHibernateFactory().find(hqlString);// 区县
}
if (this.carvesid == null || this.carvesid.equals("")) {
this.carvesid = "%";
}
String sql1 = "where t.userName like '%" + this.userName + "%' and t.realName like '%" + this.realName + "%' and t.TCityListByCityId.id like '%" + this.cityid + "%' and t.TCityListByBoroughId.id like '%" + this.boroughid
+ "%' and t.TCarves.id like '%" + this.carvesid + "%'";
String countSql = "select count(*) from TUser t " + sql1;
String strSql = "from TUser t " + sql1 + " order by t.id ";

List<TUser> carvesuserList = this.daoFactory.getHibernateFactory().find(strSql);
List<TCityList> citylistList = this.daoFactory.getHibernateItemFunction().findCityAll();

if (this.userName.equals("%")) {
this.userName = "";
}
if (this.realName.equals("%")) {
this.realName = "";
}
if (this.cityid.equals("%")) {
this.cityid = "";
}
if (this.boroughid.equals("%")) {
this.boroughid = "";
}
if (this.carvesid.equals("%")) {
this.carvesid = "";
}

List<Object> headList = new ArrayList<Object>();
headList.add("用户名");
headList.add("用户姓名");
headList.add("性别");
headList.add("证件类型");
headList.add("证件号码");
headList.add("所属刻企");
getSession().setAttribute("headList", headList);

List<List<Object>> tableList = new ArrayList<List<Object>>();
if (carvesuserList.size() > 0) {
for (int i = 0; i < carvesuserList.size(); i++) {
List<Object> objList = new ArrayList<Object>();
TUser archivesInfo = carvesuserList.get(i);
objList.add(archivesInfo.getUserName());
objList.add(archivesInfo.getRealName());
if (archivesInfo.getSex()!=null) {
objList.add(archivesInfo.getSex());
}
else {
objList.add("");
}
if (archivesInfo.getTDictionaryByCertType().getName()!=null) {
objList.add(archivesInfo.getTDictionaryByCertType().getName());
}
else {
objList.add("");
}

if (archivesInfo.getCertNumber()!=null) {
objList.add(archivesInfo.getCertNumber());
}
else {
objList.add("");
}
if (archivesInfo.getTCarves().getCarveName()!=null) {
objList.add(archivesInfo.getTCarves().getCarveName());
}
else {
objList.add("");
}
tableList.add(objList);
}
}
getSession().setAttribute("tableList", tableList);

getSession().setAttribute("tableTitle", DateFormat.getDate());

jsonResultMsg = SUCCESS + FinalString.SPLIT_SEMICOLON + "导出报表时生成session,成功";
} catch (Exception e) {
e.printStackTrace();
jsonResultMsg = ERROR + FinalString.SPLIT_SEMICOLON + "导出报表时生成session,失败,异常错误";
} finally {
}
return SUCCESS;
}
/**
* 导出报表时清空session

* @return
*/
public String carvesExportReportRemoveSessionJson() {
try {
getSession().removeAttribute("headList");
getSession().removeAttribute("tableList");
getSession().removeAttribute("tableTitle");

jsonResultMsg = SUCCESS + FinalString.SPLIT_SEMICOLON + "导出报表时清空session,成功";
} catch (Exception e) {
e.printStackTrace();
jsonResultMsg = ERROR + FinalString.SPLIT_SEMICOLON + "导出报表时清空session,失败,异常错误";
} finally {
}
return SUCCESS;
}

5、web.xml

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<!--
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
-->
<filter-class>com.item.spring.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<description>列表信息导出Excel的servlet</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DownloadExcelServlet</servlet-name>
<servlet-class>com.item.action.DownloadExcelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadExcelServlet</servlet-name>
<url-pattern>/download/DownloadExcel</url-pattern>
</servlet-mapping>

<display-name>springPDFTest</display-name>
<servlet>
<servlet-name>springPDFTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springPDFTest</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  导出excel 导出pdf