VS2013 MFC做的文档中英文分拣(支持xml excel text格式)屏蔽字宏 游戏便捷小程序
程序员文章站
2022-06-13 22:58:29
...
1、需要分拣的文档在excel表格中 对文档进行中英文分类到两个text文档中。
2、建立MFC工程设置文档工作界面(MFC文件功能设置)
3、添加按钮和文本框(MFC添加按钮和文本框步骤不在这里细说百度可得到)
4、编写相应的执行程序
// DirtyWordView.cpp : CDirtyWordView 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "DirtyWord.h"
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CDirtyWordView
IMPLEMENT_DYNCREATE(CDirtyWordView, CFormView)
BEGIN_MESSAGE_MAP(CDirtyWordView, CFormView)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_BN_CLICKED(IDC_OPEN_BUTTON, &CDirtyWordView::OnBnClickedOpenButton)
ON_BN_CLICKED(IDC_SAVEcf_BUTTON, &CDirtyWordView::OnBnClickedSavecfButton)
ON_BN_CLICKED(IDC_SAVEef_BUTTON, &CDirtyWordView::OnBnClickedSaveefButton)
ON_BN_CLICKED(IDC_Implement_BUTTON4, &CDirtyWordView::OnBnClickedImplementButton4)
ON_BN_CLICKED(IDC_DISPLAYcf_BUTTON, &CDirtyWordView::OnBnClickedDisplaycfButton)
ON_BN_CLICKED(IDC_DISPLAYef_BUTTON, &CDirtyWordView::OnBnClickedDisplayefButton)
END_MESSAGE_MAP()
// CDirtyWordView 构造/析构
CDirtyWordView::CDirtyWordView()
: CFormView(CDirtyWordView::IDD)
{
// TODO: 在此处添加构造代码
}
CDirtyWordView::~CDirtyWordView()
{
}
void CDirtyWordView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
}
BOOL CDirtyWordView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CFormView::PreCreateWindow(cs);
}
void CDirtyWordView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CDirtyWordView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CDirtyWordView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CDirtyWordView 诊断
#ifdef _DEBUG
void CDirtyWordView::AssertValid() const
{
CFormView::AssertValid();
}
void CDirtyWordView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CDirtyWordDoc* CDirtyWordView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDirtyWordDoc)));
return (CDirtyWordDoc*)m_pDocument;
}
#endif //_DEBUG
// CDirtyWordView 消息处理程序
void CDirtyWordView::OnBnClickedOpenButton()
{
// TODO: 在此添加控件通知处理程序代码
// 设置过滤器
TCHAR szFilter[] = _T("文本文件(*.xls)|*.xls|所有文件(*.*)|*.*||");
// 构造打开文件对话框
CFileDialog fileDlg(TRUE, _T("xls"), NULL, 0, szFilter, this);
CString strFilePathOpen;
// 显示打开文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
strFilePathOpen = fileDlg.GetPathName();
SetDlgItemText(IDC_OPEN_EDIT, strFilePathOpen);
//导入 拷贝
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (!app.CreateDispatch(_T("Excel.Application")))
{
this->MessageBox(_T("无法创建Excel应用!"));
return;
}
books = app.get_Workbooks();
//打开Excel,其中pathname为Excel表的路径名
lpDisp = books.Open(strFilePathOpen, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
book.AttachDispatch(lpDisp);
sheets = book.get_Worksheets();
sheet = sheets.get_Item(COleVariant((short)1));
}
}
void CDirtyWordView::OnBnClickedSavecfButton()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|Word文件(*.doc)|*.doc|所有文件(*.*)|*.*||");
// 构造保存文件对话框
CFileDialog fileDlg(FALSE, _T("doc"), _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
/*CString strFilePath;*/
CStdioFile file;
// 显示保存文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“保存”按钮,则将选择的文件路径显示到编辑框里
strFilePathcf = fileDlg.GetPathName();
file.Open(strFilePathcf, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
SetDlgItemText(IDC_SAVEcf_EDIT, strFilePathcf);
}
}
void CDirtyWordView::OnBnClickedSaveefButton()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|Word文件(*.doc)|*.doc|所有文件(*.*)|*.*||");
// 构造保存文件对话框
CFileDialog fileDlg(FALSE, _T("doc"), _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
/*CString strFilePath;*/
CStdioFile file;
// 显示保存文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“保存”按钮,则将选择的文件路径显示到编辑框里
strFilePathef = fileDlg.GetPathName();
file.Open(strFilePathef, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
SetDlgItemText(IDC_SAVEef_EDIT, strFilePathef);
}
}
void CDirtyWordView::OnBnClickedImplementButton4()
{
// TODO: 在此添加控件通知处理程序代码
//建立TXT文件 cf和ef
CStdioFile File;
File.Open(strFilePathcf, CFile::modeCreate | CFile::modeReadWrite);//如果文件事先不存在的话,就需要CFile::modeCreate,否则就不需要。
CStdioFile File1;
File1.Open(strFilePathef, CFile::modeCreate | CFile::modeReadWrite);
//获得坐标为(A,2)的单元格
using namespace std;
vector<CString> VecString;
CString str;
CString strTemp;
for (int n = 2;; n++)
{
str.Format(_T("A%d"), n);
range = sheet.get_Range(COleVariant(str), COleVariant(str));
COleVariant rValue;
rValue = COleVariant(range.get_Value2());
strTemp = rValue;
/*VecString.push_back(CString(rValue.bstrVal));*/
if (strTemp.IsEmpty())
{
break;
}
else
{
VecString.push_back(strTemp);
for (int i = 0; i < VecString.size(); ++i) //把数据从容器中取出
{
string strVecString(VecString[i].GetBuffer()); //CString转string
wstring wstrVecString = (LPWSTR)_bstr_t(VecString[i]);//Cstring转wstring
if (strVecString.size() != wstrVecString.size())
{
//CString tTempStr;
//tTempStr.Format(_T("%s%d"), _T("A"), i + 2);
for (int num = 0; num < strVecString.size(); ++num)
{
char ch3 = VecString[i][num];
char ch4 = VecString[i][num + 1];
if ((int)VecString[i][num] < 0 && (int)VecString[i][num + 1] < 0)
{
CString strFirst = VecString[i].Mid(num, 2);
CString tTempStr;
tTempStr.Format(_T("%s%d"), _T("B"), i + 2);
CString ch5 = "|";
File.WriteString("1 ");
File.WriteString(VecString[i]);
File.WriteString(ch5);
File.WriteString(strFirst);
File.WriteString("\n");
break;
}
}
}
else //非汉字的输出到D列
{
CString tTempStr;
tTempStr.Format(_T("%s%d"), _T("D"), i + 2);
File1.WriteString(strTemp);
File1.WriteString("\n");
}
}
//释放Vector
vector<CString>().swap(VecString);
}
}
MessageBox("已经加载完毕");
}
void CDirtyWordView::OnBnClickedDisplaycfButton()
{
// TODO: 在此添加控件通知处理程序代码
ShellExecute(NULL, _T("open"), _T(strFilePathcf), NULL, NULL, SW_SHOW);
}
void CDirtyWordView::OnBnClickedDisplayefButton()
{
// TODO: 在此添加控件通知处理程序代码
ShellExecute(NULL, _T("open"), _T(strFilePathef), NULL, NULL, SW_SHOW);
}
void CDirtyWordView::CString_Find(CString CString_Word)
{
using namespace std;
vector<CString> VecString;
VecString.push_back(CString_Word);
for (int i = 0; i < VecString.size(); ++i) //把数据从容器中取出
{
string strVecString(VecString[i].GetBuffer()); //CString转string
wstring wstrVecString = (LPWSTR)_bstr_t(VecString[i]); //Cstring转wstring
if (strVecString.size() != wstrVecString.size()) //字符串作对比不相等含有中文
{
for (int num = 0; num < strVecString.size(); ++num) //逐个遍历Vector里面的元素
{
char ch3 = VecString[i][num];
char ch4 = VecString[i][num + 1];
if ((int)VecString[i][num] > 0 /*&& (int)VecString[i][num + 1] < 0*/) //当相邻的两个元素都小于0则为中文
{
vector<char> VecString_English;
VecString_English.push_back(VecString[i][num]);
}
}
}
}
//释放Vector
vector<CString>().swap(VecString);
}
5、运行程序选取文档并选取输出文档的路径
6、执行
上一篇: 个人博客网站文章添加目录导航
下一篇: ROS报错处理(持续更新)