导出到Excel2003,长整型数出错 - C++ Builder / Windows SDK/API
/*
18位身份证(全数字)导出到Excel2003时,身份证最后4位变成了数字
如身份证中带有X或Y时就不会出现以上情况
*/
Variant ExcelApp;
try
{
ExcelApp = Variant::CreateObject("Excel.Application");
}catch(...)
{
return;
}
ExcelApp.OlePropertySet("Visible", false);
ExcelApp.OlePropertyGet("WorkBooks").OleFunction("add", 1);
Variant ExcelSheet = ExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);
const unsigned int c_uiRowCount = ADOQuery1->RecordCount;
ExcelSheet.OlePropertyGet("Cells", 1, 1).OlePropertySet("Value", "身份证");
ExcelApp.OlePropertyGet("Columns", 1).OlePropertySet("ColumnWidth", 100);
ADOQuery1->First();
for (unsigned int i=2; i<=c_uiRowCount+1; i++)
{
ExcelSheet.OlePropertyGet("Cells", i, 1).OlePropertySet("Value", (ADOQuery1->FieldByName("身份证")->AsString.Trim()).c_str());
ADOQuery1->Next();
}
ExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs", strFileName.c_str());
ExcelApp.OleFunction("Quit");
ExcelSheet = Unassigned;
ExcelApp = Unassigned;
//怎样解决
[解决办法]
- C/C++ code
//做了个测试你试试 //TVariant ExcelApp; AnsiString st = "123456789012345678"; Variant ExcelApp; try { ExcelApp = Variant::CreateObject("Excel.Application"); }catch(...) { return; } ExcelApp.OlePropertySet("Visible", false); ExcelApp.OlePropertyGet("WorkBooks").OleFunction("add", 1); Variant ExcelSheet = ExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1); //const unsigned int c_uiRowCount = ADOQuery1->RecordCount; ExcelSheet.OlePropertyGet("Cells", 1, 1).OlePropertySet("Value", "身份证"); ExcelApp.OlePropertyGet("Columns", 1).OlePropertySet("ColumnWidth", 100); // ADOQuery1->First(); //for (unsigned int i=2; i <=c_uiRowCount+1; i++) { ExcelSheet.OlePropertyGet("Cells",1 , 1).OlePropertySet("NumberFormatLocal","@"); //加 // ExcelSheet.OlePropertyGet("Cells", i, 1).OlePropertySet("Value", (ADOQuery1->FieldByName("身份证")->AsString.Trim()).c_str()); ExcelSheet.OlePropertyGet("Cells",1 , 1).OlePropertySet("Value", st.c_str()); //测试 // ADOQuery1->Next(); } ExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs", "YDLCHINA.XLS");//strFileName.c_str()); ExcelApp.OleFunction("Quit"); ExcelSheet = Unassigned; ExcelApp = Unassigned;