Esempio n. 1
0
void COpenExcelDlg::Lin_ExportListToExcel(CListCtrl &m_List)
{
	if (m_List.GetHeaderCtrl()->GetItemCount()>0)
	{
		CApplication app;
		CWorkbooks books;
		CWorkbook book;
		CWorksheets sheets;
		CWorksheet sheet;
		CRange range;
		CRange cols;
		int i = 3;
		CString str1, str2;
		COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
		if( !app.CreateDispatch(_T("Excel.Application")))
		{
			MessageBox(_T("无法创建Excel应用!"));
			return;
		}
		books=app.get_Workbooks();
		book=books.Add(covOptional);
		sheets=book.get_Sheets();
		sheet=sheets.get_Item(COleVariant((short)1));

		//写入表头//
		for (int ColumNum=1;ColumNum<m_List.GetHeaderCtrl()->GetItemCount();ColumNum++)
		{
			str2 = Lin_GetEnglishCharacter(ColumNum);
			str2 = str2 + _T("1");
			LVCOLUMN   lvColumn;   
			TCHAR strChar[256];
			lvColumn.pszText=strChar;   
			lvColumn.cchTextMax=256 ;
			lvColumn.mask   = LVCF_TEXT;
			m_List.GetColumn(ColumNum,&lvColumn);
			CString str=lvColumn.pszText;
			range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
			range.put_Value2(COleVariant(str));

		}
		//获取单元格的位置//
		for (int ColumNum =1;ColumNum<m_List.GetHeaderCtrl()->GetItemCount();ColumNum++)
		{

			for (int ItemNum = 0;ItemNum<m_List.GetItemCount();ItemNum++)
			{
				str2 = Lin_GetEnglishCharacter(ColumNum);
				str1.Format(_T("%d"),ItemNum+2);
				str2 = str2+str1;
				CString str = m_List.GetItemText(ItemNum,ColumNum);
				range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
				range.put_Value2(COleVariant(str));
			}
		}
		cols=range.get_EntireColumn();
		cols.AutoFit();

		app.put_Visible(TRUE);
		app.put_UserControl(TRUE);
	}else{
		AfxMessageBox(_T("列表为空"));
	}
	
}
void Cwhu_FaxSettingDlg::Lin_ExportForArrToExcel(CStringArray &m_AutoForward,CString m_FilePath)
{

	CApplication app;
	CWorkbooks books;
	CWorkbook book;
	CWorksheets sheets;
	CWorksheet sheet;
	CRange range;
	CRange cols;
	CString str1, str2;
	int m_ArrPos = 0;
	int m_ArrCount = m_AutoForward.GetSize();
	COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	if( !app.CreateDispatch(_T("Excel.Application")))
	{
		MessageBox(_T("无法创建Excel应用!"));
		return;
	}
	books=app.get_Workbooks();
	book=books.Add(covOptional);
	sheets=book.get_Sheets();
	sheet=sheets.get_Item(COleVariant((short)1));

	//写入表头//
	str2 = _T("A1");
	CString str = _T("源号码");
	range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
	range.put_Value2(COleVariant(str));
	str2 = _T("B1");
	str = _T("转发号码个数");
	range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
	range.put_Value2(COleVariant(str));

	for (int j=3;j<11;j++)
	{
		str2 = Lin_GetEnglishCharacter(j);
		str2 = str2 +_T("1");
		str= _T("转发号码");
		range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
		range.put_Value2(COleVariant(str));

	}
	CStringArray m_LineStart;
	for (int i=0;i<m_ArrCount;i++)
	{
		CString m_str= m_AutoForward.GetAt(i);
		int CharCount = m_str.GetLength();
		if (CharCount<4) //如果是号码个数//
		{
			m_str.Format(_T("%d"),i-1);
			m_LineStart.Add(m_str);
		}
	}
	int ItemNum = 2;
	int ColumNum = 1;
	for (int i=0;i<m_LineStart.GetSize();i++) 
	{
		int Pos = _ttoi(m_LineStart.GetAt(i));
		CString m_str = m_AutoForward.GetAt(Pos+1);
		int Number = _ttoi(m_str);
		for (int j=0;j<Number+2;j++)
		{
			str2 = Lin_GetEnglishCharacter(ColumNum);
			str1.Format(_T("%d"),ItemNum);
			str2 = str2 +str1;
			CString content = _T("'")+m_AutoForward.GetAt(Pos+j);
			range=sheet.get_Range(COleVariant(str2),COleVariant(str2));
			range.put_Value2(COleVariant(content));
			ColumNum++;
			if (ColumNum>10)
			{
				ColumNum = 3;
				ItemNum++;
			}
		}
		ItemNum++;
		ColumNum = 1;
	}
	cols=range.get_EntireColumn();
	cols.AutoFit();
	//app.put_Visible(TRUE);
	//app.put_UserControl(TRUE);
	book.SaveAs(COleVariant(m_FilePath),covOptional,covOptional,covOptional,covOptional,covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional);
	app.Quit();


}