c#在excel中添加超鏈接示例分享
1.新建一個項目
2.給項目添加引用:Microsoft Excel 12.0 Object Library (2007版本)
using Excel = Microsoft.Office.Interop.Excel;
3.對excel的簡單操作:如下代碼“添加超鏈接”等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelExample
{
class Program
{
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.Application(); // Creates a new Excel Application
excelApp.Visible = true; // Makes Excel visible to the user.
// The following line if uncommented adds a new workbook
//Excel.Workbook newWorkbook = excelApp.Workbooks.Add();
// The following code opens an existing workbook
string workbookPath = "F:\\11.xlsx"; // Add your own path here
Excel.Workbook excelWorkbook = null;
try
{
excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
}
catch
{
//Create a new workbook if the existing workbook failed to open.
excelWorkbook = excelApp.Workbooks.Add();
}
// The following gets the Worksheets collection
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
// The following gets Sheet1 for editing
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
// The following gets cell A1 for editing
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "B1");
// The following sets cell A1's value to "Hi There"
excelCell.Value2 = "Hi There";
Excel.Worksheet excelWorksheet2 = (Excel.Worksheet)excelSheets.get_Item("Sheet2");
Excel.Range excelCell2 = (Excel.Range)excelWorksheet2.get_Range("A1", Type.Missing);
excelCell2.Value2 = "Hi Here";
// Add hyperlinks to the cell A1
//excelWorksheet.Hyperlinks.Add(excelCell,"http:\\www.baidu.com",Type.Missing,"baidu",Type.Missing);
// Add hyperlinks from "sheet1 A1" to "sheet2 A1"
excelWorksheet.Hyperlinks.Add(excelCell, "#Sheet2!A1", Type.Missing, Type.Missing, Type.Missing);
// Close the excel workbook
//excelWorkbook.Close(true,Type.Missing,Type.Missing);
//Quit the excel app
//excelApp.Quit();
}
}
}
相關文章
C#訪問SQL Server數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了C#訪問SQL Server數(shù)據(jù)庫的實現(xiàn)方法,以實例形式簡單分析了C#連接、查詢SQL Server數(shù)據(jù)庫的具體技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10C# Winform 實現(xiàn)控件自適應父容器大小的示例代碼
這篇文章主要介紹了C# Winform 實現(xiàn)控件自適應父容器大小的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03解決C#運行程序修改數(shù)據(jù)后數(shù)據(jù)表不做更新的問題
近日,在使用C#連接數(shù)據(jù)庫的時候,對數(shù)據(jù)庫中的表做更新后,在當前啟動項目中去顯示表數(shù)據(jù)時雖然會發(fā)生一個更新,但是在結束程序運行后再去觀察數(shù)據(jù)表中的記錄時發(fā)現(xiàn)并沒有發(fā)生一個變化,所以本文給大家解決一下這個問題,需要的朋友可以參考下2023-08-08C# winform程序實現(xiàn)開機自啟動并且識別是開機啟動還是雙擊啟動
這篇文章主要介紹了C# winform程序實現(xiàn)開機自啟動并且識別是開機啟動還是雙擊啟動的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10原生實現(xiàn)C#與Lua相互調用方法(Unity3D可用)
Lua是一種很好的擴展性語言,Lua解釋器被設計成一個很容易嵌入到宿主程序的庫,下面這篇文章主要給大家介紹了關于原生實現(xiàn)C#與Lua相互調用方法,Unity3D可用的相關資料,需要的朋友可以參考下2022-04-04