1 Star 1 Fork 1

Michael.Ma/xlsToxml11

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
xlsToxml11Dlg.cpp 9.69 KB
一键复制 编辑 原始数据 按行查看 历史
马国梁 GuoliangMa 提交于 2015-08-04 14:25 . add files
// xlsToxml11Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "xlsToxml11.h"
#include "xlsToxml11Dlg.h"
#include "afxdialogex.h"
#include "CWorkbooks.h"
#include "CWorkbook.h"
#include "CWorksheets.h"
#include "CWorksheet.h"
#include "CRange.h"
#include "Markup.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CxlsToxml11Dlg 对话框
CxlsToxml11Dlg::CxlsToxml11Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CxlsToxml11Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CxlsToxml11Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CxlsToxml11Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_GEN, &CxlsToxml11Dlg::OnBnClickedButtonGen)
END_MESSAGE_MAP()
// CxlsToxml11Dlg 消息处理程序
BOOL CxlsToxml11Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
SetDlgItemText(IDC_EDIT_XLS_PATH, _T("E:\\xls2xml\\20140425083023.xls"));
SetDlgItemText(IDC_EDIT_XML_PATH, _T("e:\\out.xml"));
SetDlgItemText(IDC_EDIT_PRODUCE_DATE, _T(""));
SetDlgItemText(IDC_EDIT_CORP_BATCH_NO, _T("0001031"));
SetDlgItemText(IDC_EDIT_PRODUCT_ID, _T("00"));
SetDlgItemText(IDC_EDIT_ORDER_ID, _T(""));
SetDlgItemText(IDC_EDIT_ACTOR, _T("1111"));
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CxlsToxml11Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CxlsToxml11Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CxlsToxml11Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CxlsToxml11Dlg::OnBnClickedButtonGen()
{
// TODO: 在此添加控件通知处理程序代码
if(!m_app.CreateDispatch(_T("Excel.Application"))) //启动Excel
{
AfxMessageBox(_T("居然你连OFFICE都没有安装吗?"));
return;
}
CString xls_filename;
GetDlgItemText(IDC_EDIT_XLS_PATH, xls_filename);
DWORD dwAttr;
dwAttr = ::GetFileAttributes(xls_filename);
if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
_tprintf(_T("Travers foloder: %s\n"), xls_filename);
TraverseFolder(xls_filename);
}
else {
_tprintf(_T("upload single file: %s\n"), xls_filename);
if (!genXML(xls_filename))
AfxMessageBox(_T("failed to gen xml"));
}
m_app.Quit();
}
bool CxlsToxml11Dlg::genXML(const TCHAR *filename)
{
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
COleVariant covTrue((short)TRUE), covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString xml_filename;
GetDlgItemText(IDC_EDIT_XML_PATH, xml_filename);
CString produce_date, batch_no, product_id, order_id, actor;
GetDlgItemText(IDC_EDIT_PRODUCE_DATE, produce_date);
GetDlgItemText(IDC_EDIT_CORP_BATCH_NO, batch_no);
GetDlgItemText(IDC_EDIT_PRODUCT_ID, product_id);
GetDlgItemText(IDC_EDIT_ORDER_ID, order_id);
GetDlgItemText(IDC_EDIT_ACTOR, actor);
books = m_app.get_Workbooks();
book = books.Open(filename,
covFalse, covTrue, covOptional, COleVariant(_T("")),
COleVariant(_T("")), covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);
sheets = book.get_Worksheets();
sheet = sheets.get_Item(COleVariant((short)1));
range = sheet.get_UsedRange();
//CComVariant Cell1(_T("A1"));
//CComVariant Cell2(_T("G7"));
//range = sheet.get_Range(Cell1, Cell2);//范围在这边自己选
VARIANT ret;
ret = range.get_Value(covOptional);
COleSafeArray sa(ret);
long lNumRows;
long lNumCols;
sa.GetUBound(1, &lNumRows);
sa.GetUBound(2, &lNumCols);
long index[2];
VARIANT val;
int r, c;
CString cstr = _T("");
bool bEmpty;
CMarkup dom;
dom.SetDoc(_T("<?xml version=\"1.0\"?>\r\n"));
dom.AddElem(_T("Document"));
dom.AddAttrib(_T("xmlns:xsi"), _T("http://www.w3.org/2001/XMLSchema-instance"));
dom.AddAttrib(_T("xmlns:xsd"), _T("http://www.w3.org/2001/XMLSchema"));
dom.AddAttrib(_T("SN"), _T("000000000000"));
dom.AddAttrib(_T("License"), _T("GICOM手持终端"));
dom.AddAttrib(_T("Version"), _T("2.10"));
dom.IntoElem();
dom.AddElem(_T("Events"));
dom.IntoElem();
dom.AddElem(_T("Event"));
dom.AddAttrib(_T("Name"), _T("SalesWareHouseOut"));
dom.AddAttrib(_T("MainAction"), _T("WareHouseOut"));
dom.IntoElem();
dom.AddElem(_T("DataField"));
dom.IntoElem();
/*dom.AddChildElem(_T("SN"), _T("123344A-J"));
dom.AddChildElem(_T("NAME"), _T("crank casing"));
dom.AddChildElem(_T("QTY"), _T("1"));*/
for (r=1;r<=lNumRows;r++) {
if (r > 1) {
dom.AddElem(_T("Data"));
}
bEmpty = true;
for(c=1;c<=lNumCols;c++) {
index[0]=r;
index[1]=c;
sa.GetElement(index, &val);
CString cstr2 = _T("");
switch(val.vt) {
case VT_BOOL: //如果单元格中的值为布尔值
if (val.boolVal == VARIANT_TRUE)
cstr += _T("TRUE");
else
cstr += _T("FALSE");
cstr += _T("\t");
bEmpty = false;
break;
case VT_R8: //数字
//TRACE("\t\t%1.2f", val.dblVal);
cstr2.Format(_T("%1.1f"),val.dblVal);
cstr += cstr2+ _T("\t");
bEmpty = false;
break;
case VT_BSTR: //文字
//TRACE("\t\t%s",(CString)val.bstrVal);
cstr +=(CString)val.bstrVal + _T("\t");
if (r > 1) {
if (c == 1) {
dom.AddAttrib(_T("Code"), (CString)val.bstrVal);
dom.AddAttrib(_T("Actor"), actor);
}
else if(c == 7) {
dom.AddAttrib(_T("ActDate"), (CString)val.bstrVal);
}
}
bEmpty = false;
break;
case VT_EMPTY:
break;
}
}
cstr +=_T("\n");
dom.AddAttrib(_T("CorpOrderID"), order_id);
dom.AddAttrib(_T("CorpProductID"), product_id);
dom.AddAttrib(_T("CorpBatchNo"), batch_no);
dom.AddAttrib(_T("ProduceDate"), produce_date);
if (bEmpty)
break;
}
CString csXML = dom.GetDoc();
dom.Save(xml_filename);
AfxMessageBox(_T("All done!"));
CString save_filename, tmp;
tmp = filename;
tmp.Truncate(tmp.ReverseFind(_T('.')));
save_filename = tmp + _T("_done.xls");
book.SaveAs(COleVariant(save_filename),
covOptional, covOptional, covOptional, covOptional,
covOptional, 0, covOptional, covOptional,
covOptional, covOptional, covOptional);
book.Close(COleVariant((short)FALSE), covOptional, covOptional);
DeleteFile(filename);
return true;
}
int CxlsToxml11Dlg::TraverseFolder(LPCTSTR lpPath)
{
TCHAR szFind[MAX_PATH] = {_T("\0")};
WIN32_FIND_DATA findFileData;
BOOL bRet;
_tcscpy_s(szFind, MAX_PATH, lpPath);
_tcscat_s(szFind, _T("\\*.xls")); //这里一定要指明通配符,不然不会读取所有文件和目录
HANDLE hFind = ::FindFirstFile(szFind, &findFileData);
if (INVALID_HANDLE_VALUE == hFind)
{
return 1;
}
//遍历文件夹
TCHAR filepath[MAX_PATH] = {_T("\0")};
while(1) {
if (findFileData.cFileName[0] != _T('.')) {
//不是当前路径或者父目录的快捷方式 ".." "."
_stprintf_s(filepath, _T("%s\\%s"), lpPath, findFileData.cFileName);
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
//这是一个普通目录
_tprintf(_T("------------enter folder: %s\n"), filepath);
//设置下一个将要扫描的文件夹路径
_tcscpy_s(szFind, MAX_PATH, lpPath);
_tcscat_s(szFind, _T("\\"));
_tcscat_s(szFind, findFileData.cFileName);
///_tcscat_s(szNextDir, _T("\\*"));
//遍历该目录
TraverseFolder(szFind);
}
else {
if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ||
(findFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM )) {
// pass
}
else {
_tprintf(_T("%s\t"), findFileData.cFileName);
if (!genXML(findFileData.cFileName))
AfxMessageBox(_T("failed to gen xml"));
}
}
}
//如果是当前路径或者父目录的快捷方式,或者是普通目录,则寻找下一个目录或者文件
bRet = ::FindNextFile(hFind, &findFileData);
if (!bRet) {
//函数调用失败
//_tprintf(_T("FindNextFile failed, error code: %d\n"), GetLastError());
break;
}
}
::FindClose(hFind);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/iloveyaya/xlsToxml11.git
[email protected]:iloveyaya/xlsToxml11.git
iloveyaya
xlsToxml11
xlsToxml11
master

搜索帮助