void __fastcall CrnExportStringGrid2Excel(TStringGrid *sg, String strXlsFile)
{
Variant vExcelApp, vSheet;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, TEXT("啟動 Excel 出錯, 可能是沒有安裝Excel."),
TEXT("ExportStringGrid2Excel"), MB_OK | MB_ICONERROR);
return;
}
// 隱藏Excel界面
vExcelApp.OlePropertySet("Visible", false);
// 新建一個作業表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 作業表
// 操作這個作業表
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
.OlePropertyGet("Sheets", 1);
// 設定Excel檔案的字體
vSheet.OleProcedure("Select");
vSheet.OlePropertyGet("Cells").OleProcedure("Select");
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
.OlePropertySet("Size", sg->Font->Size);
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
.OlePropertySet("Name", WideString(sg->Font->Name));
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
.OlePropertySet("FontStyle", WideString("常規"));
vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
// 表格的行數
int nRowCount = sg->RowCount;
nRowCount = nRowCount < 2? 2: nRowCount;
// 表格的列數
int nColCount = sg->ColCount;
nColCount = nColCount < 1? 1: nColCount;
for (int i = 1; i < nRowCount; i++)
{
// 63 63 72 75 6E 2E 63 6F 6D
for (int j = 1; j < nColCount; j++)
{
vSheet.OlePropertyGet("Cells", i, j)
.OlePropertySet("Value", WideString(sg->Cells[j][i]));
}
}
// 保存Excel檔案并退出
vExcelApp.OlePropertyGet("ActiveWorkbook")
.OleFunction("SaveAs", WideString(strXlsFile));
vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
// 作業結束
MessageBox(0, TEXT("轉換結束!"),
TEXT("ExportStringGrid2Excel"), MB_OK | MB_ICONINFORMATION);
}
這是將StringGarid中的內容匯入到作業簿的第一個作業表中,現有一個需求,想把StringGarid中的不同內容匯入到同一作業簿的不同作業表中,該如何修改代碼?求大神給予詳細幫助,別只泛泛的說,可以舉點實體!
uj5u.com熱心網友回復:
// 隱藏Excel界面vExcelApp.OlePropertySet("Visible", false);
// 新建一個作業表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 作業表
// 操作這個作業表
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
.OlePropertyGet("Sheets", 1);
根據需求更改這段代碼就行了吧,我估計是修改這句
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
.OlePropertyGet("Sheets", 1);
你查找一下相關函式的功能吧
uj5u.com熱心網友回復:
別估計啊!我修改過了,沒改成,去哪查相關函式的功能呢?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/111515.html
標籤:數據庫及相關技術
上一篇:我用VS2008中的UGOpen向導,就簡單編了hollow,UG的對話框,在編譯時怎么老是彈出這個框,誰能解決一下
