1,新建C#控制台应用程序(Excel创建图表)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//解决方案中 添加引用 Execl(COM组件)
using MSExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
namespace ExeclCharts
{
class Program
{
static void Main(string[] args)
{
object path; //文件路径变量
MSExcel.Workbook excelDoc; //Excel文档变量
MSExcel.Application excelApp; //Excel应用程序变量 MSExcel.Workbook excelDoc; //Excel文档变量
path = @"C:\ExcelData\MyExcel.xlsx"; //路径
excelApp = new MSExcel.Application(); //初始化
//vs2010不能用ApplicationClass(),而用Application();
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Nothing代替 Object Nothing = Missing.Value;
Object Nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(Nothing);
//使用第一个工作表作为插入数据的工作表
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1]; //声明一个MSExcel.Range 类型的变量r
MSExcel.Range r;
//获得A1处的表格,并赋值
r = ws.get_Range("A1", "A1");
r.Value2 = "3";
//获得A2处的表格,并赋值
r = ws.get_Range("A2", "A2");
r.Value2 = "5.7";
//获得A3处的表格,并赋值
r = ws.get_Range("A3", "A3");
r.Value2 = "4.8";
//获得A4处的表格,并赋值
r = ws.get_Range("A4", "A4");
r.Value2 = "9.2";
//获得A1处的表格,并赋值
r = ws.get_Range("B1", "B1");
r.Value2 = "3";
//获得A2处的表格,并赋值
r = ws.get_Range("B2", "B2");
r.Value2 = "5.7";
//获得A3处的表格,并赋值
r = ws.get_Range("B3", "B3");
r.Value2 = "4.8";
//获得A4处的表格,并赋值
r = ws.get_Range("B4", "B4");
r.Value2 = "9.2";
//获得A1处的表格,并赋值
r = ws.get_Range("C1", "C1");
r.Value2 = "3";
//获得A2处的表格,并赋值
r = ws.get_Range("C2", "C2");
r.Value2 = "5.7";
//获得A3处的表格,并赋值
r = ws.get_Range("C3", "C3");
r.Value2 = "4.8";
//获得A4处的表格,并赋值
r = ws.get_Range("C4", "C4");
r.Value2 = "9.2";
excelDoc.Charts.Add(Nothing, Nothing, Nothing, Nothing);
excelDoc.ActiveChart.ChartType = MSExcel.XlChartType.xlBubble;//xlBubble 指散点图 三维气泡图(xlBubble3DEffect)
excelDoc.ActiveChart.SetSourceData(ws.get_Range("A1", "C4"), MSExcel.XlRowCol.xlColumns);
excelDoc.ActiveChart.Location(MSExcel.XlChartLocation.xlLocationAsObject, "sheet1");
excelDoc.ActiveChart.HasTitle = true;
excelDoc.ActiveChart.ChartTitle.Text = "创建 - 散点图表";
excelDoc.ActiveChart.HasDataTable = false;
//WdSaveFormat为Excel文档的保存格式
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
//将excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, format, Nothing, Nothing, Nothing, Nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelDoc.Close(Nothing, Nothing, Nothing); //关闭excelApp组件对象
excelApp.Quit();
Console.WriteLine(path + " 创建完毕!");
}
}
}
2,新建空网站,添加一个页面,在页面中添加一个Button,添加事件(保存本地,word获取)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;//解决方案中 添加引用 System.Windows.Forms(.NET组件)
using System.Threading;
using Microsoft.Office.Interop.Excel;
using MSExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using MSWord = Microsoft.Office.Interop.Word;
namespace WebCharts
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{