C++ builder 2010 操作Excel表格的编程实现
2013-09-12 23:08
155 查看
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "ProcessBar.h" #include <stdio.h> // For FILE, fopen, fstat, fileno, fread and fclose #include <sys\stat.h> // For fstat and the stat struct #include <Math.hpp> // For Min #include <memory> //For STL auto_ptr class //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm4 *Form4; //--------------------------------------------------------------------------- __fastcall TForm4::TForm4(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm4::btn1Click(TObject *Sender) { FILE *F; char MyData[2047]; long BytesRead; F = fopen("../oui.txt", "r"); // Path relative to Debug // Use a file larger than 2048 bytes to make it interesting. if (F) { struct stat statbuf; fstat(fileno(F), &statbuf); pb1->Max = statbuf.st_size; if (pb1->Max > 10) { pb1->Step = (pb1->Max)/10; pb1->Step = Min(pb1->Step, 2047); } else pb1->Step = pb1->Max; std::auto_ptr<char> DataBuffer(new char[pb1->Step]); for (pb1->Position = 0; pb1->Position < pb1->Max; pb1->StepIt()) // Move the ProgressBar Position using StepIt. { fread(DataBuffer.get(), pb1->Step, 1, F); // Do this or else the read wraps and starts over. pb1->Step = Min(pb1->Step, pb1->Max - pb1->Position); } mmo1->Lines->Add(DataBuffer.get()); fclose(F); DataBuffer.release(); } } //--------------------------------------------------------------------------- void __fastcall TForm4::btn2Click(TObject *Sender) { Variant vExcelApp, vWorkbook, vRange, Sheet1; vExcelApp = Variant::CreateObject("Excel.Application"); vExcelApp.OlePropertySet("Visible", false); vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", dlgOpen1->FileName.c_str()); vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook"); int BookCount = vWorkbook.OlePropertyGet("Sheets").OlePropertyGet ("Count"); for (int i = 0; i < BookCount; i++) { AnsiString SheetName = vWorkbook.OlePropertyGet("Sheets", i + 1) .OlePropertyGet("Name"); // ShowMessage(SheetName); } // Sheet1 = vWorkbook.OlePropertyGet( "ActiveSheet "); // vExcelApp.Exec(PropertyGet( "Cells ") < <1 < <3).Exec(PropertySet( "Value ") < <15); //赋值 // Sheet1.OlePropertyGet( "Rows ", 1).OlePropertyGet( "Insert "); //插入 String kkl, hgf; int it = 9, st = 1; while (it > 8 && BookCount >= st) { vWorkbook.OlePropertyGet("Sheets", st).OleProcedure("Select"); // 选择sheet for (it = 1; it <= 8; it++) { kkl = vExcelApp.Exec(PropertyGet("Cells") << 5 << it); //if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取 break; } st++; } int kk = 1; for (kk = 1; kk <= 8; kk++) { kkl = vExcelApp.Exec(PropertyGet("Cells") << kk << it); //if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取 break; } hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + 2); int ki = 1; while (hgf.Trim() != "") { if (ki == StringGrid2->RowCount - 1) //StringGrid1->RowCount++; // kkl==vExcelApp.Exec(PropertyGet( "Cells ") < <kk < <it); StringGrid2->Cells[1][ki] = vExcelApp.Exec (PropertyGet("Cells") << kk << it); // 2-4列的数据 StringGrid2->Cells[2][ki] = vExcelApp.Exec (PropertyGet("Cells") << kk << it + 1); StringGrid2->Cells[3][ki] = vExcelApp.Exec (PropertyGet("Cells") << kk << it + 2); kk++; ki++; hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + 2); } vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells ", 1, 2) .OleProcedure("Select"); vRange = vExcelApp.OlePropertyGet("Selection"); vRange.Exec(Function("Sort") << vExcelApp.OlePropertyGet("Selection") << 1); // vWorkbook.OleProcedure( "Save"); vWorkbook.OleProcedure("Close"); vExcelApp.OleFunction("Quit"); vWorkbook = Unassigned; vExcelApp = Unassigned; } // ---------------------------------------------------------------------------
相关文章推荐
- C++ builder 2010 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现(OLE/COM)
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现
- VS2010 C++ 操作Excel表格的编程实现