按钮:<a class="nui-button" iconCls="icon-download" onclick="expDatas()">导出</a>
<!--导出 -->
<iframe id="excelDownload" style="display: none;"></iframe>
function expDatas(){
var data =grid.getData();//获取grid表格数据
if(data.length<1){
showAlert("提示", "没有数据不能导出!");
return;
}
var json = nui.encode({"param" : data});
nui.ajax({
url : "com.sgai.fssc.service.hec4cwgx.bgt.bgtJournalImport.exportFinancialExcel.biz.ext",
type : "post",
data : json,
cache : false,
contentType : 'text/json',
success : function(text) {
var returnJson = nui.decode(text);
if(returnJson.result=="0"){
if(!text.exception){
var fileName="预算日记账";//导出的名称
//download.jsp是导出后需要进行输出处理
var downloadURL=encodeURI("<%=request.getContextPath()%>/pay/download.jsp?filePath="
+ returnJson.downloadFile
+ "&filename="
+ fileName);
$("#excelDownload").attr("src",downloadURL);
} else {
nui.alert("操作失败!<br/>"+ text.exception.message);
}
}else{
showAlert("失败", "操作失败!");
}
},
error : function() {
showAlert("失败", "请求失败!");
}
});
}
@Bizlet("")
public static String getObjectsToJsonString(DataObject[] obs) {
JSONArray jsonAry = new JSONArray();
JSONObject jsonObj = new JSONObject();
DataObject tmpob = null;
for (int i = 0; i < obs.length; i++) {
tmpob = obs[i];
List<PropertyImpl> properties = tmpob.getInstanceProperties();
Iterator<PropertyImpl> it = properties.iterator();
while (it.hasNext()) {
PropertyImpl property = it.next();
String keystr = property.getName();
String value = tmpob.getString(keystr);
jsonObj.element(keystr, value);
}
jsonAry.add(jsonObj);
}
return jsonAry.toString();
}
拼装数据:
@Bizlet("预算日记账导入")
public List<DataObject> getExcelData(String filePath,int sid,IUserObject [] user,String bgtOrgCode) throws FileNotFoundException, IOException,Exception{
//创建工作簿对象
// 得到工作簿所有的工作表对象
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(filePath)));
HSSFSheet sheet = wb.getSheetAt(0);
// 遍历所有行
List<DataObject> list = new ArrayList<DataObject>();
ITransactionManager manager = TransactionManagerFactory.getTransactionManager();
com.sgai.fssc.service.hec4cwgx.subjectDef.DBTool tool = new com.sgai.fssc.service.hec4cwgx.subjectDef.DBTool();
try {
manager.begin();
for(int i = 1;i<sheet.getLastRowNum()+1;i++){
DataObject data = DataObjectUtil.createDataObject("com.primeton.das.datatype.AnyType");
HSSFRow row = sheet.getRow(i);
if(CheckRowNull(row)>=38){//判断是否是空行
continue;
}else{
data.setInt("batchId",sid);//批次id
if(row.getCell((short)0)!=null){
Object batchLineId = ExcelUtil.getCellValue(row.getCell((short)0));//规则序列
data.setInt("batchLineId", Integer.parseInt((String)batchLineId));
}
if(row.getCell((short)1)!=null){
Object bgtJournalTypeCode = ExcelUtil.getCellValue(row.getCell((short)1));//预算日记账类型
data.setString("bgtJournalTypeCode", bgtJournalTypeCode.toString().trim());
}
if(row.getCell((short)2)!=null){
Object journalCompanyCode = ExcelUtil.getCellValue(row.getCell((short)2));//头公司
data.setInt("journalCompanyCode", Integer.parseInt((String)journalCompanyCode));
}
if(row.getCell((short)3)!=null){
Object journalEmployeeCode = ExcelUtil.getCellValue(row.getCell((short)3));//申请人
data.setString("journalEmployeeCode", journalEmployeeCode.toString().trim());
}
if(row.getCell((short)4)!=null){
Object journalPositionCode = ExcelUtil.getCellValue(row.getCell((short)4));//申请人岗位
data.setString("journalPositionCode", journalPositionCode.toString().trim());
}
if(row.getCell((short)5)!=null){
Object journalHeaderNotes = ExcelUtil.getCellValue(row.getCell((short)5));//头备注
data.setString("journalHeaderNotes", journalHeaderNotes.toString().trim());
}
if(row.getCell((short)6)!=null){
Object budgetStructureCode = ExcelUtil.getCellValue(row.getCell((short)6));//预算表
data.setString("budgetStructureCode", budgetStructureCode.toString().trim());
}
if(row.getCell((short)7)!=null){
Object scenarioCode = ExcelUtil.getCellValue(row.getCell((short)7));//场景
data.setString("scenarioCode", scenarioCode.toString().trim());
}
if(row.getCell((short)8)!=null){
Object versionCode = ExcelUtil.getCellValue(row.getCell((short)8));//版本
data.setString("versionCode", versionCode.toString().trim());
}
if(row.getCell((short)9)!=null){
Object companyCode = ExcelUtil.getCellValue(row.getCell((short)9));//行公司
data.setString("companyCode", companyCode.toString().trim());
}
if(row.getCell((short)10)!=null){
Object periodName = ExcelUtil.getCellValue(row.getCell((short)10));//期间
data.setString("periodName", periodName.toString().trim());
}
if(row.getCell((short)11)!=null){
Object responsibilityCenterCode = ExcelUtil.getCellValue(row.getCell((short)11));//责任中心
data.setString("responsibilityCenterCode", responsibilityCenterCode.toString().trim());
}
if(row.getCell((short)12)!=null){
Object responsibilityCenterCode = ExcelUtil.getCellValue(row.getCell((short)12));//预算科目
data.setString("budgetItemCode", responsibilityCenterCode.toString().trim());
}
if(row.getCell((short)13)!=null){
Object amount = ExcelUtil.getCellValue(row.getCell((short)13));//金额
float a=Float.parseFloat(amount.toString());//b为object类型
data.set("amount", a);
}
Object quantity = ExcelUtil.getCellValue(row.getCell((short)14));//数量
if(!org.springframework.util.StringUtils.isEmpty(quantity)){
data.setInt("quantity", Integer.parseInt((String)quantity));
}
if(row.getCell((short)15)!=null){
Object journalLinesNotes = ExcelUtil.getCellValue(row.getCell((short)15));//行备注
data.setString("journalLinesNotes", journalLinesNotes.toString().trim());
}
if(row.getCell((short)16)!=null){
Object journalOperationUnitCode = ExcelUtil.getCellValue(row.getCell((short)16));//头经营单位
data.setString("journalOperationUnitCode", journalOperationUnitCode.toString().trim());
}
if(row.getCell((short)17)!=null){
Object periodYear = ExcelUtil.getCellValue(row.getCell((short)17));//年度
data.setInt("periodYear", Integer.parseInt((String)periodYear));
}
if(row.getCell((short)18) !=null){
if(row.getCell((short)18).toString() !=""){
int a = new Integer(row.getCell((short)18).toString());
if(a>0){
Object periodQuarter = ExcelUtil.getCellValue(row.getCell((short)18));//季度
data.setInt("periodQuarter", Integer.parseInt((String)periodQuarter));
}
}
}
if(row.getCell((short)19)!=null){
Object currencyCode = ExcelUtil.getCellValue(row.getCell((short)19));//币种
data.setString("currencyCode", currencyCode.toString().trim());
}
if(row.getCell((short)20)!=null){
Object exchangeRateType = ExcelUtil.getCellValue(row.getCell((short)20));//汇率类型
data.setString("exchangeRateType", exchangeRateType.toString().trim());
}
if(row.getCell((short)21)!=null){
Object exchangeRateQuotation = ExcelUtil.getCellValue(row.getCell((short)21));//标价方法
data.setString("exchangeRateQuotation", exchangeRateQuotation.toString().trim());
}
if(row.getCell((short)22)!=null){
Object exchangeRate = ExcelUtil.getCellValue(row.getCell((short)22));//汇率
data.setInt("exchangeRate", Integer.parseInt((String)exchangeRate));
}
if(row.getCell((short)23)!=null){
Object uom = ExcelUtil.getCellValue(row.getCell((short)23));//单位
data.setString("uom", uom.toString().trim());
}
if(row.getCell((short)24)!=null){
Object operationUnitCode = ExcelUtil.getCellValue(row.getCell((short)24));//行经营单位
data.setString("operationUnitCode", operationUnitCode.toString().trim());
}
if(row.getCell((short)25)!=null){
Object unitCode = ExcelUtil.getCellValue(row.getCell((short)25));//部门
data.setString("unitCode", unitCode.toString().trim());
}
if(row.getCell((short)26)!=null){
Object positionCode = ExcelUtil.getCellValue(row.getCell((short)26));//岗位
data.setString("positionCode", positionCode.toString().trim());
}
if(row.getCell((short)27)!=null){
Object employeeCode = ExcelUtil.getCellValue(row.getCell((short)27));//员工
data.setString("employeeCode", employeeCode.toString().trim());
}
if(row.getCell((short)28)!=null){
Object dimension1Code = ExcelUtil.getCellValue(row.getCell((short)28));//维度1
data.setString("dimension1Code", dimension1Code.toString().trim());
}
if(row.getCell((short)29)!=null){
Object dimension2Code = ExcelUtil.getCellValue(row.getCell((short)29));//维度2
data.setString("dimension2Code", dimension2Code.toString().trim());
}
if(row.getCell((short)30)!=null){
Object dimension3Code = ExcelUtil.getCellValue(row.getCell((short)30));//维度3
data.setString("dimension3Code", dimension3Code.toString().trim());
}
if(row.getCell((short)31)!=null){
Object dimension4Code = ExcelUtil.getCellValue(row.getCell((short)31));//维度4
data.setString("dimension4Code", dimension4Code.toString().trim());
}
if(row.getCell((short)32)!=null){
Object dimension5Code = ExcelUtil.getCellValue(row.getCell((short)32));//维度5
data.setString("dimension5Code", dimension5Code.toString().trim());
}
if(row.getCell((short)33)!=null){
Object dimension6Code = ExcelUtil.getCellValue(row.getCell((short)33));//维度6
data.setString("dimension6Code", dimension6Code.toString().trim());
}
if(row.getCell((short)34)!=null){
Object dimension7Code = ExcelUtil.getCellValue(row.getCell((short)34));//维度7
data.setString("dimension7Code", dimension7Code.toString().trim());
}
if(row.getCell((short)35)!=null){
Object dimension8Code = ExcelUtil.getCellValue(row.getCell((short)35));//维度8
data.setString("dimension8Code", dimension8Code.toString().trim());
}
if(row.getCell((short)36)!=null){
Object dimension9Code = ExcelUtil.getCellValue(row.getCell((short)36));//维度9
data.setString("dimension9Code", dimension9Code.toString().trim());
}
if(row.getCell((short)37)!=null){
Object dimension10Code = ExcelUtil.getCellValue(row.getCell((short)37));//维度10
data.setString("dimension10Code", dimension10Code.toString().trim());
}
data.setString("journalCompanyId",user[0].getUserOrgId());//公司ID
data.setString("bgtOrgCode", bgtOrgCode);//预算组织code
data.setDate("creationDate", new Date());//创建日期
data.setString("createdBy", user[0].getUserId());//创建人
DataObject tmp =DataObjectUtil.convertDataObject(data,BgtJournalInterface,false);
DatabaseExt.getPrimaryKey(tmp); //设置主键
DatabaseUtil.insertEntity("default", tmp);
}
}
manager.commit();
} catch (Exception e) {
manager.rollback();
throw e;
}
return list;
}
参数exportObjects里面传递已经查询出来的数据
download.jsp:导出数据后输出处理
<%@page pageEncoding="UTF-8"%><%@page import="java.net.URLEncoder,javax.servlet.ServletOutputStream,java.io.*,com.eos.foundation.common.utils.DateUtil,java.util.Date"%><%
//获取标签中使用的国际化资源信息
String filePath = request.getParameter("filePath").trim();
String fileName = request.getParameter("filename").trim();
byte[] buffer = new byte[512];
int size = 0;
boolean isIE = request.getHeader("USER-AGENT").toLowerCase().indexOf("msie")>0?true:false;
boolean isIE11 = request.getHeader("USER-AGENT").toLowerCase().indexOf("gecko")>0 && request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11")>0?true:false;
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel;charset=utf8");
fileName = fileName +"("+ DateUtil.format(new Date(),"yyyyMMddHHmmss") +").xls";
if(isIE){
fileName = URLEncoder.encode(fileName, "utf-8");
}else if(isIE11){
fileName = URLEncoder.encode(fileName, "utf-8");
}else{
fileName = new String(fileName.getBytes(),"ISO_8859_1");
}
response.setHeader("Content-disposition", "attachment;filename=\""+fileName+"\"");
ServletOutputStream os = null;
FileInputStream in = null;
try {
os = response.getOutputStream();
File downloadFile=new File(filePath);
if(downloadFile != null && downloadFile.exists()){
in = new FileInputStream(new File(filePath));
while ((size = in.read(buffer)) != -1) {
os.write(buffer, 0, size);
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(in!=null)in.close();
if(os!=null)os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
%>