📄 importinfodlg.cpp
字号:
// ImportInfoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "StudentCheck.h"
#include "ImportInfoDlg.h"
#include "Excel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImportInfoDlg dialog
CImportInfoDlg::CImportInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImportInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CImportInfoDlg)
m_strPathName = _T("");
m_strRow = _T("");
//}}AFX_DATA_INIT
m_CurAdo.InitialDB();
}
CImportInfoDlg::~CImportInfoDlg()
{
m_CurAdo.ClosedDB();
}
void CImportInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CImportInfoDlg)
DDX_Control(pDX, IDC_BUTTON_SELECT, m_ctrlSelect);
DDX_Text(pDX, IDC_PATHNAME_EDIT, m_strPathName);
DDX_Text(pDX, IDC_STATIC_ROW, m_strRow);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CImportInfoDlg, CDialog)
//{{AFX_MSG_MAP(CImportInfoDlg)
ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImportInfoDlg message handlers
void CImportInfoDlg::OnButtonSelect()
{
// TODO: Add your control notification handler code here
//显示文件打开对话框
CFileDialog dlg(TRUE, NULL, "*.xls",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"All Files(*.xls)|*.xls");
if ( dlg.DoModal()!=IDOK )
return;
//获取文件的绝对路径
CString sFileName=dlg.GetPathName();
this->m_strPathName=sFileName;
if(!this->m_strPathName.IsEmpty())
GetDlgItem(IDC_BUTTON_IMPORT)->EnableWindow(TRUE);
UpdateData(FALSE);
}
void CImportInfoDlg::OnButtonImport()
{
// TODO: Add your control notification handler code here
UpdateData();//更新数据
CString str;
CString m_strRight;
CString strExcelFile ;
strExcelFile=this->m_strPathName;
if(strExcelFile.GetLength()<=0)
{
MessageBox("请选择Excel文件!","选择文件");
return;
}
else
{
str=strExcelFile.Right(4);
str.MakeLower();
if(str.Collate(".xls"))
{
MessageBox("请选择正确的Excel文件!","文件格式");
return;
}
}
if(ImportData())
MessageBox("导入数据成功!","信息提示");
else
MessageBox("导入数据出错!","信息提示");
}
BOOL CImportInfoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//如果没有选择文件,则将“导入”按钮置为相应的不可选
GetDlgItem(IDC_BUTTON_IMPORT)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CImportInfoDlg::ImportData()
{
//导入数据
CString strFile;
strFile=this->m_strPathName;
CExcel excel( strFile , "Sheet1" );
nCol = excel.GetTotalCuloumns();
nRow = excel.GetTotalLows();
if(nCol<=11)
{
CString str;
str.Format("Sheet1共%i条记录,",nRow-1);//减去表头
this->m_strRow=str;
UpdateData(FALSE);
CStringArray FirstItems[11];
for(int i=1; i<=nCol; i++)
{
CString str;
str=excel.Read(0,i);
FirstItems[i-1].Add(str);
}
CString Fields[11]; //存放表目
for(int k=0;k<nCol;k++)
{
Fields[k]=FirstItems[k].GetAt(0);
Fields[k].TrimRight();//去掉后面的空白字符
}
if(Fields[0]=="学号" &&
Fields[1]=="姓名" &&
Fields[2]=="性别" &&
Fields[3]=="出生日期" &&
Fields[4]=="年级" &&
Fields[5]=="学院" &&
Fields[6]=="专业" &&
Fields[7]=="班级" &&
Fields[8]=="公寓号" &&
Fields[9]=="寝室号" &&
Fields[10]=="担任职务" )
{
//把Sheet1中的数据存储在变量中
for(i=1; i<=nCol; i++)
for(int j=1; j<=nRow-1; j++)
{
//读取两个表当前项的值
CString str1;
str1=excel.Read(j,i);
saveItems[i-1].Add(str1);
}
CString items[11];//存放一行的数据
for(int nIndex=0;nIndex<nRow-1;nIndex++)
{
for(k=0;k<nCol;k++)
{
items[k]=saveItems[k].GetAt(nIndex);
}
//将取得的值插入到数据库中
_bstr_t vSQL;
BOOL bCount;
//根据学号查询数据库中是否有此学号对应的记录
//如果有,则进行更新操作,如果没有则进行插入操作
CString BirthDay=items[3].Left(4)+"-"+items[3].Mid(4,2)+"-"+items[3].Mid(6,2);
vSQL="Select StudentNum from Student where StudentNum='"+items[0]+"'";
bCount=m_CurAdo.GetRecordCount(vSQL);
if(bCount)//进行更新操作
{
vSQL="Update Student Set Name='"+items[1]+
"',Sex='"+items[2]+
"',BirthDay='"+BirthDay+
"',ClassID='"+items[7]+
"',SpecialtyID='"+items[6]+
"',CollegeID='"+items[5]+
"',GradeID='"+items[4]+
"',BuildNum='"+items[8]+
"',RommNum='"+items[9]+
"',StuOfficer='"+items[10]+"' Where StudentNum='"+items[0]+"'";
m_CurAdo.ExecuteSQL(vSQL);
}
else //进行插入操作
{
vSQL="Insert into Student(StudentNum,Name,Sex,BirthDay,ClassID,SpecialtyID,CollegeID,GradeID,BuildNum,RommNum,StuOfficer) \
Values('"+items[0]+"','"//学号
+items[1]+"','" //姓名
+items[2]+"','" //性别
+BirthDay+"','" //出生日期
+items[7]+"','" //班级号
+items[6]+"','" //专业号
+items[5]+"','" //学院号
+items[4]+"','" //年级号
+items[8]+"','" //公寓号
+items[9]+"','" //寝室号
+items[10]+"')"; //担任职务
m_CurAdo.ExecuteSQL(vSQL);
}
}
}
else
{
MessageBox("Excel文件的Sheet1工作区中第一行必须是(包括排列顺序):\n[学号、姓名、性别、出生日期、年级、学院、专业、班级、公寓号、寝室号、担任职务]",
"错误提示");
return FALSE;
}
}
else
{
MessageBox("Excel文件的Sheet1工作区中必须是11列(包括排列顺序):\n[学号、姓名、性别、出生日期、年级、学院、专业、班级、公寓号、寝室号、担任职务]",
"错误提示");
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -