Excel 文件操作代码
来源:互联网 发布:电脑淘宝怎么删除差评 编辑:程序博客网 时间:2024/05/18 02:59
#include "stdafx.h"
#include "comdef.h"
#include <cmath>
#import \
"C:\Program Files (X86)\Common Files\Microsoft Shared\OFFICE12\mso.dll" \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("RGB", "RBGXL")
//Microsoft VBA Objects
#import \
"C:\Program Files (X86)\Common Files\Microsoft Shared\VBA\VBA6\vbe6ext.olb"
//Excel Application Objects
#import "C:\Program Files (X86)\Microsoft Office\OFFICE12\EXCEL.EXE" \
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText", "ReplaceTextXL") \
rename("CopyFile", "CopyFileXL") \
exclude("IFont", "IPicture") no_dual_interfaces
#include <iostream>
using namespace std;
int f(int x)
{
return x*x;
}
int TestExcel()
{
//Surround the entire interfacing code with a try block
try
{
//Initialise the COM interface
CoInitialize(NULL);
//Define a pointer to the Excel application
Excel::_ApplicationPtr xl;
//Start one instance of Excel
xl.CreateInstance(L"Excel.Application");
//Make the Excel application visible
xl->Visible = true;
//Add a (new) workbook
xl->Workbooks->Add(Excel::xlWorksheet);
//Get a pointer to the active worksheet
Excel::_WorksheetPtr pSheet = xl->ActiveSheet;
//Set the name of the sheet
pSheet->Name = "Chart Data";
//Get a pointer to the cells on the active worksheet
Excel::RangePtr pRange = pSheet->Cells;
//Define the number of plot points
unsigned Nplot = 100;
//Set the lower and upper limits for x
double x_low = 0.0, x_high = 20.0;
//Calculate the size of the (uniform) x interval
//Note a cast to an double here
double h = (x_high - x_low)/(double)Nplot;
//Create two columns of data in the worksheet
//We put labels at the top of each column to say what it contains
pRange->Item[1][1] = "x"; pRange->Item[1][2] = "f(x)";
//Now we fill in the rest of the actual data by
//using a single for loop
for(unsigned i=0;i<Nplot;i++)
{
//Calculate the value of x (equally-spaced over the range)
double x = x_low + i*h;
//The first column is our equally-spaced x values
pRange->Item[i+2][1] = x;
//The second column is f(x)
pRange->Item[i+2][2] = f(x);
}
//The sheet "Chart Data" now contains all the data
//required to generate the chart
//In order to use the Excel Chart Wizard,
//we must convert the data into Range Objects
//Set a pointer to the first cell containing our data
Excel::RangePtr pBeginRange = pRange->Item[1][1];
//Set a pointer to the last cell containing our data
Excel::RangePtr pEndRange = pRange->Item[Nplot+1][2];
//Make a "composite" range of the pointers to the start
//and end of our data
//Note the casts to pointers to Excel Ranges
Excel::RangePtr pTotalRange =
pSheet->Range[(Excel::Range*)pBeginRange][(Excel::Range*)pEndRange];
// Create the chart as a separate chart item in the workbook
Excel::_ChartPtr pChart=xl->ActiveWorkbook->Charts->Add();
//Use the ChartWizard to draw the chart.
//The arguments to the chart wizard are
//Source: the data range,
//Gallery: the chart type,
//Format: a chart format (number 1-10),
//PlotBy: whether the data is stored in columns or rows,
//CategoryLabels: an index for the number of columns
// containing category (x) labels
// (because our first column of data represents
// the x values, we must set this value to 1)
//SeriesLabels: an index for the number of rows containing
// series (y) labels
// (our first row contains y labels,
// so we set this to 1)
//HasLegend: boolean set to true to include a legend
//Title: the title of the chart
//CategoryTitle: the x-axis title
//ValueTitle: the y-axis title
pChart->ChartWizard((Excel::Range*)pTotalRange,
(long)Excel::xlXYScatter,
6L,(long)Excel::xlColumns, 1L, 1L, true,
"My Graph", "x", "f(x)");
//Give the chart sheet a name
pChart->Name = "My Data Plot";
}
//If there has been an error, say so
catch(_com_error & error)
{
cout << "COM ERROR" << endl;
}
//Finally Uninitialise the COM interface
CoUninitialize();
//Finish the C++ program
return 0;
}
#include "comdef.h"
#include <cmath>
#import \
"C:\Program Files (X86)\Common Files\Microsoft Shared\OFFICE12\mso.dll" \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("RGB", "RBGXL")
//Microsoft VBA Objects
#import \
"C:\Program Files (X86)\Common Files\Microsoft Shared\VBA\VBA6\vbe6ext.olb"
//Excel Application Objects
#import "C:\Program Files (X86)\Microsoft Office\OFFICE12\EXCEL.EXE" \
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText", "ReplaceTextXL") \
rename("CopyFile", "CopyFileXL") \
exclude("IFont", "IPicture") no_dual_interfaces
#include <iostream>
using namespace std;
int f(int x)
{
return x*x;
}
int TestExcel()
{
//Surround the entire interfacing code with a try block
try
{
//Initialise the COM interface
CoInitialize(NULL);
//Define a pointer to the Excel application
Excel::_ApplicationPtr xl;
//Start one instance of Excel
xl.CreateInstance(L"Excel.Application");
//Make the Excel application visible
xl->Visible = true;
//Add a (new) workbook
xl->Workbooks->Add(Excel::xlWorksheet);
//Get a pointer to the active worksheet
Excel::_WorksheetPtr pSheet = xl->ActiveSheet;
//Set the name of the sheet
pSheet->Name = "Chart Data";
//Get a pointer to the cells on the active worksheet
Excel::RangePtr pRange = pSheet->Cells;
//Define the number of plot points
unsigned Nplot = 100;
//Set the lower and upper limits for x
double x_low = 0.0, x_high = 20.0;
//Calculate the size of the (uniform) x interval
//Note a cast to an double here
double h = (x_high - x_low)/(double)Nplot;
//Create two columns of data in the worksheet
//We put labels at the top of each column to say what it contains
pRange->Item[1][1] = "x"; pRange->Item[1][2] = "f(x)";
//Now we fill in the rest of the actual data by
//using a single for loop
for(unsigned i=0;i<Nplot;i++)
{
//Calculate the value of x (equally-spaced over the range)
double x = x_low + i*h;
//The first column is our equally-spaced x values
pRange->Item[i+2][1] = x;
//The second column is f(x)
pRange->Item[i+2][2] = f(x);
}
//The sheet "Chart Data" now contains all the data
//required to generate the chart
//In order to use the Excel Chart Wizard,
//we must convert the data into Range Objects
//Set a pointer to the first cell containing our data
Excel::RangePtr pBeginRange = pRange->Item[1][1];
//Set a pointer to the last cell containing our data
Excel::RangePtr pEndRange = pRange->Item[Nplot+1][2];
//Make a "composite" range of the pointers to the start
//and end of our data
//Note the casts to pointers to Excel Ranges
Excel::RangePtr pTotalRange =
pSheet->Range[(Excel::Range*)pBeginRange][(Excel::Range*)pEndRange];
// Create the chart as a separate chart item in the workbook
Excel::_ChartPtr pChart=xl->ActiveWorkbook->Charts->Add();
//Use the ChartWizard to draw the chart.
//The arguments to the chart wizard are
//Source: the data range,
//Gallery: the chart type,
//Format: a chart format (number 1-10),
//PlotBy: whether the data is stored in columns or rows,
//CategoryLabels: an index for the number of columns
// containing category (x) labels
// (because our first column of data represents
// the x values, we must set this value to 1)
//SeriesLabels: an index for the number of rows containing
// series (y) labels
// (our first row contains y labels,
// so we set this to 1)
//HasLegend: boolean set to true to include a legend
//Title: the title of the chart
//CategoryTitle: the x-axis title
//ValueTitle: the y-axis title
pChart->ChartWizard((Excel::Range*)pTotalRange,
(long)Excel::xlXYScatter,
6L,(long)Excel::xlColumns, 1L, 1L, true,
"My Graph", "x", "f(x)");
//Give the chart sheet a name
pChart->Name = "My Data Plot";
}
//If there has been an error, say so
catch(_com_error & error)
{
cout << "COM ERROR" << endl;
}
//Finally Uninitialise the COM interface
CoUninitialize();
//Finish the C++ program
return 0;
}
- Excel 文件操作代码
- Excel 文件复制操作vba代码
- Excel 文件复制操作vba代码
- [代码实例][.NET]操作Excel文件
- C#读写操作Excel文件,Excel进程资源回收代码
- VBA处理文件框架代码 【第五部分:Excel文件操作】
- 操作EXCEL文件成功释放COM对象的代码
- java之 jdbc连接数据库与操作excel文件代码
- php操作excel文件多种方法与实例代码
- 代码操作Excel
- Excel操作代码
- JAVA操作EXCEL文件
- 直接操作excel文件
- asp操作excel文件
- vb操作EXCEL文件
- JAVA 操作EXCEL文件
- vb操作Excel文件
- Excel文件操作类
- Eric4 + PyQt 编写Python程序
- C#之图片框控件(PictureBox)常用属性
- comobox
- android usb挂载分析--各种格式支持
- linux块设备8
- Excel 文件操作代码
- Broadcast Receiver
- HashSet 中储存先判断对象的hash值 如果一样 在调用 对象equals方法判断是否是同一个对象
- 搭建Struts2开发环境--Struts2在web中的启动配置
- vi 常用命令
- 360安全卫士 误查杀win8启动程序
- 内部类
- dedecms导航,当前栏目高亮显示代码
- WCF 4 Step By Step Chapter 15 Note(Building REST Services)