写的一个类 class wo
{
public wo(string Excelpath)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Visible = false;
Workbook wb = excel.Workbooks._Open(Excelpath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
public void ExcelReplace(string strOldText, string strNewText)
{
int icount = wb.Sheets.Count; for (int i = 1; i <= icount; i++)
{ xSheet = (Worksheet)wb.Sheets[i]; object what = strOldText; //查找字符串 object retxt = strNewText; //替换字符串 xSheet.Cells.Replace(what, retxt, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); }
}
}
下面的是FORM里一个按钮的事件 private void button1_Click(object sender, EventArgs e)
{
wo w = new wo(@"C:\Documents and Settings\Administrator\桌面\Sheet1.xls");
w.ExcelReplace("object1.Tittle", "qq");
}总是提示ExcelReplace()里的wb没有获取对象
{
public wo(string Excelpath)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Visible = false;
Workbook wb = excel.Workbooks._Open(Excelpath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
public void ExcelReplace(string strOldText, string strNewText)
{
int icount = wb.Sheets.Count; for (int i = 1; i <= icount; i++)
{ xSheet = (Worksheet)wb.Sheets[i]; object what = strOldText; //查找字符串 object retxt = strNewText; //替换字符串 xSheet.Cells.Replace(what, retxt, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); }
}
}
下面的是FORM里一个按钮的事件 private void button1_Click(object sender, EventArgs e)
{
wo w = new wo(@"C:\Documents and Settings\Administrator\桌面\Sheet1.xls");
w.ExcelReplace("object1.Tittle", "qq");
}总是提示ExcelReplace()里的wb没有获取对象
public wo(string Excelpath) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Visible = false; wb = excel.Workbooks._Open(Excelpath,