首页 > 代码库 > c# 操作Word 添加图标
c# 操作Word 添加图标
/// <summary>
/// 插入图表
/// </summary>
/// <param name="strbookmark">书签名</param>
/// <param name="dtsheet">图表数据源datatable</param>
/// <param name="xlcharttype">图表格式</param>
public void InsertChart(string strbookmark, DataTable dtsheet,Graph.XlChartType xlcharttype)
{
int i, j;
Graph.Chart wrdChart;
Graph.Axis axis;
object oClassType = "MSGraph.Chart.8";
object bookmark = strbookmark;
//在指定的书签位置插入图表
Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref bookmark).Range;
//初始化一张图表
wrdChart = (Graph.Chart)wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing).OLEFormat.Object;
//wrdChart.Application.Visible = false;
wrdChart.Application.PlotBy = Graph.XlRowCol.xlColumns;//根据Y轴来画图表
//改变图表格式
wrdChart.ChartType = xlcharttype;
axis = (Graph.Axis)wrdChart.Axes(1, 1);//设置X轴的属性
wrdChart.Application.DataSheet.Cells.Clear();//清空表格的初始数据
//填充图表,起始的行号和列号都是1
for (i = 0; i < dtsheet.Columns.Count; i++)//初始化列名
{
wrdChart.Application.DataSheet.Cells[1, i + 1] = dtsheet.Columns[i].ColumnName;
}
for (i = 0; i < dtsheet.Rows.Count; i++)//填充数据
{
for (j = 0; j < dtsheet.Columns.Count; j++)
{
wrdChart.Application.DataSheet.Cells[i + 2, j + 1] = dtsheet.Rows[i][j].ToString().Replace("9999999", "100ys");
}
}
//axis.MaximumScale = 1;//X轴最大刻度
//axis.MajorUnit = 0.1;
wrdChart.Legend.Delete();
wrdChart.Width = 500;
//wrdChart.Height = 666;
//oShape.Height = oWord.InchesToPoints(3.57f);
//更新图表并保存退出
wrdChart.Application.Update();
wrdChart.Application.Quit();
this.Save();
}