Xpages实现导入导出功能需要相关的java包,首先要将包添加到项目当中,具体操作如下:
1、打开窗口----打开eclipse视图----打开包资源管理器。这样就进入到一个类似eclipse设计的视图,在这里有项目的所有设计元素。
2、将java包(需要的包在rar文件中)粘贴到WebContent-------WEB-INF--------lib文件夹下,如图。
3、在项目上单击右键---构建路径---配置构建路径(类似eclipse的构建环境),选择 库,然后单击添加JAR,找到第2步copy过来所有的包,然后添加即可,如图。
Xpages的导出功能较为简单,与CS端并没有很大的出入,例子为<销售平台内控关差平台>,例子为根据用户选择的条件来导入,如果不需要可以自行简化,具体操作如下:
1、创建一个定制控件,创建各个条件的域与导出按钮。如图。
2、在代码----java---中创建新java类,名称为xpages/MyReport.java。
3、在第一步创建的定制控件中的导出按钮里添加以下代码:
//获得组件
Var djDateTextBox1:com.ibm.xsp.extlib.component.dojo.form.UIDojoDateTextBox = getComponent("djDateTextBox1");
var djDateTextBox2:com.ibm.xsp.extlib.component.dojo.form.UIDojoDateTextBox = getComponent("djDateTextBox2");
//获得用户选择条件
requestScope.put("dc_auditStart",djDateTextBox1.value);
requestScope.put("dc_auditOver",djDateTextBox2.value);
requestScope.put("dc_auditType",currentDocument.getItemValue("dc_auditType"));
requestScope.put("dc_venture",currentDocument.getItemValue("dc_venture"));
requestScope.put("dc_area",currentDocument.getItemValue("dc_area"));
//创建java对象,执行exportExcel方法
var myExportTool = new xpages.MyReport();
myExportTool.exportExcel();
4、在第二步创建的java类中添加如下代码
package xpages;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.local.DateTime;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFCreationHelper;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
public class MyReport {
public void exportExcel() {
// 创建一个新工作簿对象从poi库
HSSFWorkbook wb = new HSSFWorkbook();
// 创建额外的表使用相同的语法和不同的表的名称
HSSFSheet sheet1 = wb.createSheet("firstsheet");
// 创建助手类和日期风格
HSSFCreationHelper createHelper = wb.getCreationHelper();
HSSFCellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
HSSFCellStyle headerStyle = wb.createCellStyle();
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
// 为电子表格创建文件名
String fileName = "report.xls";
FacesContext context = FacesContext.getCurrentInstance();
Session session = (Session) context.getApplication()
.getVariableResolver().resolveVariable(context, "session");
// Faces上下文全局对象提供了访问servlet
// 通过外部环境的内容
ExternalContext extCont = context.getExternalContext();
// servlet的响应对象提供的控制响应对象
HttpServletResponse pageResponse = (HttpServletResponse) extCont
.getResponse();
HttpServletRequest request = (HttpServletRequest) extCont.getRequest();
//创建表头
HSSFRow row = sheet1.createRow(0);
HSSFCell hCell = row.createCell(0);
hCell.setCellValue("统计导出");
hCell.setCellStyle(headerStyle);
CellRangeAddress cra=new CellRangeAddress(0, 0, 0, 99);
sheet1.addMergedRegion(cra);
//第一行(部分代码)
row = sheet1.createRow(1);
row.setRowStyle(headerStyle);
row.createCell(0).setCellValue("审计类型");
cra = new CellRangeAddress(1, 1, 0, 0);
sheet1.addMergedRegion(cra);
row.createCell(1).setCellValue("经营体维度");
cra = new CellRangeAddress(1, 1, 1, 1);
sheet1.addMergedRegion(cra);
row.createCell(2).setCellValue("区域维度");
cra = new CellRangeAddress(1, 1, 2, 2);
sheet1.addMergedRegion(cra);
//获取条件数据(请参照第三步中获得的数据)
Vector dc_auditType = (Vector)request.getAttribute("dc_auditType");
Vector dc_venture = (Vector)request.getAttribute("dc_venture");
Vector dc_area = (Vector)request.getAttribute("dc_area");
Date dc_auditStart = (Date)request.getAttribute("dc_auditStart");
Date dc_auditOver = (Date)request.getAttribute("dc_auditOver");
//数据导出(下面是你需要自己修改的代码了)
Database db = null;
View view = null;
Document doc = null;
try {
db = session.getCurrentDatabase();
view = db.getView("vh_main");
doc = view.getFirstDocument();
} catch (Exception e1) {
e1.printStackTrace();
}
int rowNum = 2;//数据行的位置
while(doc != null){
//判断文档是否符合条件
boolean bl1 = false;//审计类型判断
boolean bl2 = false;//经营体维度判断
boolean bl3 = false;//区域维度判断
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean dt1 = false;
boolean dt2 = false;
System.out.print(dc_auditType);
try {
if(" ".equals(dc_auditType.get(0))){
bl1 = true;
}
else if (dc_auditType.get(0).equals(String.valueOf(doc.getItemValue("auditType").get(0)))){
bl1 = true;
}
if(" ".equals(dc_venture.get(0))){
bl2 = true;
}
else if (dc_venture.get(0).equals(String.valueOf(doc.getItemValue("venture").get(0)))){
bl2 = true;
}
if(" ".equals(dc_area.get(0))){
bl3 = true;
}
else if (dc_area.get(0).equals(String.valueOf(doc.getItemValue("area").get(0)))){
bl3 = true;
}
if(doc.getItemValue("auditStart") != null && doc.getItemValue("auditStart").size() != 0 && dc_auditStart != null && dc_auditOver != null){//时间不是空,则进行判断
String x = String.valueOf(doc.getItemValue("auditStart").get(0));
String y = String.valueOf(doc.getItemValue("auditOver").get(0));
if(x.compareTo( formatter.format(dc_auditStart)) >0 && y.compareTo( formatter.format(dc_auditOver)) <0 ){
dt1 = true;
}
}
else{
dt1 = true;
}
}
catch (Exception e2) {
System.out.println("比较出现异常biubiubiu~~~~~");
e2.printStackTrace();
}
//导出文档中的数据(这是导出内容的核心数据)
if(bl1 && bl2 && bl3 && dt1){
try{
row = sheet1.createRow(rowNum);
row.createCell(0).setCellValue(doc.getItemValue("auditType") != null && doc.getItemValue("auditType").size() != 0?String.valueOf(doc.getItemValue("auditType").get(0)):"");//审计类型
row.createCell(1).setCellValue(doc.getItemValue("venture") != null && doc.getItemValue("venture").size() != 0?String.valueOf(doc.getItemValue("venture").get(0)):"");//经营体维度
row.createCell(2).setCellValue(doc.getItemValue("area") != null && doc.getItemValue("area").size() != 0?String.valueOf(doc.getItemValue("area").get(0)):"");
row.createCell(3).setCellValue(doc.getItemValue("auditStart") != null && doc.getItemValue("auditStart").size() != 0?String.valueOf(doc.getItemValue("auditStart").get(0)):"");
}
catch (Exception e1) {
e1.printStackTrace();
}
rowNum++;
}
try {
doc = view.getNextDocument(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
//下面的代码不需要关心,复制就可以了
ServletOutputStream pageOutput = null;
try {
pageOutput = pageResponse.getOutputStream();
pageResponse.setContentType("application/x-ms-excel");
pageResponse.setHeader("Cache-Control", "no-cache");
pageResponse.setHeader("Content-Disposition", "inline; filename="
+ fileName);
// Write the output, flush the buffer and close the stream
wb.write(pageOutput);
pageOutput.flush();
pageOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
context.responseComplete();
}
}
5、以上就完成了导出功能,需要注意的是导出功能较难调试,很有可能出现死循环等错误,调试需要在代码中多处print,然后查看控制台。
在Xpages中导入功能目前并不人性化,但是基础功能是可以实现的。实现的原理是用户事先将一个表格存放在一个视图的第一个文档中,然后再点击导入实现功能。具体操作如下:
1、首先需要一个导入的视图,再创建一个导入的表单,表单中有作者,有文件上载和文件下载两个组件,如图。
2、在java中创建类,类名为xpages/MyImpotTool_1.java。
3、在视图中添加导入按钮,按钮中加入以下代码:
var myImportTool = new xpages.MyImportTool_1();
myImportTool.importExcel();
view.postScript(" alert('导入成功!'); ");
4、在第二步创建的java类中添加以下代码:
package xpages;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.RichTextItem;
import lotus.domino.Session;
import lotus.domino.View;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.util.*;
import lotus.domino.*;
public class MyImportTool_1 {
public void importExcel() {
FacesContext context = FacesContext.getCurrentInstance();
Session session = (Session) context.getApplication()
.getVariableResolver().resolveVariable(context, "session");
ExternalContext extCont = context.getExternalContext();
HttpServletResponse pageResponse = (HttpServletResponse) extCont
.getResponse();
HttpServletRequest request = (HttpServletRequest) extCont.getRequest();
try {
Database database = session.getCurrentDatabase();
View view = database.getView("导入表格所在的视图");
Document doc = view.getFirstDocument();
RichTextItem body = null;
body = (RichTextItem) doc.getFirstItem("data");
// String path = jhFile.getBody().getAbsolutePath();
//body.getEmbeddedObject("").getInputStream();
EmbeddedObject e = (EmbeddedObject)body.getEmbeddedObjects().get(0);
InputStream is = e.getInputStream();
byte[] buf = IOUtils.toByteArray(is);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
POIFSFileSystem fs;
HSSFWorkbook wb;
HSSFSheet sheet;
HSSFRow row;
fs = new POIFSFileSystem(byteArrayInputStream);
wb = new HSSFWorkbook(fs);
sheet = wb.getSheetAt(0);
System.out.print("2");
//遍历所有的行,取出数据,创建文档
for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
row=sheet.getRow(i);
Document cpdoc = database.createDocument();
cpdoc.replaceItemValue("form", "main");
//cpdoc.replaceItemValue("cjdate", new Date());
//DateTime dt = session.createDateTime("Today");
//dt.setNow();
//主表单隐藏域中的信息
cpdoc.replaceItemValue("author", session.getUserName());
cpdoc.replaceItemValue("step",0);
cpdoc.replaceItemValue("status", "导入待提交");
cpdoc.replaceItemValue("lotNumber","无编号" );
System.out.print("3");
if(row.getCell(1) != null){
cpdoc.replaceItemValue("auditType", row.getCell(1).getStringCellValue()); //审计类型
}
if(row.getCell(2) != null){
cpdoc.replaceItemValue("venture", row.getCell(2).getStringCellValue());//经营体维度
}
if(row.getCell(3) != null){
cpdoc.replaceItemValue("area", row.getCell(3).getStringCellValue());//区域维度
}
System.out.println(i);
cpdoc.save();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}