/// <summary>
/// 删除指定的整行
/// 作者:长江支流 www.webmis.com.cn
/// </summary>
/// <param name="p_rowIndex">行索引</param>
public void DeleteRow(int p_rowIndex)
{
Excel.Range range;
range = GetRange(p_rowIndex,"A");
range.Select();
range.EntireRow.Delete(oMissing);
}
需要二
public void DeleteRows(int rowIndex,int count)
{
try
{
for(int n=1;n<=this.WorkSheetCount;n++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets[n];
range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
for(int i=0;i<count;i++)
{
range.Delete(Excel.XlDirection.xlDown);
}
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
Excel2000和Excel2003中的插入行的参数是不一样的,但是用一个版本的开发编译后可以在另一版本中运行。
/// <summary>
/// 在指定的行上插入一整行
/// 作者:长江支流 www.webmis.com.cn
/// </summary>
/// <param name="p_rowIndex">行索引</param>
public void InsertRow(int p_rowIndex)
{
Excel.Range range;
range = GetRange(p_rowIndex,"A"); //楼主用自己的方法取得插入行的Range
range.Select();
//Excel2003支持两参数
//range.EntireRow.Insert(oMissing,oMissing);
//Excel2000支持一个参数,经过测试,用Interop.ExcelV1.3(Excel2000),可以正常运行在Excel2003中
range.EntireRow.Insert(oMissing);
}
m_oSheet.get_Range(1,1).Rows.InsertIndent(0);
给你个方法
using Excel = Microsoft.Office.Interop.Excel;
...
...
...
Excel.Application myExcel;
myExcel = new Excel.Application();
/// <summary>
/// 删除一个区域
/// </summary>
/// <param name="startRow"></param>
/// <param name="startColumn"></param>
/// <param name="endRow"></param>
/// <param name="endColumn"></param>
public void Delete(int startRow, int startColumn, int endRow, int endColumn, bool IsDeleteEntireRow)
{
Excel.Range range = myExcel .get_Range(myExcel .Cells[startRow, startColumn], myExcel .Cells[endRow, endColumn]);
range.Select();
if (IsDeleteEntireRow)//是否整行删除
range.EntireRow.Delete(XlDeleteShiftDirection.xlShiftUp);
else
range.Delete(XlDeleteShiftDirection.xlShiftUp);
}
private void Form1_Load(object sender, System.EventArgs e)
{
my=new Excel.ApplicationClass();
my.Visible=true;
object objMissing=System.Reflection.Missing.Value;
mybook=my.Workbooks.Open("g://123.xls",objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing);
mysheet=(Excel.Worksheet)mybook.Worksheets.get_Item(1);
}
private void button1_Click(object sender, System.EventArgs e)//添加
{
((Excel.Range) mysheet.Cells[3,3]).Select();
((Excel.Range) mysheet.Cells[3,3]).EntireRow.Insert(0);
}
private void button2_Click(object sender, System.EventArgs e)//DELETE
{
((Excel.Range) mysheet.Cells[3,3]).Select();
((Excel.Range) mysheet.Cells[3,3]).EntireRow.Delete(0);
}
邮件源码下载地址:
http://download.csdn.net/down/138012/chenxh