格式化已经格式化为文本的Excel列(在C ++中)? [英] Formatting excel columns already formatted as text (in c++)?

查看:102
本文介绍了格式化已经格式化为文本的Excel列(在C ++中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一个启动excel,将数字数据写入单元格并激活excel的应用程序.单元格的格式默认为文本,并且excel显示错误信息,即数字数据存储为文本.
我尝试通过此MFC代码更改单元格格式:

这段代码在MFC中. CLR中类似的代码也具有相同的功能.

I have creaded an application that starts excel, writes numeric data to cells and activates excel. The format of cell defaults to text and excel shows error that numberic data is stored as text.
I try to change the cell format by this MFC code:

This code is in MFC. The similar code in CLR also does the same.

CRange range;
range = sheet.get_Range(COleVariant(L"A1"), COleVariant(L"A1"));
range.put_NumberFormat( (COleVariant(L"$0.00") )


写入excel Cell A1的代码如下:


The code that writes to excel Cell A1 is like this:

COleSafeArray saRet;
DWORD numElements[]={1,1};//1x1 element array
saRet.Create(VT_BSTR, 2, numElements);
FillSafeArray(L"123456", 0, 0, &saRet);
CRange range;
Range = sheet.get_Range(COleVariant("A1"),COleVariant("A1"));
range.put_Value2(COleVariant(saRet));


void FillSafeArray(OLECHAR FAR* sz, int iRow, int iCol, COleSafeArray* sa)
{
    long index[2];
    index[0] = iRow;
    index[1] = iCol;
    VARIANT v;
    VariantInit(&v);
    v.vt = VT_BSTR;
    v.bstrVal = SysAllocString(sz);
    sa->PutElement(index, v.bstrVal);
    SysFreeString(v.bstrVal);
    VariantClear(&v);
}



但它不起作用.我完全不能将单元格格式更改为任何数字格式,但是一旦将单元格格式转换为数字宽度的excel UI,就可以更改为任何数字格式.谁能告诉我这段代码有什么问题,或者我应该采取什么其他方法?

最好的问候
abzadeh



but It does not work. I can not change the Cell format to any of Number formats at all, But once I convert the cell format to number width excel UI, then I can change to any number format. can any body tell me what is wrong with this code, or what other approach should I take?

best regards
abzadeh

推荐答案

0.00))


写入excel Cell A1的代码如下:


The code that writes to excel Cell A1 is like this:

COleSafeArray saRet;
DWORD numElements[]={1,1};//1x1 element array
saRet.Create(VT_BSTR, 2, numElements);
FillSafeArray(L"123456", 0, 0, &saRet);
CRange range;
Range = sheet.get_Range(COleVariant("A1"),COleVariant("A1"));
range.put_Value2(COleVariant(saRet));


void FillSafeArray(OLECHAR FAR* sz, int iRow, int iCol, COleSafeArray* sa)
{
    long index[2];
    index[0] = iRow;
    index[1] = iCol;
    VARIANT v;
    VariantInit(&v);
    v.vt = VT_BSTR;
    v.bstrVal = SysAllocString(sz);
    sa->PutElement(index, v.bstrVal);
    SysFreeString(v.bstrVal);
    VariantClear(&v);
}



但它不起作用.我完全不能将单元格格式更改为任何数字格式,但是一旦将单元格格式转换为数字宽度的excel UI,就可以更改为任何数字格式.谁能告诉我这段代码有什么问题,或者我应该采取什么其他方法?

最好的问候
abzadeh



but It does not work. I can not change the Cell format to any of Number formats at all, But once I convert the cell format to number width excel UI, then I can change to any number format. can any body tell me what is wrong with this code, or what other approach should I take?

best regards
abzadeh


saRet.Create(VT_BSTR, 2, numElements);
FillSafeArray(L"123456", 0, 0, &saRet);


好吧,尽管该字段包含数字,但它仍然是文本字符串,而不是数字.


Well although that field contains numeric digits, it''s still a text string, not a number.


您可能会发现以下有趣的地方:
ExcelFormat库 [
You might find this interesting:
ExcelFormat Library[^]

Regards
Espen Harlinn


这篇关于格式化已经格式化为文本的Excel列(在C ++中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆