1、帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; using System.Reflection; namespace ExcelDemo { public delegate List<T> ReadExcelDelegate<T>(Worksheet wsSheet); public delegate void WriteExcelDelegate<T>(Worksheet wsSheet, List<T> infoList); public class ExcelHelper<T> { private object missing = Missing.Value; private Application excelApp; private Workbook wsBook; public ReadExcelDelegate<T> ReadOperation; public WriteExcelDelegate<T> WriteOperation; public ExcelHelper() { } public List<T> Read(string fileName, int index) { List<T> list = null; Worksheet wsSheet = null; try { if (excelApp != null) { excelApp = null; } if (wsBook != null) { wsBook = null; } excelApp = new Application(); wsBook = excelApp.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); wsSheet = (Worksheet)wsBook.Sheets.get_Item(index); if (ReadOperation != null) list = ReadOperation(wsSheet); //wsBook.Save(); excelApp.DisplayAlerts = false; excelApp.Visible = false; } catch (Exception ex) { //write log; throw; } finally { wsSheet = null; Close(); } return list; } public void Write(string fileName, List<T> infoList, int index) { Worksheet wsSheet = null; try { if (excelApp != null) { excelApp = null; } if (wsBook != null) { wsBook = null; } excelApp = new Application(); wsBook = excelApp.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); wsSheet = (Worksheet)wsBook.Sheets.get_Item(index); if (WriteOperation != null) WriteOperation(wsSheet, infoList); wsBook.Save(); excelApp.DisplayAlerts = false; excelApp.Visible = false; } catch (Exception ex) { //write log; throw; } finally { wsSheet = null; Close(); } } public void Close() { wsBook.Close(false, missing, missing); wsBook = null; excelApp.Quit(); excelApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } } 2、调用 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; using System.IO; namespace ExcelDemo { class Program { static void Main(string[] args) { ExcelHelper<Student> excel = new ExcelHelper<Student>(); excel.ReadOperation = ReadStudentExcel; excel.WriteOperation = WriteStudentExcel; //读Excel List<Student> list = excel.Read(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"students.xls"), 1); //写Excel excel.Write(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"students.xls"), list, 1); Console.WriteLine(list.Count()); Console.WriteLine("OK"); //使用CTRL+F5运行程序时,若有此句(Console.Read()),使用其他读写Excel时,Excel进程打开后,不能关闭。 //使用泛型委托便可以解决此问题 Console.Read(); } public static List<Student> ReadStudentExcel(Worksheet wsSheet) { List<Student> students = new List<Student>(); for (int i = 2; ; i++) { dynamic id = wsSheet.get_Range("A" + i, Type.Missing).Value2; if (id == null) { break; } Student student = new Student(int.Parse(id.ToString()), wsSheet.get_Range("B" + i, Type.Missing).Value2); students.Add(student); } return students; } public static void WriteStudentExcel(Worksheet wsSheet, List<Student> infoList) { int i = 2; foreach (var item in infoList) { wsSheet.Cells[i, 5] = item.Id; wsSheet.Cells[i, 6] = item.Name; i++; } } } public class Student { public int Id { get; set; } public string Name { get; set; } public Student(int id, string name) { Id = id; Name = name; } } } 3、Excel文件 第一列为Id,第二列为Name