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

时间:2023-06-03 14:10:45 

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

标签:excel中添加超链接
0
投稿

猜你喜欢

  • C#编写游戏客户端的实现代码

    2021-08-28 06:24:58
  • 详解Spring Security如何配置JSON登录

    2023-02-08 17:39:07
  • Spring Boot Logback配置日志过程解析

    2022-12-09 18:08:06
  • Avalonia封装实现指定组件允许拖动的工具类

    2023-01-01 19:03:03
  • Android五大布局与实际应用详解

    2022-01-30 05:21:36
  • C# FileStream文件读写详解

    2021-10-27 15:44:14
  • Spring boot Mybatis 整合(完整版)

    2023-03-28 17:14:32
  • spring boot使用sharding jdbc的配置方式

    2022-02-16 00:29:15
  • Mono for Android 实现高效的导航(Effective Navigation)

    2023-09-05 21:39:56
  • 用c#实现简易的计算器功能实例代码

    2022-05-09 19:28:51
  • Java Lambda List转Map代码实例

    2022-05-24 20:15:42
  • Quarkus中RESTEasy Reactive集成合并master分支

    2023-06-07 14:20:45
  • Java 使用 HttpClient 发送 GET请求和 POST请求

    2023-07-23 07:56:13
  • Android UI之ImageView实现图片旋转和缩放

    2023-08-04 02:53:39
  • Java实现将txt文件转成xls文件的方法

    2022-05-20 10:21:25
  • Java类和成员变量声明类详解

    2023-02-13 18:54:46
  • 利用Intellij Idea连接远程服务器实现远程上传部署功能

    2022-05-31 13:15:54
  • SpringBoot自定义注解实现Token校验的方法

    2023-11-13 23:17:52
  • Spring+SpringMVC+MyBatis深入学习及搭建(一)之MyBatis的基础知识

    2021-09-27 15:12:59
  • MVC设定默认路由为指定的Area下的某个action

    2023-03-03 00:00:56
  • asp之家 软件编程 m.aspxhome.com