c#在excel中添加超链接示例分享

 更新时间:2013年12月22日 10:07:13   作者:  
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();
        }
    }
}

相关文章

  • WinForm中的登录实现

    WinForm中的登录实现

    在一般的C/S系统中,登录功能基本上是必备功能,总结了三种C# Winform登录功能的实现方法
    2012-11-11
  • C# Winform 分页功能的实现

    C# Winform 分页功能的实现

    本文主要介绍了C# Winform 分页功能的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C#结合JS实现HtmlTable动态添加行并保存到数据库的流程步骤

    C#结合JS实现HtmlTable动态添加行并保存到数据库的流程步骤

    在 Web 应用项目中,实现一对多录入的数据管理功能是一项常见的应用,因此可以实现一个相对轻量化的设计实现表格的录入,为保证功能的可用性、界面友好性,本文给大家介绍了C#结合JS实现HtmlTable动态添加行并保存到数据库,需要的朋友可以参考下
    2024-12-12
  • C# 运算符 ?、??、?: 各种问号的用法和说明

    C# 运算符 ?、??、?: 各种问号的用法和说明

    本文介绍C#中三种常见的问号运算符的使用方法,简单讲解给大家,希望对大家有所帮助。
    2016-04-04
  • C#实现数组元素的数据类型转换方法详解

    C#实现数组元素的数据类型转换方法详解

    这篇文章主要为大家介绍了C#中一维数组如何快速实现数组元素的数据类型的转换,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-04-04
  • C#多线程之Thread中Thread.Join()函数用法分析

    C#多线程之Thread中Thread.Join()函数用法分析

    这篇文章主要介绍了C#多线程之Thread中Thread.Join()函数用法,实例分析了Thread.Join()方法的原理与使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#微信公众号开发之接收事件推送与消息排重的方法

    C#微信公众号开发之接收事件推送与消息排重的方法

    这篇文章主要介绍了C#微信公众号开发之接收事件推送与消息排重的方法,详细分析了事件推送与消息排重的使用技巧,对微信开发有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • Unity实现场景加载功能

    Unity实现场景加载功能

    这篇文章主要为大家详细介绍了Unity实现场景加载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • 基于WPF实现弹幕效果的示例代码

    基于WPF实现弹幕效果的示例代码

    这篇文章主要为大家详细介绍了如何利用WPF实现弹幕效果,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2022-09-09
  • C#通过标签软件Bartender的ZPL命令打印条码

    C#通过标签软件Bartender的ZPL命令打印条码

    这篇文章介绍了C#通过标签软件Bartender的ZPL命令打印条码,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01

最新评论