C#在excel中添加超链接
2024-08-30 08:15:21
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, ,
false, , "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, , 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
最新文章
- WCF、Web API、WCF REST、Web Service比较
- oracle基础教程(8)oracle修改字符集
- hibernate 实现分页查询语句、单条查询语句、多条查询语句、修改、删除语句
- Android Studio插件安装及使用Genymotion模拟器
- 【leetcode】Path Sum I &; II(middle)
- 注解:【有连接表的】Hibernate单向N->;N关联
- PHPCMS V9静态化HTML生成设置及URL规则优化
- Jquery 对象与 DOM对象的相互转换
- mysql数据库本地化操作
- lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
- 堆排序 C++
- hibernate中一对多 多对多 inverse cascade
- 液晶顯示器 LCD (Liquid Crystal Disply )
- [转]Patching the Mach-o Format the Simple and Easy Way
- Hrbustoj 2266 Legendary Weights(辗转相除求最大公约数)
- struts2标签库----控制标签详解
- [国嵌攻略][070][GDB调试程序]
- sys系统模块
- Log4Net 添加自定义字段并保存到数据库
- Gradle 大杂烩
热门文章
- tornado RequestHandler request.body &; request.arguments
- 将 JAR 转为 EXE – EXE4J 的使用教程(第一期)(转载)
- dtw算法优化(重写C语言版本)
- ubuntu14.04 us sources.list
- CSS中的float与clear
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
- ALV 输入数据时数字缩小一千倍的问题解决
- Effective C++ -----条款40:明智而审慎地使用多重继承
- 【leetcode】Merge Intervals(hard)
- 【资料】Boost的资料