通过c++对c#语言进行封装操作word

ccj2326051096 2019-04-25 09:18:57
下面链接的内容为如下代码,是通过c#对NPOI的使用来操作excel,我现在想通过c++对下列代码进行封装,从而达到在c++平台上进行使用的目的,但是未成功,不知道哪位大神有过类似经历,求指教~~~~积分不多,多多见谅。江湖救急~~~~~

https://www.cnblogs.com/sstn/p/9435337.html

const int NUM_OF_ROWS = 3;
const int NUM_OF_COLUMNS = 10;

static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2)
{
var chart = drawing.CreateChart(anchor) as XSSFChart;
//生成图例
var legend = chart.GetOrCreateLegend();
  //图例位置
legend.Position = LegendPosition.TopRight;

//图表
var data = chart.ChartDataFactory.CreateLineChartData<double, double>(); //折线图
  //var data = chart.ChartDataFactory.CreateScatterChartData<double, double>(); //散点图

// X轴.
var bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
bottomAxis.IsVisible = false; //默认为true 不显示 设置为fase 显示坐标轴(BUG?)

//Y轴
IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
leftAxis.Crosses = (AxisCrosses.AutoZero);
leftAxis.IsVisible = false; //设置显示坐标轴

//数据源
IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));

IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));

//数据系列
var s1 = data.AddSeries(xs, ys1);
s1.SetTitle(serie1);
var s2 = data.AddSeries(xs, ys2);
s2.SetTitle(serie2);


chart.Plot(data, bottomAxis, leftAxis);

}

static void Main(string[] args)
{
IWorkbook wb = new XSSFWorkbook();
ISheet sheet = wb.CreateSheet("linechart");

// Create a row and put some cells in it. Rows are 0 based.
IRow row;
ICell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
{
row = sheet.CreateRow((short)rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
{
cell = row.CreateCell((short)colIndex);
cell.SetCellValue(colIndex * (rowIndex + 1));
}
}

IDrawing drawing = sheet.CreateDrawingPatriarch();
//锚点
IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);
CreateChart(drawing, sheet, anchor1, "title1","title2");
IClientAnchor anchor2 = drawing.CreateAnchor(0, 0, 0, 0, 0, 20, 10, 35);
CreateChart(drawing, sheet, anchor2, "s1", "s2");
using (FileStream fs =File.Create("test.xlsx"))
{
wb.Write(fs);
}

}
...全文
131 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccj2326051096 2019-04-26
  • 打赏
  • 举报
回复
感谢@stherix 我之前没接触过这种方式 按照你的方式试了一下 可以使用 虽然还不知道这种方式是否满足公司的业务要求 但还是感谢你提供的方式方法
@tongshou 你说的这种方式我没看懂😅 有时间我再研究研究 因为公司这边都用的是NPOI 所以阁下的方式我并没有采用

以上:谢谢诸位~~~~
tongshou 2019-04-25
  • 打赏
  • 举报
回复
从C++里操作 Excel 很繁琐。(但是上面的C#也没有好到哪里去),可以利用 IDispatch的 invoke方法操作 Excel。不是几句话可以说清楚,网上应该可以找到类似的例子(好像MSDN 里就有)。如果操作量大,最好借助有关脚本程序语言。
ccj2326051096 2019-04-25
  • 打赏
  • 举报
回复
没有人做过类似的工作吗 自己顶一顶~~~~~~!!!
stherix 2019-04-25
  • 打赏
  • 举报
回复
你可以把c#编译成一个dll,其中有静态函数的接口,c++是有能力通过clr宿主启动c#的模块能力的,可以百度c++里运行c#

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧