C# 操作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)
{
((Range)((Worksheet)ActiveSheet).Rows[row, TM]).Copy(TM);
((Range)((Worksheet)ActiveSheet).Cells[row, 1]).EntireRow.Insert(0);
}
// 在第col行插入一列(复制本列)
public void InsertCopyColumnAt(object col)
{
((Range)((Worksheet)ActiveSheet).Columns[col, TM]).Copy(TM);
((Range)((Worksheet)ActiveSheet).Cells[1, col]).EntireColumn.Insert(0);
}
// 对book工作簿的sheet工作表, 删除第row行
public void DeleteRowAt(object book, object sheet, object row)
{
((Range)((Worksheet)Workbooks[book].Sheets[sheet]).Rows[row, TM]).Delete(TM);
}
// 删除第row行
public void DeleteRowAt(object row)
{
((Range)((Worksheet)ActiveSheet).Rows[row, TM]).Delete(TM);
}
// 分类汇总
public void Subtotal(int r0, int c0, int r1, int c1, int GroupBy, int[] TotalList)
{
get_Range(Cells[r0,c0], Cells[r1,c1]).
Subtotal(GroupBy, XlConsolidationFunction.xlSum, TotalList, TM, TM, XlSummaryRow.xlSummaryBelow);
}
// 清除
public void Clear(int r0, int c0, int r1, int c1)
{
get_Range(Cells[r0,c0], Cells[r1,c1]).Clear();
}
// 保存所有工作簿
public void SaveAll()
{
foreach (Workbook book in Workbooks)
{
book.Save();
}
}
}
}