C#在excel中添加超連結

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   for   div   sp   art   

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

C#在excel中添加超連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.