CFileDialog FileDialog(FALSE, _T("*.xls"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Microsoft Excel 2000(*.xls)|*.xls|所有文件(*.*)"), this);
if (FileDialog.DoModal() != IDOK)
{
return;
}
CString cStrFile = FileDialog.GetPathName(); //选择保存路径名称
if (::PathFileExists(cStrFile))
DeleteFile(cStrFile);
//CString cStrFile = _T("E:\\myexcel.xls");
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app; //Excel程序
CWorkbooks books; //工作簿集合
CWorkbook book; //工作表
CWorksheets sheets; //工作簿集合
CWorksheet sheet; //工作表集合
CRange range; //使用区域
CoUninitialize();
book.PrintPreview(_variant_t(false));
if (CoInitialize(NULL) == S_FALSE)
{
MessageBox(_T("初始化COM支持库失败!"));
return;
}
if (!app.CreateDispatch(_T("Excel.Application"))); //创建IDispatch接口对象
{
//MessageBox(_T("Error!"));
}
books = app.get_Workbooks();
book = books.Add(covOptional);
sheets = book.get_Worksheets();
sheet = sheets.get_Item(COleVariant((short)1)); //得到第一个工作表
CHeaderCtrl *pmyHeaderCtrl = l_list.GetHeaderCtrl(); //获取表头
int m_cols = pmyHeaderCtrl->GetItemCount(); //获取列数
int m_rows = l_list.GetItemCount(); //获取行数
TCHAR lpBuffer[256];
HDITEM hdi; //This structure contains information about an item in a header control. This structure has been updated to support header item images and order values.
hdi.mask = HDI_TEXT;
hdi.pszText = lpBuffer;
hdi.cchTextMax = 256;
CString colname;
CString strTemp;
int iRow, iCol;
for (iCol = 0; iCol < m_cols; iCol++)//将列表的标题头写入EXCEL
{
GetCellName(1, iCol + 1, colname); //(colname就是对应表格的A1,B1,C1,D1)
range = sheet.get_Range(COleVariant(colname), COleVariant(colname));
pmyHeaderCtrl->GetItem(iCol, &hdi); //获取表头每列的信息
range.put_Value2(COleVariant(hdi.pszText)); //设置每列的内容
int nWidth = l_list.GetColumnWidth(iCol) / 6;
//得到第iCol+1列
range.AttachDispatch(range.get_Item(_variant_t((long)(iCol + 1)), vtMissing).pdispVal, true);
//设置列宽
range.put_ColumnWidth(_variant_t((long)nWidth));
}
range = sheet.get_Range(COleVariant(_T("A1 ")), COleVariant(colname));
range.put_RowHeight(_variant_t((long)50));//设置行的高度
range.put_VerticalAlignment(COleVariant((short)-4108));//xlVAlignCenter = -4108
COleSafeArray saRet; //COleSafeArray类是用于处理任意类型和维数的数组的类
DWORD numElements[] = { m_rows,m_cols }; //行列写入数组
saRet.Create(VT_BSTR, 2, numElements); //创建所需的数组
range = sheet.get_Range(COleVariant(_T("A2 ")), covOptional); //从A2开始
range = range.get_Resize(COleVariant((short)m_rows), COleVariant((short)m_cols)); //表的区域
long index[2];
for (iRow = 1; iRow <= m_rows; iRow++)//将列表内容写入EXCEL
{
for (iCol = 1; iCol <= m_cols; iCol++)
{
index[0] = iRow - 1;
index[1] = iCol - 1;
CString szTemp;
szTemp = l_list.GetItemText(iRow - 1, iCol - 1); //取得l_list控件中的内容
BSTR bstr = szTemp.AllocSysString(); //The AllocSysString method allocates a new BSTR string that is Automation compatible
saRet.PutElement(index, bstr); //把m_list控件中的内容放入saRet
SysFreeString(bstr);
}
}
range.put_Value2(COleVariant(saRet)); //将得到的数据的saRet数组值放入表格
book.SaveCopyAs(COleVariant(cStrFile)); //保存到cStrFile文件
book.put_Saved(true);
books.Close();
//
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
app.Quit();
但是我用上面的代码运行后会报一堆错误,底层类好像有问题,所以改成了如下代码:文章来源地址https://www.toymoban.com/news/detail-446397.html
void CDlgStratueCal::OnBnClickedClistctrltoexc()
{
CStringArray str11;
for (int i = 0; i < l_list.GetItemCount(); i++)
{
str11.Add(l_list.GetItemText(i, 0));
str11.Add(l_list.GetItemText(i, 1));
str11.Add(l_list.GetItemText(i, 2));
}
CFileDialog fg(false, _T("*.csv"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("Excel CSV File(*.csv)|*.csv|Text File(*.txt)|*.txt|All File(*.*)|*.*||"), NULL);
if (fg.DoModal() == IDOK)
{
CString filename = fg.GetPathName();
CStdioFile file(filename, CFile::modeCreate | CFile::modeWrite);
CString str = _T("序号,地层厚度,实际厚度\n");
file.WriteString(str);
for (int i = 0; i < str11.GetSize() - 1;)
{
if (i == 0)
{
str = str11.GetAt(i) + _T(",");
str += str11.GetAt(++i) + _T(",");
str += str11.GetAt(++i) + _T("\n");
}
else
{
str = str11.GetAt(++i) + _T(",");
str += str11.GetAt(++i) + _T(",");
str += str11.GetAt(++i) + _T("\n");
}
file.WriteString(str);
}
file.Close();
}
}
文章来源:https://www.toymoban.com/news/detail-446397.html
到了这里,关于【MFC CListCtrl控件内容导出Excel/.csv文件】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!