因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
如果我有帮助到你 帮忙点个赞 谢谢
如果有问题 请联系我 我会尽力帮助你