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
#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
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);
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
);
是不是我方法不对呢?
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();
//#if _EXCEL_2003
// ,Missing.Value
//
//#endif
);改为:app.Save();
app.Quit();
先问了我是否替换掉现有的那个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();
app.Quit();
另外还有一个问题
http://topic.csdn.net/u/20100331/17/58112dff-0359-432e-b48e-a3c6e6a80003.html