按鈕:<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();
}
}
%>