码迷,mamicode.com
首页 > Web开发 > 详细

.Net常用技巧_调用Excel模板格式导出Excel

时间:2014-07-16 23:12:04      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   

object missing = System.Reflection.Missing.Value;  //表示缺i省object

            SaveFileDialog savedialog = new SaveFileDialog();
            savedialog.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls";
            savedialog.Title = "导成Excel";
            if (savedialog.ShowDialog() == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel.ApplicationClass App = new Microsoft.Office.Interop.Excel.ApplicationClass();
                //Microsoft.Office.Interop.Excel.Workbook workbook = App.Workbooks._Open("C:/Documents and Settings/Administrator/桌面/WindowsForms/WindowsForms/20130122.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                //“/”是用在系统上,而“\“一般是用在网络上,所以需要使用@符号转义了
                Microsoft.Office.Interop.Excel.Workbook workbook = App.Workbooks._Open(System.Environment.CurrentDirectory+
                @"\WindowsForms\WindowsForms\20130122.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A3", "C3");
                Object[] objectdata ={ "123", "123", "123" };
                range.Value2 = objectdata;
                App.DisplayAlerts = false;//禁用该“保存”对话框
                workbook.SaveCopyAs(savedialog.FileName);
                workbook.Close(missing, missing, missing);
                App.DisplayAlerts = true;
                App.Quit();
                GC.Collect();
                MessageBox.Show("导出成功!", "Excel", MessageBoxButtons.OK);
            }

.Net常用技巧_调用Excel模板格式导出Excel,布布扣,bubuko.com

.Net常用技巧_调用Excel模板格式导出Excel

标签:style   blog   http   color   使用   os   

原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!