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();
        }
    }
}


转载于:https://my.oschina.net/u/582827/blog/1539794

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值