標籤:資料 width os art for io
/// <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();
}