08-10-2012 09:52 AM
The excel file has 3 different worksheets. I modified the data on worksheet 3 but unable to save it in to MS DOS format. It saved the worksheet 1 instead. How am I save the data on worksheet 3? following is the code sample. I am running windows XP SP3 latest and CVI 2010.
#include "excelreport.h"
int main (int argc, char *argv[])
{
HRESULT hrRc;
char szErrMsg[256];
CAObjHandle HdlWorkbook;
CAObjHandle HdlXLApp;
CAObjHandle HdlWorksheet;
double data[2] = {200,2354.25898};
double value = 7777.9;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
// Open Excel and make it visible
hrRc = ExcelRpt_ApplicationNew (VTRUE, &HdlXLApp);
if ( hrRc ) goto ExcelRpt_Err;
// Open Excel file
hrRc = ExcelRpt_WorkbookOpen(HdlXLApp, "Template.xls", &HdlWorkbook);
if ( hrRc ) goto ExcelRpt_Err;
hrRc = ExcelRpt_GetWorksheetFromName (HdlWorkbook, "Test_Cases", &HdlWorksheet);
if ( hrRc ) goto ExcelRpt_Err;
// put a breakpoint in here
hrRc = ExcelRpt_WriteData (HdlWorksheet, "D8:D8", ExRConst_dataDouble, 1, 1, &value);
if ( hrRc ) goto ExcelRpt_Err;
// put a breakpoint in here
value = value + 1111.1;
hrRc = ExcelRpt_WriteData (HdlWorksheet, "D9:D9", ExRConst_dataDouble, 1, 1, &value);
if ( hrRc ) goto ExcelRpt_Err;
// Save workbook
hrRc = ExcelRpt_WorkbookSave(HdlWorkbook, "test3.txt", ExRConst_TextMSDOS);
if ( hrRc ) goto ExcelRpt_Err;
// close workbook
hrRc = ExcelRpt_WorkbookClose (HdlWorkbook, 0);
if ( hrRc ) goto ExcelRpt_Err;
// Quit Application
hrRc = ExcelRpt_ApplicationQuit (HdlXLApp);
if ( hrRc ) goto ExcelRpt_Err;
// tidy up memory space
CA_DiscardObjHandle(HdlWorksheet);
CA_DiscardObjHandle(HdlWorkbook);
CA_DiscardObjHandle(HdlXLApp);
ExcelRpt_Err:
memset(szErrMsg, 0, sizeof(szErrMsg));
CA_GetAutomationErrorString(hrRc, szErrMsg, sizeof(szErrMsg));
MessagePopup("Excel Report Error", szErrMsg);
return 0;
}
Solved! Go to Solution.
08-10-2012 11:10 AM
I think you may have to explicitly make that page active.
Excel_WorksheetActivate (ws_handle, NULL);
JC
08-10-2012 12:06 PM - edited 08-10-2012 12:09 PM
Edit:
You can't save an individual worksheet in Excel. At least not in Excel 2007 that I have.
Just try to do it manually and see what that gets you.
If you mean, you want to save a *.xls file in *.txt format, you need to do a SaveAs.