// ZCOutTabDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ZCOutTabDlg.h"
#include "afxdialogex.h"
#include "BAS_DRAW_FUN1.h"
#include "BAS_DRAW_FUN.h"
#include "StationDataBase.h"
#include "CalJXAndGD.h"
#include "BaseLine.h"
#include <afxpriv.h>
#include "excel9.h"
#include "excel9.cpp"

extern CString Cworkdir, Pro;
extern ACHAR SysPath[STR_LONG];
extern int incZX;
extern int G_ISSX;
extern vector<CBaseLine*>pJXGTZXArray;
extern _Application ExcelApp;//应用程序
extern _Workbook wbMyBook;
extern Worksheets wssMysheets;
extern _Worksheet wsMysheet;

//extern vector<Switch> SXDCCJArray, XXDCCJArray;
int issetpos = 0;
// CZCOutTabDlg 对话框

IMPLEMENT_DYNAMIC(CZCOutTabDlg, CDialog)

void CZCOutTabDlg::OnBnClickedOk()
{
	Range rgMyRge;
	//创建Excel服务
		if (!ExcelApp.CreateDispatch(L"Excel.Application", NULL))
		{
			AfxMessageBox(L"不能打开Excel表格!请确认Excel已经安装正确!");
			return;
		}
	m_SingleTable.SetPos(100);
	// 利用模板文件建立新文档:
	wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(), true);
	CFileFind Ffind;
	if (Ffind.FindFile(m_EXCEL))//如果输出文件存在，连接输出文件
	{
		wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t(m_EXCEL)));
		// 4.x1.得到Worksheets 
		wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(), true);
		// 4.x2 附加检测
		try
		{
			wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("土石方工程数量")), true); // 以土石方表作为测试标注
			rgMyRge.ReleaseDispatch();
			wsMysheet.ReleaseDispatch();
		}
		catch (CException* e)
		{
			rgMyRge.ReleaseDispatch();
			wsMysheet.ReleaseDispatch();
			wssMysheets.ReleaseDispatch();
			AfxMessageBox(L"RESULT路径下的\"*_出表.xls\"不符合站场诸表标准！\r\n请检查并更改该文件名以避免后续冲突！\r\n本次操作改用模板文件进行！");
			// 释放原连接
			wbMyBook.ReleaseDispatch();
			// 连接模板文件
			if (Ffind.FindFile(m_ExcelTemplate) == TRUE)
				wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t(m_ExcelTemplate)));
			else
			{
				wbMyBook.ReleaseDispatch();
				AfxMessageBox(L"模板文件丢失！");
				return;
			}
			e->Delete();
			// 得到Worksheets 
			wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(), true);
		}
	}
	else // 如果输出文件不存在，连接模板文件
	{
		if (Ffind.FindFile(m_ExcelTemplate) == TRUE)
			wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t(m_ExcelTemplate)));
		else
		{
			wbMyBook.ReleaseDispatch();
			AfxMessageBox(L"模板文件丢失！");
			return;
		}
		// 得到Worksheets 
		wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(), true);
	}

	//开始写表
	CString m_MdbName = Cworkdir + "\\DATA\\" + Pro + DBEXT;
	//设置数据库路径
	CCalGCSLToolObject.m_MdbName = m_MdbName;
	m_SingleTable.SetPos(100);
	OutGDB();
	m_SingleTable.SetPos(100);
	if (m_OPENEXCEL)
	{
		wsMysheet.Activate();
		ExcelApp.SetVisible(true);
		ExcelApp.SetUserControl(true);   //调用excel表
	}
	else
	{
		wbMyBook.SaveAs(COleVariant(m_EXCEL));
		wsMysheet.Activate();
		ExcelApp.SetVisible(false);
		ExcelApp.SetUserControl(false);   //不调用excel表
		ExcelApp.Quit();
		//ACHAR MES[STR_LONG];
		//_stprintf(MES, L"Excel已保存至项目RESULT文件 %s", m_EXCEL);
		//AfxMessageBox(MES);
	}
	wsMysheet.ReleaseDispatch();

	wssMysheets.ReleaseDispatch();
	wbMyBook.ReleaseDispatch();
	wbsMyBooks.ReleaseDispatch();
	ExcelApp.ReleaseDispatch();
	ExcelApp.Quit();

}

bool CZCOutTabDlg::WriteInExcel(Range rgMyRge, long Line, long Col, _variant_t Value)
{
	rgMyRge.SetItem(_variant_t(Line), _variant_t(Col), Value);
	rgMyRge.BorderAround(_variant_t((long)1), _variant_t((long)2), _variant_t((long)-4105), vtMissing);//设置边框 
	return true;
}

void CZCOutTabDlg::OutGDB()
{
	int nPos = 0;
	try
	{
		wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("股道表")), true); //填挖高度表
	}
	catch (CException* e)
	{
		Range rgMyRge;
		rgMyRge.ReleaseDispatch();
		wsMysheet.ReleaseDispatch();
		AfxMessageBox(L"股道表模板丢失！");
		e->Delete();
		return;
	}

	m_SingleTable.SetPos(40);

	CString tmp;
	//得到全部Cells，此时,rgMyRge是cells的集合 
	Range rgMyRge;
	rgMyRge.AttachDispatch(wsMysheet.GetCells(), true);

	int StartRow = 6;
	int RowNum;
	CCalGCSLToolObject.CalGDBData(RowNum);

	m_SingleTable.SetPos(40);

	for (int i = 0; i < (int)CCalGCSLToolObject.GDBArray.size(); i++)
	{
		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].BH);
		WriteInExcel(rgMyRge, i + StartRow, 1, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].Name);
		WriteInExcel(rgMyRge, i + StartRow, 2, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].GX);
		WriteInExcel(rgMyRge, i + StartRow, 3, _variant_t(tmp));

		tmp.Format(L"%s%s", CCalGCSLToolObject.GDBArray[i].StartDCZT, CCalGCSLToolObject.GDBArray[i].StartDCBH);
		WriteInExcel(rgMyRge, i + StartRow, 4, _variant_t(tmp));

		tmp = L"";
		for (int k = 0; k < (int)CCalGCSLToolObject.GDBArray[i].DCNoArray.size(); k++)
			tmp += CCalGCSLToolObject.GDBArray[i].DCNoArray[k];
		WriteInExcel(rgMyRge, i + StartRow, 5, _variant_t(tmp));

		tmp.Format(L"%s%s", CCalGCSLToolObject.GDBArray[i].EndDCZT, CCalGCSLToolObject.GDBArray[i].EndDCBH);
		WriteInExcel(rgMyRge, i + StartRow, 6, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].QuanChang);
		WriteInExcel(rgMyRge, i + StartRow, 7, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].YouXiaoChang);
		WriteInExcel(rgMyRge, i + StartRow, 8, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].PuGuiChang);
		WriteInExcel(rgMyRge, i + StartRow, 9, _variant_t(tmp));

		tmp.Format(L"%s", CCalGCSLToolObject.GDBArray[i].Note);
		WriteInExcel(rgMyRge, i + StartRow, 10, _variant_t(tmp));

		nPos = (int)(60 * i * 1.0 / CCalGCSLToolObject.GDBArray.size());
		m_SingleTable.SetPos(40 + nPos);
	}


	rgMyRge.ReleaseDispatch();
	wsMysheet.ReleaseDispatch();
	m_SingleTable.SetPos(100);
}
