学生信息管理系统v1.0

  昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙。需要统计处理很多学生信息,想让我帮他做一个管理系统。实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到对应的文档。(笔者注:原话如此)

  和他又扯了好久,分析整理了一下思路,推测他大概就是想这样:

学校发出的成绩表格统计是这样的:

-----------------------------程序运行后-------------------------------------》

每个文件的内容是:

  大概就是用统计好的成绩表来自动填写成绩单模板。(以上全是我的推测)

  有过类似想法的程序员朋友都知道,OFFICE文件的格式和文本文档不同,想要打开并进行操作的话,需要调用微软的API,并且根据版本不同还有不同的API。这样操作起来就相当的麻烦。这里呢,他又用的WPS这种我不太熟悉的软件,另外交代的需求也相当不清晰。这里我简单的写出一个程序的框架,等以后有时间再慢慢完善好了。

  首先表格的格式可能有很多种变换,设置一个固定的格式将来肯定需要修改很多次,这里,提供一个txt作为模板,通过模板程序对读取的数据进行分类。从表格数据到文本的转化可以直接通过WPS完成,表格数据项与项之间用连字符号‘-’连接。程序读取之后,将每行数据的前两项(一般来说应该是序号和姓名)作为文件名,每行数据作为文件内容保存在学生信息管理系统当前路径的Data文件夹内。

  新建一个MFC程序,选择对话框格式,并搭建界面如下:

程序将在初始化窗口的时候导入模板文件,我们在OnInitDialog中添加代码如下:

BOOL CSImangerDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    
    CString TempFolder = "Template";                            
    if ( !PathIsDirectory( TempFolder ) )                        //文件夹是否已经存在?
        CreateDirectory( TempFolder,NULL );

    SetList();                                                    //初始化列表框。

    return TRUE;  // return TRUE  unless you set the focus to a control
}

双击“编辑模板”按钮添加消息响应函数OnBtnEdit:

void CSImangerDlg::OnBtnEdit() 
{
    // TODO: Add your control notification handler code here
    CString Path = GetModuleDir();

    Path += "\Template\Template.txt";
    CString cmd = "notepad.exe " + Path;
    system( cmd );

    ( (CListBox*)GetDlgItem( IDC_TEMPLATELIST ) )->ResetContent();    //列表框清空

    SetList();                                                        //设置列表框。
}

类成员函数GetModuleDir:

CString CSImangerDlg::GetModuleDir()                                //获取可执行文件路径
{
     HMODULE module = GetModuleHandle(0); 
     char pFileName[MAX_PATH]; 
     GetModuleFileName(module, pFileName, MAX_PATH); 
 
     CString csFullPath(pFileName); 
     int nPos = csFullPath.ReverseFind( _T('\') ); 
     if( nPos < 0 ) 
      return CString(""); 
     else 
      return csFullPath.Left( nPos ); 
}

类成员函数SetList:

void CSImangerDlg::SetList()                                    //打开文件并利用文件内容初始化列表框。
{
    char* pszFileName= "Template\Template.txt";                
    CStdioFile myFile;
    CFileException fileException;

    myFile.Open( pszFileName,
                 CFile::typeText | CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate,&fileException);
    myFile.SeekToBegin();

    CString Temp;
    while ( myFile.ReadString( Temp ) )
        ( (CListBox*)GetDlgItem( IDC_TEMPLATELIST ) )->AddString( Temp );
    
    myFile.Close();

    return ;
}

这样添加与编辑模板的功能就完成了。接下来我们需要读入学生信息文件,并将其分类保存。

添加一个类成员变量vector<CString> record;存放字段名。

双击“导入文件并分类”按钮添加消息响应函数OnBtnImport和其他的一些成员函数:

void CSImangerDlg::OnBtnImport()                            //导入数据文件
{
    // TODO: Add your control notification handler code here
    CFileDialog fileDlg( TRUE );
    fileDlg.m_ofn.lpstrTitle = "导入你的数据文件(txt格式):";    
    fileDlg.m_ofn.lpstrFilter = "Text Files(*.txt)*.txtAll Files(*.*)*.*";

    if ( IDOK == fileDlg.DoModal() ){
        CStdioFile myFile;
        CFileException fileException;

        myFile.Open( fileDlg.GetFileName(),
        CFile::typeText | CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate,&fileException);
        myFile.SeekToBegin();

        CString Temp,str;
        vector<CString> data;

        GetList();
        while ( myFile.ReadString( Temp ) ){            //读取文件内容并保存至data容器
            str += (Temp + "
");                        //MFC编辑框中换行需用"
"字串
            data.push_back( Temp );
        }
        
        myFile.Close();
        SetDlgItemText( IDC_SHOWSI,str );                //显示导入文件内容.

        CString Folder = GetModuleDir() + "\Data";            
        
        if ( !PathIsDirectory( Folder ) )                //文件夹是否已经存在?
            CreateDirectory( Folder,NULL );

        vector<CString>::iterator itr = data.begin();
        for ( ; itr != data.end() ; ++itr ){
            Temp = *itr;
            CString FileName = Temp.Left( Temp.Find('-',Temp.Find('-')+1) );
            FileName += ".txt";
            Temp = AddRecord( Temp );
            Temp.Replace( "-","
" );

            CFile file( Folder + "\" + FileName,CFile::modeCreate | CFile::modeWrite );
            file.Write( Temp,Temp.GetLength() );
            file.Close();
        }
    }
}

void CSImangerDlg::GetList()                            //将CListBox的内容读入vector
{
    CListBox *m_lstInfo = ( CListBox* )GetDlgItem( IDC_TEMPLATELIST );
    int Count = m_lstInfo->GetCount();
    CString Temp;

    record.clear();
    for ( int i = 0 ; i < Count ; ++i ){
        m_lstInfo->GetText( i,Temp );
        record.push_back( Temp );
    }

    return ;
}

CString &CSImangerDlg::AddRecord(CString &Temp)
{
    vector<CString>::iterator itr = record.begin();

    if ( itr != record.end() ){
        Temp = (*itr++) + Temp;                                    //在文件头插入第一个字段

        int pos = 0;
        while ( itr != record.end() ){
            pos = Temp.Find( '-',pos ) + 1;
            Temp.Insert( pos,*itr + ":" );
            pos += itr->GetLength();
            itr++;
        }
    }

    return Temp;
}

这样,一个简单的框架就搭建完毕了。

下载地址:http://pan.baidu.com/s/1pJRWM3h

我们一路奋战,不是为了改变世界,而是不让世界改变我们 ——《熔炉》
原文地址:https://www.cnblogs.com/ZRBYYXDM/p/5157555.html