我正在使用自动化从 Excel 工作表中获取文本。我这样做是因为我需要格式化的值(获取单元格的值不应用任何格式)。如果单元格所在的列太窄,我会得到“#####”,就像我通过 Excel 查看电子表格一样。我怎样才能避免这种情况?
编辑:
以下是相关代码:
// Return the (string) value of a cell
HRESULT CDialogImport::GetCellValue(IRange *irange, int irow, int icol, CString &cstrValue)
{
// Get dispatch interface for the cell at irow,icol
COleVariant vCell;
HRESULT hr = AutoWrap(
DISPATCH_PROPERTYGET,
&vCell,
irange,
L"Item",
2,
COleVariant((short)(icol+1)),
COleVariant((short)(irow+1)));
if (FAILED(hr)) return hr;
// Use the dispatch interface to get the value of the cell
COleVariant result;
hr = AutoWrap(
DISPATCH_PROPERTYGET,
&result,
vCell.pdispVal,
L"Text",
0);
if (SUCCEEDED(hr))
{
cstrValue = result;
}
return hr;
}