使用自动化从 Excel 获取文本。 如果单元格太窄,则获取 #####。 我怎样才能避免这种情况?
我正在使用自动化从 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;
}
I'm using Automation to get_Text from an Excel worksheet. I do this because I need the formatted value (getting the value of the cell doesn't apply any formatting). If the column the cell is in is too narrow, I get "#####" the same was I would if I were to look at the spreadsheet via Excel. How can I avoid that?
EDIT:
Here is the relevant code:
// 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IRange
接口提供了AutoFit()
方法。根据 文档,调用它将使列宽足以容纳其内容。 (这是 .NET 互操作文档,但我希望这里没有差异)
请注意(强调我的):
The
IRange
interface provides anAutoFit()
method.According to the documentation, calling this would make columns wide enough to fit their contents. (It's the .NET interop documentation, but I expect no differences here)
Be aware that (emphasis mine):
您应该能够获取和设置列宽度(rangeobject.ColumnWidth) - 在抓取文本之前增加宽度应该可以解决问题。
You should be able to get and set the column width (rangeobject.ColumnWidth) - increasing the width before grabbing the text should do the trick.