NC63重写导入功能

因NC中提供的导入导出功能不能够满足开发的需求,故重写了导入导出功能,下面为导入功能的代码,导入我们只导入了表体的字段,有需求可以自行更改。仅供参考。

/**
 * @author conn
 * @date 2019-9-9
 */
package nc.ui.tzsc.bs.action;

import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;

import nc.pub.templet.converter.util.helper.ExceptionUtils;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.report.ReportBaseClass;
import nc.ui.pubapp.uif2app.view.BillForm;
import nc.ui.trade.business.HYPubBO_Client;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.editor.IEditor;
import nc.ui.uif2.model.AbstractAppModel;
import nc.uif.pub.exception.UifException;
import nc.vo.pub.lang.UFDate;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * @author conn
 * @date 2019-9-9
 */
public class ImportBth extends NCAction {
	private AbstractAppModel model;
	private IEditor editor;
	protected static JFileChooser m_chooser = null;
	protected static ReportBaseClass tp;

	public AbstractAppModel getModel() {
		return model;
	}

	public void setModel(AbstractAppModel model) {
		this.model = model;
	}

	public IEditor getEditor() {
		return editor;
	}

	public void setEditor(IEditor editor) {
		this.editor = editor;
	}

	public ImportBth() {
		super();
		this.setBtnName("导入Excel");
		this.setCode("BtnImport");
	}

	/**
	 * 获得文件选择器
	 * 
	 * @author conn
	 * @return
	 * @return JFileChooser
	 * @date 2019-9-10
	 */
	public JFileChooser getChooser() {
		if (m_chooser == null) {
			m_chooser = new JFileChooser();
			m_chooser.setDialogType(JFileChooser.SAVE_DIALOG);
		}
		return m_chooser;
	}

	/**
	 * Excel数据导入
	 * 
	 * @author conn
	 * @throws Exception
	 * @return void
	 * @date 2019-9-11
	 */
	public void ImportData() {
		String filePath = null;
		BillCardPanel cp = ((BillForm) this.editor).getBillCardPanel();
		FileInputStream fis = null;
		HSSFWorkbook hw = null;
		
		//new一个新的文件选择器并打开
		JFileChooser jfile = new JFileChooser();
		jfile.setDialogType(JFileChooser.SAVE_DIALOG);
		if (jfile.showSaveDialog(tp) == JFileChooser.CANCEL_OPTION) {
			return;
		}

		// 获得选择的文件名
		filePath = jfile.getSelectedFile().toString();
		try {
			fis = new FileInputStream(filePath);
		} catch (FileNotFoundException e) {
			ExceptionUtils.wrapException(new Exception(e.getMessage()));
			e.printStackTrace();
		}
		
		try {
			hw = new HSSFWorkbook(fis);
		} catch (IOException e) {
			e.printStackTrace();
			ExceptionUtils.wrapException(new Exception(e.getMessage()));
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
				ExceptionUtils.wrapException(new Exception(e.getMessage()));
			}
		}
		// sheet 从0开始
		HSSFSheet sheet = hw.getSheetAt(0);
		// 行长度
		int rowNum = sheet.getLastRowNum() + 1;

		// 行循环开始
		for (int i = 1; i < rowNum; i++) {
			// 插入行
			cp.addLine();
			cp.setBodyValueAt(10 * i, i - 1, "crown");
			
			//页面根据表头的集团、组织填充表体集团、组织
			cp.setBodyValueAt(cp.getHeadItem("pk_group").getValueObject().toString(), i - 1, "pk_group");
			cp.setBodyValueAt(cp.getHeadItem("pk_org").getValueObject().toString(), i - 1, "pk_org");

			// excel第一行
			HSSFRow row1 = sheet.getRow(0);
			// 获得行
			HSSFRow row = sheet.getRow(i);
			// 列长度
			int colNum = row1.getLastCellNum();
			// 列循环开始
			for (int j = 0; j < colNum; j++) {
			    //取第一行第J列的列名中文字段后的字段名
				String firstRowCell = row1.getCell(j).getStringCellValue().split(",")[1];
				//获得第J列的单元格 从第二行开始
				HSSFCell cell = row.getCell(j);
				
				//声明一个单元格值
				Object cellvalue = null;
				if (null != cell) {
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					cellvalue = cell.getStringCellValue();
				}
				
				//将Excel中填充的编码转换为可以保存的主键值
				if (null != cellvalue) {
					try {
						if (firstRowCell.equals("pk_cust")) {
							cellvalue = (String) HYPubBO_Client.findColValue("bd_customer", "pk_customer", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("pk_cust_b")) {
							cellvalue = (String) HYPubBO_Client.findColValue("bd_customer", "pk_customer", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("pk_inv")) {
							cellvalue = (String) HYPubBO_Client.findColValue("bd_material", "pk_material", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("pk_dept")) {
							cellvalue = (String) HYPubBO_Client.findColValue("org_dept", "pk_dept", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("pk_person")) {
							cellvalue = (String) HYPubBO_Client.findColValue("bd_psndoc", "pk_psndoc", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("subsettlepro")) {
							cellvalue = (String) HYPubBO_Client.findColValue("tzsc_subsettle", "pk_subsettle", "code='"
									+ cellvalue + "' and dr=0");
						} else if (firstRowCell.equals("pk_custype")) {
							cellvalue = (String) HYPubBO_Client.findColValue("bd_custclass", "pk_custclass", "code='" + cellvalue
									+ "' and dr=0");
						} else if (firstRowCell.equals("effdate")) {
						    //转换为NC中日期格式数据
							cellvalue = new UFDate(HSSFDateUtil.getJavaDate(Double.parseDouble(cellvalue.toString())));
						} else if (firstRowCell.equals("expdate")) {
							cellvalue = new UFDate(HSSFDateUtil.getJavaDate(Double.parseDouble(cellvalue.toString())));
						}
					} catch (NumberFormatException e) {
						e.printStackTrace();
						ExceptionUtils.wrapException(new Exception(e.getMessage()));
					} catch (UifException e) {
						e.printStackTrace();
						ExceptionUtils.wrapException(new Exception(e.getMessage()));
					}
				}
				//设置表体的值
				cp.setBodyValueAt(cellvalue, i - 1, firstRowCell);

			}
		}
		
		cp.getBillModel().loadLoadRelationItemValue();
		cp.getBillModel().execLoadFormula();
	}

	@Override
	public void doAction(ActionEvent event) {
		ImportData();
	}

}

XML中需要配置导入按钮

	<!-- 导入按钮 -->
	<bean id="btnImport" class="nc.ui.tzsc.bs.action.ImportBth">
		<property name="model" ref="manageAppModel" />
		<property name="editor" ref="billFormEditor" />
	</bean>

姓 名:Conn
邮 箱:tzconn@163.com
如果我有帮助到你 帮忙点个赞 谢谢
如果有问题 请联系我 我会尽力帮助你


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的世界没光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值