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

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

时间: 2024-12-24 14:00:55

.Net常用技巧_调用Excel模板格式导出Excel的相关文章

.Net常用技巧_操作Excel知识点

C#操作Excel知识点 近期在使用C#操作excel,主要是读取excel模板,复制其中的模板sheet页,生成多个sheet页填充相应数据后另存到excel文件,所用到的知识点如下. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Off

.Net常用技巧_导出 Excel 和相关打印设置

Excel.Application myExcel = new Excel.Application();发 表Excel.Workbook workbookData = myExcel.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);Excel.Worksheet xlSheet = (Worksheet)workbookData.Worksheets[1];//取得sheet1 1) 显示当前窗口: xlSheet.

.Net常用技巧_传入DataGrid直接导出成Excel

注:非调用OFFICE的DLL方法. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Utility; using System.IO; names

.Net常用技巧_生成单据号

自己用的,没整理,代码比较乱,请不要学我. using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateDocNo { /// <summary> /// 获取单号 /// <

.Net常用技巧_汉字转拼音类(除汉字外,其余的原样输出)

注:如果是多音字,则默认取第一个拼音. using System; using System.Collections.Generic; using System.Text; using Microsoft.International.Converters.PinYinConverter; namespace MyTool { /// <summary> /// 汉字转拼音类 /// </summary> public class PinYinConver { public PinY

.Net常用技巧_动态加载Dll

原理如下: 1.利用反射进行动态加载和调用. Assembly ass=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll Assembly.LoadFile 只加载指定文件,并不会自动加载依赖程序集.Assmbly.Load无需后辍名 2.加载dll后,需要使用dll中某类. Type type=ass.GetType(“TypeName”);//用类型的命名空间和名称获得类型 3.需要实例化类型,才可以使用

.Net常用技巧_生成物料编号(根据分类代码生成)

这个是自己用的,不一定符合您的需求. using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateMaterialCode { /// <summary> /// 获取新物料编号(

.Net常用技巧_读取XML某节点例子

注:此例子只是自己在代码中为了读某固定的几个值,写的有点死,所以另作他用的时候请自行修改或扩充 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; namespace MyTool {

.net常用技巧_窗体中回车键转TAB键

在窗体的KeyDown()事件中 /// <summary> /// 表单的回车转TAB键 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BasicForm_KeyDown(object sender, KeyEventArgs e) { #region 回车转