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();
        }
    }
}
复制代码

参考:

http://support.microsoft.com/kb/302084/zh-cn

http://www.codeproject.com/Articles/5123/Opening-and-Navigating-Excel-with-C

  

 

作者: 阿凡卢
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关文章
|
9月前
|
前端开发 C#
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
|
1月前
|
存储 SQL C#
C#实现Excel合并单元格数据导入数据集
C#实现Excel合并单元格数据导入数据集
|
8月前
|
C#
45【软件技术基础】C#调用NPOI插件对EXCEL进行处理
NPOI插件进行EXCEL表格处理,不依赖第三方软件,功能强大,使用简便。 C#调用NPOI插件进行EXCEL单个表和文件夹中批量表的处理。
142 1
|
4月前
|
XML JSON 数据处理
C# | 导出DataGridView中的数据到Excel、CSV、TXT
从DataGridView中导出数据到Excel、CSV、TXT是开发中非常常见的一种需求。本文将讲解如何高效的完成对这三种格式的单向导出。 倘若直接写三种格式的导出必定会产生大量的重复代码,而从表中获取结构化数据的思路是基本一致的,因此有一个思路是先将DataGridView中的数据转换为DataTable对象,再进一步导出成我们的目标格式。 本文将介绍如何将DataGridView中的数据转换为DataTable格式,并提供将DataTable转换为Excel、CSV、TXT三种格式的例子。
102 0
C# | 导出DataGridView中的数据到Excel、CSV、TXT
|
5月前
|
XML JSON 开发框架
盘点5个C#实用的Word、PPT、Excel、Mail第三方库
盘点5个C#实用的Word、PPT、Excel、Mail第三方库
84 0
|
6月前
|
C#
C#写入模板excel数据
C#写入模板excel数据
27 0
|
9月前
|
数据库 C#
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
220 0
|
10月前
|
C#
C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
154 0
|
JSON 程序员 C#
一个基于C#开发的Excel转Json工具
一个基于C#开发的Excel转Json工具,比通过Office Excel组件访问数据性能提升100倍,支持界面、命令模式。
207 0
一个基于C#开发的Excel转Json工具