在client端action包中创建exportaction和importaction Java类,在action同级创建一个dialog包用来存放exportdialog和filedialog Java类
exportaction.java
package nc.ui.jygyl.xmqddzb.ace.action;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.ui.jygyl.yzxzxxb.ace.dialog.ExportDialog;
import nc.ui.pubapp.uif2app.model.BillManageModel;
import nc.ui.pubapp.uif2app.view.ShowUpableBillForm;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.vo.jygyl.tool.NullValueUtils;
import nc.vo.jygyl.xmqddzb.AggXmqddzbVO;
import nc.vo.jygyl.xmqddzb.XmqddzbBVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 业主征询信息表导出
* @author wf
* @date 2023-05-29
*/
public class ExportAction extends NCAction{
private static final long serialVersionUID = -2288549251908687419L;
private BillManageModel model;
protected ShowUpableBillForm editor;
public ExportAction() {
setBtnName("导出");
setCode("exportAction");
}
@Override
public void doAction(ActionEvent arg0) throws Exception {
// TODO 自动生成的方法存根
ExportDialog fileDlg = new ExportDialog();
if (fileDlg.show()) {
// 得到导出的EXCEL文件路径
String filepath = fileDlg.getFilePath();
if (filepath == null) {
ShowStatusBarMsgUtil.showErrorMsg("提示", "请选择导出路径!", getModel()
.getContext());// 最下面红色显示
return;
}
try {
export(filepath);
} catch (Exception e) {
ExceptionUtils.wrappBusinessException("程序出错:" + e.getMessage());
}
}
ShowStatusBarMsgUtil.showStatusBarMsg("导出模板成功!", getModel()
.getContext());
}
public void export(String filepath) throws BusinessException {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
SXSSFWorkbook wb = new SXSSFWorkbook(xssfWorkbook, 100);
// 创建工作表
SXSSFSheet wbSheet = (SXSSFSheet) wb.createSheet(getExcelSheetName());
// 设置默认行宽
wbSheet.setDefaultColumnWidth(10);
// 标题样式(加粗,垂直居中)
XSSFCellStyle cellStyle = (XSSFCellStyle) wb.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中
XSSFFont fontStyle = (XSSFFont) wb.createFont();
fontStyle.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
fontStyle.setFontHeightInPoints((short) 16); // 设置标题字体大小
cellStyle.setFont(fontStyle);
// 设置表头样式,表头居中
XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle();
// 设置单元格样式
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 设置字体
XSSFFont font = (XSSFFont) wb.createFont();
font.setFontHeightInPoints((short) 9);
style.setFont(font);
// 在第1行创建rows
SXSSFRow row = (SXSSFRow) wbSheet.createRow((int) 0);
// 设置列头元素
SXSSFCell cellHead = null;
for (int i = 0; i < getExportFieldsName().length; i++) {
cellHead = (SXSSFCell) row.createCell(i);
cellHead.setCellValue(getExportFieldsName()[i]);
cellHead.setCellStyle(style);
}
// 获取页面所有数据
// AggXmqddzbVO aggvo = (AggXmqddzbVO) getModel().getSelectedData();
Object[] objs=this.model.getSelectedOperaDatas();
if (objs == null) {
throw new BusinessException(" 未选择导出数据! ");
}
List<AggXmqddzbVO> aggvolist=new ArrayList<AggXmqddzbVO>();
for (Object obj : objs) {
//aggvo
AggXmqddzbVO aggvo = (AggXmqddzbVO)obj;
aggvolist.add(aggvo);
}
if (aggvolist != null && aggvolist.size() > 0) {
for (int i = 0; i < aggvolist.size(); i++) {
XmqddzbBVO[] bvos = (XmqddzbBVO[]) aggvolist.get(i).getChildren(XmqddzbBVO.class);
if (bvos != null && bvos.length > 0) {
for (int j = 0; j < bvos.length; j++) {
// 在第2行创建rows
SXSSFRow rows = (SXSSFRow) wbSheet.createRow(j+1);
// 设置列头元素
SXSSFCell cellHeads = null;
// 写入数据内容
String[] fieldsValue = new String[] {
NullValueUtils.getNullStringValue(getCfwlbm(bvos[j].getCfwlbm())),
NullValueUtils.getNullStringValue(bvos[j].getCfwlmc()),
NullValueUtils.getNullStringValue(bvos[j].getCfgg()),
NullValueUtils.getNullStringValue(bvos[j].getCfxh()),
NullValueUtils.getNullStringValue(bvos[j].getPmwlbm()),
NullValueUtils.getNullStringValue(bvos[j].getPmwlmc()),
NullValueUtils.getNullStringValue(bvos[j].getPmgg()),
NullValueUtils.getNullStringValue(bvos[j].getPmxh())
};
// 开始写入数据,l为列值
for (int l = 0; l < fieldsValue.length; l++) {
cellHeads = (SXSSFCell) rows.createCell(l);
cellHeads.setCellValue(fieldsValue[l]);
cellHeads.setCellStyle(style);
}
}
}
try {
FileOutputStream fout = new FileOutputStream(filepath
+ "\\项目清单对照表"
+ new UFDate().toStdString().substring(0, 10) + ".xlsx");
wb.write(fout);
fout.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
throw new BusinessException("生成excel出错:" + e.getMessage());
}
}
}
}
private Object getCfwlbm(String cfwlbm) throws BusinessException {
// TODO 自动生成的方法存根
String sql = " select code from bd_material where isnull(dr,0) = 0 and pk_material = '"+cfwlbm+"' ";
String code = NullValueUtils.getNullStringValue(getService().executeQuery(sql, new ColumnProcessor()));
if (code != null && !"".equals(code)) {
return code.toString();
}
return null;
}
private String[] getExportFieldsName() {
return new String[] { "CF物料编码", "CF物料名称", "CF规格", "CF型号", "PM物料编码",
"PM物料名称", "PM规格", "PM型号"};
}
private String getExcelSheetName() {
String date = new UFDate().toString().substring(0, 10);
return "项目清单对照表" + date;
}
public BillManageModel getModel() {
return model;
}
public void setModel(BillManageModel model) {
this.model = model;
this.model.addAppEventListener(this);
}
public ShowUpableBillForm getEditor() {
return editor;
}
public void setEditor(ShowUpableBillForm editor) {
this.editor = editor;
}
private IUAPQueryBS service;
private IUAPQueryBS getService() {
if(service == null) {
service = NCLocator.getInstance().lookup(IUAPQueryBS.class);
}
return service;
}
}
importaction.java
package nc.ui.jygyl.xmqddzb.ace.action;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import nc.bs.framework.common.NCLocator;
import nc.itf.jygyl.IXmqddzbMaintain;
import nc.ui.jygyl.yzxzxxb.ace.dialog.FileDialog;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pubapp.uif2app.model.BillManageModel;
import nc.ui.pubapp.uif2app.view.ShowUpableBillForm;
import nc.ui.uif2.NCAction;
import nc.vo.jygyl.tool.NullValueUtils;
import nc.vo.jygyl.xmqddzb.AggXmqddzbVO;
import nc.vo.jygyl.xmqddzb.XmqddzbBVO;
import nc.vo.jygyl.xmqddzb.XmqddzbVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.AppContext;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 项目清单对照表导入
* @author wf
*
*/
public class ImportAction extends NCAction{
/**
*
*/
private static final long serialVersionUID = 1L;
private BillManageModel model;
protected ShowUpableBillForm editor;
public ImportAction() {
// TODO 自动生成的构造函数存根
setBtnName("导入");
setCode("ImportAction");
}
@Override
public void doAction(ActionEvent e) throws Exception {
FileDialog fileDlg = new FileDialog();
if(fileDlg.show()){
//得到导入的EXCEL文件路径
String filepath = fileDlg.getFilePath();
if (filepath == null) {
MessageDialog.showErrorDlg(null, "", "请选择文件");
return;
}
try {
List<AggXmqddzbVO> voslist = new ArrayList<AggXmqddzbVO>();
InputStream input = null;
XSSFWorkbook wb = null;
try {
input = new FileInputStream(filepath);
wb = new XSSFWorkbook(input);
XSSFSheet xssfSheet = wb.getSheetAt(0);
if (xssfSheet == null) {
throw new BusinessException("解析excel出错:未读取到页签!");
}
int totalRows = xssfSheet.getPhysicalNumberOfRows();// 行数
int totalCells = xssfSheet.getRow(0).getLastCellNum();// 列数
if (totalRows <= 0) {
throw new BusinessException("Excel为空,请检查!");
}
//for (int i = 1; i < 2; i++) {
AggXmqddzbVO aggvo = new AggXmqddzbVO();
XmqddzbVO vo = new XmqddzbVO();
//创建时间
vo.setDbilldate(NullValueUtils.getUFDateNullValue(xssfSheet.getRow(1).getCell(0).getStringCellValue()));
//项目编号
vo.setXmbh(NullValueUtils.getNullStringValue(xssfSheet.getRow(1).getCell(1).getStringCellValue()));
vo.setVbillstatus(-1);
//单据类型
vo.setPk_billtype("QDDZ");
//单据编码
vo.setTranstypecode("QDDZ");
//集团
vo.setPk_group(AppContext.getInstance().getPkGroup());
//组织
vo.setPk_org("001");
vo.setPk_org_v(vo.getPk_org());
vo.setVbillstatus(-1);
//制单时间
vo.setCreationtime(new UFDateTime());
//制单人
vo.setCreator(AppContext.getInstance().getPkUser());
vo.setStatus(VOStatus.NEW);
aggvo.setParent(vo);
SuperVOUtil.execFormulaWithVOs(new XmqddzbVO[]{vo}, new String[]{
"xmbh->getcolvalue(bd_project,pk_project ,project_code,\""+vo.getXmbh()+"\")",
"pk_org->getcolvalue(org_orgs,pk_org,code,\""+vo.getPk_org()+"\")",
});
List<XmqddzbBVO> bvolist = new ArrayList<XmqddzbBVO>();
for (int z = 3; z < totalRows; z++) {
XmqddzbBVO bvo = new XmqddzbBVO();
//CF物料编码
bvo.setCfwlbm(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(0).getStringCellValue()));
//CF物料名称
bvo.setCfwlmc(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(1).getStringCellValue()));
//CF物料规格
bvo.setCfgg(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(2).getStringCellValue()));
//CF物料型号
bvo.setCfxh(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(3).getStringCellValue()));
//PM物料编码
bvo.setPmwlbm(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(4).getStringCellValue()));
//PM物料名称
bvo.setPmwlmc(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(5).getStringCellValue()));
//PM物料规格
bvo.setPmgg(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(6).getStringCellValue()));
//PM物料型号
bvo.setPmxh(NullValueUtils.getNullStringValue(xssfSheet.getRow(z).getCell(7).getStringCellValue()));
bvolist.add(bvo);
aggvo.setChildren(XmqddzbBVO.class, bvolist.toArray(new XmqddzbBVO[bvolist.size()]));
SuperVOUtil.execFormulaWithVOs(new XmqddzbBVO[]{bvo}, new String[]{
"cfwlbm->getcolvalue(bd_material,pk_material,code,\""+bvo.getCfwlbm()+"\")",
});
}
voslist.add(aggvo);
//}
AggXmqddzbVO[] vos = getService().insert(voslist.toArray(new AggXmqddzbVO[0]), voslist.toArray(new AggXmqddzbVO[0]));
getModel().initModel(vos);
} catch (Exception e1) {
throw new BusinessException(e1.getMessage());
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e1) {
}
}
}catch (Exception e1) {
e1.printStackTrace();
MessageDialog.showErrorDlg(null, "", "导入失败," + e1.getMessage());
return;
}
MessageDialog.showHintDlg(null, "", "导入成功!");
}
}
private IXmqddzbMaintain service;
private IXmqddzbMaintain getService() {
if (service == null) {
service = NCLocator.getInstance().lookup(
IXmqddzbMaintain.class);
}
return service;
}
@Override
public boolean isActionEnable() {
return true;
}
public BillManageModel getModel() {
return model;
}
public void setModel(BillManageModel model) {
this.model = model;
this.model.addAppEventListener(this);
}
public ShowUpableBillForm getEditor() {
return editor;
}
public void setEditor(ShowUpableBillForm editor) {
this.editor = editor;
}
}
exportdialog.java
package nc.ui.jygyl.xmqddzb.ace.dialog;
import javax.swing.JFileChooser;
public class ExportDialog {
JFileChooser jFileChooser = new JFileChooser();
public ExportDialog() {
super();
}
public boolean show() {
jFileChooser.removeChoosableFileFilter(jFileChooser
.getAcceptAllFileFilter());
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
return JFileChooser.APPROVE_OPTION == jFileChooser.showOpenDialog(null);
}
public String getFilePath() {
return jFileChooser.getSelectedFile().getPath();
}
}
filedialog.java
package nc.ui.jygyl.xmqddzb.ace.dialog;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
public class FileDialog {
/**
* @describe:excel文件选择对话框
* @author:wf
* @date:2023-05-29
*/
JFileChooser jFileChooser = new JFileChooser();
public FileDialog() {
super();
}
public boolean show(){
jFileChooser.removeChoosableFileFilter(jFileChooser.getAcceptAllFileFilter());
jFileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if(f.getName().endsWith(".xlsx")||f.isDirectory()){
return true;
}
return false;
}
public String getDescription() {
// TODO 自动生成方法存根
return "所有文件(*.xlsx)";
}
});
return JFileChooser.APPROVE_OPTION == jFileChooser.showOpenDialog(null);
}
public String getFilePath(){
return jFileChooser.getSelectedFile().getPath();
}
}
之后在xml中添加导入导出的按钮(如下位置,只在非编辑态下可见):
<!-- 列表显示下的按钮 -->
<bean id="actionsOfList" class="nc.ui.uif2.actions.StandAloneToftPanelActionContainer">
<constructor-arg ref="billListView" />
<property name="model" ref="bmModel" />
<property name="actions">
<list>
<ref bean="addAction" />
<ref bean="editAction" />
<ref bean="deleteScriptAction" />
<ref bean="defaultQueryAction" />
<ref bean="copyAction" />
<ref bean="separatorAction" />
<ref bean="defaultRefreshAction" />
<ref bean="separatorAction" />
<ref bean="commitScriptAction" />
<ref bean="unCommitScriptAction" />
<ref bean="approveScriptAction" />
<ref bean="uNApproveScriptAction" />
<ref bean="separatorAction" />
<ref bean="linkQueryAction" />
<ref bean="separatorAction" />
<ref bean="metaDataBasedPrintAction" />
<ref bean="metaDataBasedPrintActiona" />
<ref bean="outputAction" />
<ref bean="separatorAction" />
<ref bean="importAction" />
<ref bean="exportAction" />
</list>
</property>
</bean>
<!-- 卡片显示下的按钮 -->
<bean id="actionsOfCard" class="nc.ui.uif2.actions.StandAloneToftPanelActionContainer">
<constructor-arg ref="billForm" />
<property name="model" ref="bmModel" />
<!-- 此处配置的Action浏览态始终可见 -->
<property name="actions">
<list>
<ref bean="addAction" />
<ref bean="editAction" />
<ref bean="deleteScriptAction" />
<ref bean="defaultQueryAction" />
<ref bean="separatorAction" />
<ref bean="copyAction" />
<ref bean="cardRefreshAction" />
<ref bean="separatorAction" />
<ref bean="commitScriptAction" />
<ref bean="unCommitScriptAction" />
<ref bean="approveScriptAction" />
<ref bean="uNApproveScriptAction" />
<ref bean="separatorAction" />
<ref bean="linkQueryAction" />
<ref bean="separatorAction" />
<ref bean="metaDataBasedPrintAction" />
<ref bean="metaDataBasedPrintActiona" />
<ref bean="outputAction" />
<ref bean="separatorAction" />
<ref bean="pFApproveStatusInfoAction" />
<ref bean="separatorAction" />
<ref bean="importAction" />
<ref bean="exportAction" />
</list>
</property>
.
.
.
.
.
.
.
.
.
.
<!--======= 动作:[newActions] [导入] ===========-->
<bean id="importAction" class="nc.ui.jygyl.xmqddzb.ace.action.ImportAction">
<property name="model"><ref bean="bmModel"/></property>
<property name="editor"><ref bean="billForm"/></property>
<property name="exceptionHandler"><ref bean="exceptionHandler" /></property>
</bean>
<!--======= 动作:[newActions] [导出] ===========-->
<bean id="exportAction" class="nc.ui.jygyl.xmqddzb.ace.action.ExportAction">
<property name="model"><ref bean="bmModel"/></property>
<property name="editor"><ref bean="billForm"/></property>
<property name="exceptionHandler"><ref bean="exceptionHandler" /></property>
</bean>
<!--分隔按钮-->
<bean id="separatorAction" class="nc.funcnode.ui.action.SeparatorAction" />
<bean id="exceptionHandler" class="nc.ui.uif2.DefaultExceptionHanler">
<constructor-arg><ref bean="container"/></constructor-arg>
<property name="context"><ref bean="context" /></property>
</bean>
<!-- 批量保存校验服务类 -->
</beans>
最后,登录客户端,动态建模平台→开发配置→功能注册中去修改其参数值: