public string GetCellLink(Excel.Worksheet ws, int x, int y)
        #region //获取带有外连接字段的URL函数
        {
            string url="";
            if((ws.Cells[x, y] as Excel.Range).Hyperlinks[1].Address!=null)
            {
                url = (ws.Cells[x, y] as Excel.Range).Hyperlinks[1].Address;
            }
            else if((ws.Cells[x, y] as Excel.Range).Hyperlinks[1].SubAddress!=null)
            {
                url = (ws.Cells[x, y] as Excel.Range).Hyperlinks[1].SubAddress;
            }
            else 
            {
                url = "";
            }
            return url;
        }
        #endregion

解决方案 »

  1.   

    顶一下,我不怎么会用.NET对EXCEL操作,请大家告诉一下,谢谢了
      

  2.   

    using Microsoft.Office.Interop.Excel;object TM = System.Type.Missing;
    string filename = "abc.xls";
    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
    Workbook book = app.Workbooks.Open(filename, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM);
    Worksheet sheet1 = (Worksheet)book1.Sheets[1]; // Sheets[1] 代表第1个工作簿,注意索引不是0起始的。
    int x = 5, y = 7;
    GetCellLink(sheet1, x, y);
      

  3.   

    注意,要添加 microsoft.office.interop.excel.dll 这个dll。
      

  4.   

    项目 -> 添加引用 -> COM -> Microsoft Excel 11.0 Object Library -> 确定。
      

  5.   

    太感谢你了.可是有个新问题,保存不了,提示"1.XLS"只读错,可是1.XLS并没有只读啊
    String PhysicalPath = this.ExcelFileName.Text;//物理地址 
                object TM = System.Type.Missing;
                string filename = PhysicalPath;
                Excel.Application app = new Excel.Application();
                Workbook book = app.Workbooks.Open(PhysicalPath, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM,TM, TM);
                Worksheet sheet1 = (Worksheet)book.Sheets[1]; // Sheets[1] 代表第1个工作簿,注意索引不是0起始的。
                int x = 7, y = 3;
                string url = GetCellLink(sheet1, x, y);
                //this.label8.Text = url.ToString();
                urlcount = url.IndexOf("!",1);
                url=url.Substring(0,urlcount);
                sheet1.Cells[2, 1] = url.ToString();//结果是把单元格A3的链接写到单元格A2中区了
                sheet1.SaveAs(PhysicalPath, TM, TM, TM, TM, TM, TM, TM, TM, TM
                        //#if _EXCEL_2003
                        //                    ,Missing.Value 
                        //                
                        //#endif
                        );
      

  6.   

    sheet1.SaveAs(PhysicalPath, TM, TM, TM, TM, TM, TM, TM, TM, TM);这样保存就提示1.XLS为只读?
    是不是我方法不对呢?
      

  7.   

    Excel.Application app=new Application();
    Excel._Workbook book;
    Excel._Worksheet sheet;
    book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
    Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    sheet=(Excel._Worksheet)book.Sheets[1];
    int j=dvs.Count;
    Excel.Range ran1=app.ActiveCell;
    ran1=sheet.get_Range(sheet.Cells[1,1],sheet.Cells[1,9]);
    ran1.Value2="";


    sheet.Cells[1,"A"]="";
    book.Save();
    book.Close(null,null,null);
    app.Workbooks.Close();
    app.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
    sheet=null;
    book=null;
    app=null;
    GC.Collect();
    GC.Collect();
    GC.Collect();
      

  8.   

    把sheet1.SaveAs(PhysicalPath, TM, TM, TM, TM, TM, TM, TM, TM, TM
    //#if _EXCEL_2003
    // ,Missing.Value
    //
    //#endif
    );改为:app.Save();
    app.Quit();
      

  9.   

    这个方法不行.
    先问了我是否替换掉现有的那个XLS文件,选了是以后
    在app.Quit();行提示 "未处理COMException"            String PhysicalPath = this.ExcelFileName.Text;//物理地址 
                object TM = System.Type.Missing;
                string filename = PhysicalPath;
                Excel.Application app = new Excel.Application();
                Excel._Workbook book;
                Excel._Worksheet sheet1;
                book = (Excel._Workbook)app.Workbooks.Open(PhysicalPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);            sheet1 = (Excel._Worksheet)book.Sheets[1]; // Sheets[1] 代表第1个工作簿,注意索引不是0起始的。
                int x = 7, y = 3;
                string url = GetCellLink(sheet1, x, y);
                //this.label8.Text = url.ToString();
                urlcount = url.IndexOf("!",1);
                url=url.Substring(0,urlcount);
                sheet1.Cells[2, 1] = url.ToString();//结果是把单元格A3的链接写到单元格A2中区了
                app.Save(PhysicalPath);
                app.Quit();
      

  10.   

    这样试试:book.Save();  // 把 app.Save() 改为 book.Save(),不要带参数。
    app.Quit();
      

  11.   

    谢谢大家!
    另外还有一个问题
    http://topic.csdn.net/u/20100331/17/58112dff-0359-432e-b48e-a3c6e6a80003.html