日期:2014-05-18  浏览次数:21419 次

C#实现修改Excel表格数据
要实现这样的功能,可以读取Excel表格数据,并修改表格上面的数据,各位大侠帮帮忙...

------解决方案--------------------
// csc /r:Interop.Excel.dll

namespace Skyiv
{
using Excel;

public class Excel2000 : ApplicationClass
{
object TM = System.Type.Missing;

public Excel2000()
{
DisplayAlerts = false;
}

// 返回当前工作表的使用的行数
public int GetRowsCount()
{
return ((Worksheet)ActiveSheet).UsedRange.Rows.Count;
}

// 返回工作表 sheet 的使用的行数
public int GetRowsCount(int sheet)
{
return ((Worksheet)ActiveWorkbook.Sheets[sheet]).UsedRange.Rows.Count;
}

// 用模板创建工作簿
public void CreateFromTemplet(string templet, params string [] names)
{
foreach (string name in names)
{
(Workbooks.Add(templet)).SaveAs(name, TM, TM, TM, TM, TM, XlSaveAsAccessMode.xlNoChange, TM, TM, TM, TM);
}
}

// 打开工作簿
public void Open(params string [] names)
{
foreach (string name in names)
{
Workbooks.Open(name, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM, TM);
}
}

// 复制当前工作簿的 sheet0 到 sheet1 之前
public void CopySheetBefore(int sheet0, int sheet1)
{
((Worksheet)ActiveWorkbook.Sheets[sheet0]).Copy(ActiveWorkbook.Sheets[sheet1], TM);
}

// 复制当前工作簿的 sheet0 到 sheet1 之后
public void CopySheetAfter(int sheet0, int sheet1)
{
((Worksheet)ActiveWorkbook.Sheets[sheet0]).Copy(TM, ActiveWorkbook.Sheets[sheet1]);
}

// 复制工作簿 book 的 sheet0 到 sheet1 之后
public void CopySheetAfter(int book, int sheet0, int sheet1)
{
((Worksheet)Workbooks[book].Sheets[sheet0]).Copy(TM, Workbooks[book].Sheets[sheet1]);
}

// 把删除当前工作簿的工作表 sheet
public void DeleteSheet(int sheet)
{
((Worksheet)ActiveWorkbook.Sheets[sheet]).Delete();
}

// 返回工作表 sheet 的名称
public string GetSheetName(int sheet)
{
return ((Worksheet)ActiveWorkbook.Sheets[sheet]).Name;
}

// 把当前工作表改名为 name
public void SetSheetName(string name)
{
((Worksheet)ActiveSheet).Name = name;
}

// 把工作表 sheet 改名为 name
public void SetSheetName(int sheet, string name)
{
((Worksheet)ActiveWorkbook.Sheets[sheet]).Name = name;
}

// 把工作簿 book 的工作表 sheet 改名为 name
public void SetSheetName(int book, int sheet, string name)
{
((Worksheet)Workbooks[book].Sheets[sheet]).Name = name;
}

// 返回当前工作表的单元格的值
public object GetCellValue(object col, object row)
{
return ((Range)((Worksheet)ActiveSheet).Cells[row, col]).Value;
}

// 返回工作表sheet的单元格的值
public object GetCellValue(int sheet, object col, object row)
{
return ((Range)((Worksheet)ActiveWorkbook.Sheets[sheet]).Cells[row, col]).Value;
}

// 对当前工作表的单元格赋值
public void SetCellText(object col, object row, object txt)
{
Cells[row, col] = txt;
}

// 对book工作簿的sheet工作表的单元格赋值
public void SetCellText(object book, object sheet, object col, object row, object txt)
{
((Worksheet)Workbooks[book].Sheets[sheet]).Cells[row, col] = txt;
}

// 对book工作簿的sheet工作表, 在第row行插入一行(复制本行)
public void InsertCopyRowAt(object book, object sheet, object row)
{
((Range)((Worksheet)Workbooks[book].Sheets[sheet]).Rows[row, TM]).Copy(TM);
((Range)((Worksheet)Workbooks[book].Sheets[sheet]).Cells[row, 1]).EntireRow.Insert(0);
}

// 在第row行插入一行(复制本行)
public void InsertCopyRowAt(object row)