四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2340|回复: 0
打印 上一主题 下一主题

C#在excel中添加超链接

[复制链接]
  • TA的每日心情
    开心
    2022-6-10 09:59
  • 366

    主题

    741

    帖子

    9649

    积分

    超级版主

    Rank: 8Rank: 8

    积分
    9649
    跳转到指定楼层
    楼主
    发表于 2016-11-3 11:55:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    原文地址:http://www.cnblogs.com/luxiaoxun/p/3219141.html

    1.新建一个项目

    2.给项目添加引用:Microsoft Excel 12.0 Object Library (2007版本)
    using Excel = Microsoft.Office.Interop.Excel;

    3.对excel的简单操作:如下代码“添加超链接”等。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using Excel = Microsoft.Office.Interop.Excel;

    6. namespace ExcelExample
    7. {
    8.     class Program
    9.     {
    10.         static void Main(string[] args)
    11.         {
    12.             Excel.Application excelApp = new Excel.Application();  // Creates a new Excel Application
    13.             excelApp.Visible = true;  // Makes Excel visible to the user.

    14.             // The following line if uncommented adds a new workbook
    15.             //Excel.Workbook newWorkbook = excelApp.Workbooks.Add();

    16.             // The following code opens an existing workbook
    17.             string workbookPath = "F:\\11.xlsx";  // Add your own path here

    18.             Excel.Workbook excelWorkbook = null;

    19.             try
    20.             {
    21.                 excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
    22.                     false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
    23.                     false, 0, true, false, false);
    24.             }
    25.             catch
    26.             {
    27.                 //Create a new workbook if the existing workbook failed to open.
    28.                 excelWorkbook = excelApp.Workbooks.Add();
    29.             }

    30.             // The following gets the Worksheets collection
    31.             Excel.Sheets excelSheets = excelWorkbook.Worksheets;

    32.             // The following gets Sheet1 for editing
    33.             string currentSheet = "Sheet1";
    34.             Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

    35.             // The following gets cell A1 for editing
    36.             Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "B1");

    37.             // The following sets cell A1's value to "Hi There"
    38.             excelCell.Value2 = "Hi There";

    39.             Excel.Worksheet excelWorksheet2 = (Excel.Worksheet)excelSheets.get_Item("Sheet2");
    40.             Excel.Range excelCell2 = (Excel.Range)excelWorksheet2.get_Range("A1", Type.Missing);
    41.             excelCell2.Value2 = "Hi Here";

    42.             // Add hyperlinks to the cell A1
    43.             //excelWorksheet.Hyperlinks.Add(excelCell,"http:\\www.baidu.com",Type.Missing,"baidu",Type.Missing);

    44.             // Add hyperlinks from "sheet1 A1" to "sheet2 A1"
    45.             excelWorksheet.Hyperlinks.Add(excelCell, "#Sheet2!A1", Type.Missing, Type.Missing, Type.Missing);

    46.             // Close the excel workbook
    47.             //excelWorkbook.Close(true,Type.Missing,Type.Missing);

    48.             //Quit the excel app
    49.             //excelApp.Quit();
    50.         }
    51.     }
    52. }
    复制代码

    参考:
    http://support.microsoft.com/kb/302084/zh-cn
    http://www.codeproject.com/Artic ... gating-Excel-with-C
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-17 11:11 , Processed in 1.093750 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表