创建 Excel 表格

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         }

转载于:https://www.cnblogs.com/ZJ199012/archive/2012/09/26/2703581.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值