View Code
1 //创建excel 2 public void CreateExcel(string tableName, string tableType , List<HNFatherAddress> list) 3 { 4 object missing = System.Reflection.Missing.Value; 5 MSExcel.Application app = new MSExcel.Application(); 6 app.Application.Workbooks.Add(true); 7 MSExcel.Workbook book = (MSExcel.Workbook)app.ActiveWorkbook; 8 MSExcel.Worksheet sheet = (MSExcel.Worksheet)book.ActiveSheet; 9 //第一行 10 sheet.Cells[1, 1] = "table"; 11 sheet.Cells[1, 2] = tableName; 12 sheet.Cells[1, 3] = tableType; 13 14 //第二行 15 sheet.Cells[2, 1] = "编号"; 16 sheet.Cells[2, 2] = "地址"; 17 sheet.Cells[2, 3] = "类型"; 18 19 int count = 2; 20 21 foreach (HNFatherAddress item in list) 22 { 23 count = count + 1; 24 sheet.Cells[count, 1] = "num"; 25 sheet.Cells[count, 2] = item.FatherName; 26 sheet.Cells[count, 3] = item.FatherType; 27 28 } 29 //保存 30 SaveFileDialog save = new SaveFileDialog(); 31 save.Filter = "Excel(*.xls)|*.xls"; 32 save.ShowDialog(); 33 34 string savePath = save.FileName; 35 book.SaveCopyAs(savePath); 36 book.Close(false, missing, missing); 37 38 app.Quit(); 39 }