程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#操作office進行Excel圖表創建,保存本地,word獲取

C#操作office進行Excel圖表創建,保存本地,word獲取

編輯:C#入門知識

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)
{


}





public void CopyExcel()
{
string exclePath = @"C:\ExcelData\MyExcel.xlsx";
int StartRow = 1; //讀的起始行
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//引用Excel對象
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(exclePath);
excel.UserControl = true;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
excel.Visible = false;
for (int i = 0; i < workbook.Worksheets.Count; i++)//循環取所有的Sheet.
{
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Worksheets.get_Item(i + 1) as Microsoft.Office.Interop.Excel.Worksheet;//從1開始.
for (int row = StartRow; row <= sheet.UsedRange.Rows.Count; row++)
{
//取單元格值;
for (int col = 1; col <= sheet.UsedRange.Columns.Count; col++)
{
Microsoft.Office.Interop.Excel.Range range = sheet.Cells[row, col] as Microsoft.Office.Interop.Excel.Range;
sb.Append("," + col.ToString() + ":" + range.Text);
}
sb.Append(System.Environment.NewLine);
//取存圖片;
if (sheet.Shapes.Count > row - StartRow)
{
Microsoft.Office.Interop.Excel.Shape s = sheet.Shapes.Item(row - StartRow + 1) as Microsoft.Office.Interop.Excel.Shape;

Clipboard.Clear();//Clipboard類是引用 .NET組件中System.Windows.Forms的
s.CopyPicture(Appearance.Button, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap); //COPY到內存。
IDataObject iData = Clipboard.GetDataObject();


if (iData != null && iData.GetDataPresent(DataFormats.Bitmap))
{
System.Drawing.Image img = Clipboard.GetImage(); //從內存讀取圖片
if (img != null)
{
//保存圖片位置
img.Save(@"C:\\ExcelData\\另存為.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //保存到本地
}
}
else
{
}
}
}
}
workbook.Close(false, null, null);
excel.Quit();
}


protected void btnExcel_Click1(object sender, EventArgs e)
{
Thread cbThread = new Thread(new ThreadStart(CopyExcel));
cbThread.TrySetApartmentState(ApartmentState.STA); //指定單線程,否則無法從剪貼板中讀取數據
cbThread.Start();










/*
* 創建Word獲取上面的圖片
*/
object path; //文件路徑變量
string strContent; //文本內容變量
MSWord.Application wordApp; //Word應用程序變量
MSWord.Document wordDoc; //Word文檔變量


path = @"C:\\ExcelData\\MyWord.docx"; //路徑


wordApp = new MSWord.Application(); //初始化


//如果文件已存在,則刪除
if (File.Exists((string)path))
{
File.Delete((string)path);
}


//由於使用的是COM庫,因此有許多變量需要用Missing.Value代替
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);


//設置文檔的行間距
wordApp.Selection.ParagraphFormat.LineSpacing = 15f;


//移動焦點並換行
object count = 34;
object WdLine = WdUnits.wdLine;//換一行
wordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動焦點
wordApp.Selection.TypeParagraph();//插入段落


//文檔中創建表格
Microsoft.Office.Interop.Word.Table newTable = wordDoc.Tables.Add(wordApp.Selection.Range, 25, 3, ref Nothing, ref Nothing);
//設置表格樣式
//newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinMedGap;//表格外框線
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;//表格內架線
//設置表格內框顏色
newTable.Borders.InsideColor = WdColor.wdColorWhite;// White:白色
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;


//填充表格內容(第一行)
newTable.Cell(1, 1).Range.Text = "快速工況數據分析報告";
newTable.Cell(1, 1).Range.Font.Size = 24;//設置單元格中字體大小為24
newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));//合並單元格
wordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中


//填充表格內容(第二行)
newTable.Cell(2, 1).Range.Text = "基本信息:\n 1, \n 2,\r 3,";
newTable.Cell(2, 1).Range.Font.Color = WdColor.wdColorDarkBlue;//設置單元格內字體顏色
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));//合並單元格
wordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;


//填充表格內容(第3行)
newTable.Cell(3, 1).Range.Text = "名稱:";
newTable.Cell(3, 2).Range.Text = "BrandName";//?
newTable.Cell(3, 2).Merge(newTable.Cell(3, 3));//合並單元格


//填充表格內容(第4行)
newTable.Cell(4, 1).Range.Text = "文檔創建時間:";
newTable.Cell(4, 2).Range.Text = DateTime.Now.ToString("yyyy年M月d日h時s分m秒fff毫秒");//
newTable.Cell(4, 2).Merge(newTable.Cell(4, 3));//合並單元格


//填充表格內容(第5行)
newTable.Cell(5, 1).Range.Text = "一、轉速-扭矩百分比的duty-Cycle圖;(Map)";
newTable.Cell(5, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(5, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(5, 1).Merge(newTable.Cell(5, 3));
//填充表格內容(第6行)
newTable.Cell(6, 1).Merge(newTable.Cell(6, 3));//合並單元格
newTable.Cell(6, 1).Select();//選中一行
//插入圖片
string A6FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A6LinkToFile = false;
object A6SaveWithDocument = true;
object A6Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A6FileName, ref A6LinkToFile, ref A6SaveWithDocument, ref A6Anchor);


//填充表格內容(第7行)
newTable.Cell(7, 1).Range.Text = "二、轉速-扭矩百分比的氣泡圖";
newTable.Cell(7, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(7, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(7, 1).Merge(newTable.Cell(7, 3));
//填充表格內容(第8行)
newTable.Cell(8, 1).Merge(newTable.Cell(8, 3));//合並單元格
newTable.Cell(8, 1).Select();//選中一行
//插入圖片
string A8FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A8LinkToFile = false;
object A8SaveWithDocument = true;
object A8Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A8FileName, ref A8LinkToFile, ref A8SaveWithDocument, ref A8Anchor);


//填充表格內容(第9行)
newTable.Cell(9, 1).Range.Text = "三、車速-轉速的散點圖";
newTable.Cell(9, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(9, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(9, 1).Merge(newTable.Cell(9, 3));
//填充表格內容(第10行)
newTable.Cell(10, 1).Merge(newTable.Cell(10, 3));//合並單元格
newTable.Cell(10, 1).Select();//選中一行
//插入圖片
string A10FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A10LinkToFile = false;
object A10SaveWithDocument = true;
object A10Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A10FileName, ref A10LinkToFile, ref A10SaveWithDocument, ref A10Anchor);


//填充表格內容(第11行)
newTable.Cell(11, 1).Range.Text = "四、車速柱狀圖";
newTable.Cell(11, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(11, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(11, 1).Merge(newTable.Cell(11, 3));
//填充表格內容(第12行)
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));//合並單元格
newTable.Cell(12, 1).Select();//選中一行
//插入圖片
string A12FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A12LinkToFile = false;
object A12SaveWithDocument = true;
object A12Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A12FileName, ref A12LinkToFile, ref A12SaveWithDocument, ref A12Anchor);
//將圖片設置為四周環繞型
Microsoft.Office.Interop.Word.Shape c = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
c.WrapFormat.Type = WdWrapType.wdWrapSquare;


//填充表格內容(第13行)
newTable.Cell(13, 1).Range.Text = "五、轉速柱狀圖";
newTable.Cell(13, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(13, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(13, 1).Merge(newTable.Cell(13, 3));
//填充表格內容(第14行)
newTable.Cell(14, 1).Merge(newTable.Cell(14, 3));
newTable.Cell(14, 1).Select();//選中一行
//插入圖片
string A14FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A14LinkToFile = false;
object A14SaveWithDocument = true;
object A14Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A14FileName, ref A14LinkToFile, ref A14SaveWithDocument, ref A14Anchor);
//將圖片設置為四周環繞型
Microsoft.Office.Interop.Word.Shape c1 = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
c1.WrapFormat.Type = WdWrapType.wdWrapSquare;


//填充表格內容(第15行)
newTable.Cell(15, 1).Range.Text = "六、檔位使用柱狀圖;(通過車輛信息設定來計算檔位)";
newTable.Cell(15, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(15, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(15, 1).Merge(newTable.Cell(15, 3));
//填充表格內容(第16行)
newTable.Cell(16, 1).Merge(newTable.Cell(16, 3));
newTable.Cell(16, 1).Select();//選中一行
//插入圖片
string A16FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A16LinkToFile = false;
object A16SaveWithDocument = true;
object A16Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A16FileName, ref A16LinkToFile, ref A16SaveWithDocument, ref A16Anchor);
//將圖片設置為四周環繞型
Microsoft.Office.Interop.Word.Shape c2 = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
c2.WrapFormat.Type = WdWrapType.wdWrapSquare;


//填充表格內容(第17行)
newTable.Cell(17, 1).Range.Text = "七、油門";
newTable.Cell(17, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(17, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(17, 1).Merge(newTable.Cell(17, 3));
//填充表格內容(第18行)
newTable.Cell(18, 1).Merge(newTable.Cell(18, 3));
newTable.Cell(18, 1).Select();//選中一行
//插入圖片
string A18FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A18LinkToFile = false;
object A18SaveWithDocument = true;
object A18Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A18FileName, ref A18LinkToFile, ref A18SaveWithDocument, ref A18Anchor);


//填充表格內容(第19行)
newTable.Cell(19, 1).Range.Text = "八、轉速-油耗 氣泡圖";
newTable.Cell(19, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(19, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(19, 1).Merge(newTable.Cell(19, 3));
//填充表格內容(第20行)
newTable.Cell(20, 1).Merge(newTable.Cell(20, 3));
newTable.Cell(20, 1).Select();//選中一行
//插入圖片
string A20FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A20LinkToFile = false;
object A20SaveWithDocument = true;
object A20Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A20FileName, ref A20LinkToFile, ref A20SaveWithDocument, ref A20Anchor);


//填充表格內容(第21行)
newTable.Cell(21, 1).Range.Text = "九、轉速-油耗、轉速扭矩混合氣泡圖";
newTable.Cell(21, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(21, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(21, 1).Merge(newTable.Cell(21, 3));
//填充表格內容(第22行)
newTable.Cell(22, 1).Merge(newTable.Cell(22, 3));
newTable.Cell(22, 1).Select();//選中一行
//插入圖片
string A22FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A22LinkToFile = false;
object A22SaveWithDocument = true;
object A22Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A22FileName, ref A22LinkToFile, ref A22SaveWithDocument, ref A22Anchor);


//填充表格內容(第23行)
newTable.Cell(23, 1).Range.Text = "十、海拔高度時域曲線,在圖上增加兩個軸繪制出車速、轉速的曲線";
newTable.Cell(23, 1).Range.Font.Size = 14;//設置單元格中字體大小為14
newTable.Cell(23, 1).Range.Bold = 2;//設置單元格中字體為粗體
newTable.Cell(23, 1).Merge(newTable.Cell(23, 3));
//填充表格內容(第24行)
newTable.Cell(24, 1).Merge(newTable.Cell(24, 3));
newTable.Cell(24, 1).Select();//選中一行
//插入圖片
string A24FileName = @"C:\\ExcelData\\另存為.jpg"; //圖片所在路徑
object A24LinkToFile = false;
object A24SaveWithDocument = true;
object A24Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(A24FileName, ref A24LinkToFile, ref A24SaveWithDocument, ref A24Anchor);


//填充表格內容(第25行)
newTable.Cell(25, 1).Range.Text = "文檔創建時間:" + DateTime.Now.ToString("yyyy年M月d日h時s分m秒fff毫秒");
newTable.Cell(25, 1).Range.Font.Size = 16;//設置單元格中字體大小為24
newTable.Cell(25, 1).Merge(newTable.Cell(25, 3));//合並單元格
//填充表格內容(第16行)
//newTable.Cell(16, 1).Merge(newTable.Cell(16, 3));
//newTable.Cell(16, 1).Select();//選中一行


//WdSaveFormat為Word 2007文檔的保存格式
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
//將wordDoc文檔對象的內容保存為DOCX文檔
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//關閉wordDoc文檔對象
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//關閉wordApp組件對象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 創建完畢!");
}
}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved