using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application m_objExcel = null;
Microsoft.Office.Interop.Excel.Workbooks m_objBooks = null;
Microsoft.Office.Interop.Excel.Workbook m_objBook = null;
Microsoft.Office.Interop.Excel.Sheets m_objSheets = null;
Microsoft.Office.Interop.Excel.Worksheet m_objSheet = null;
Microsoft.Office.Interop.Excel.Range o_NO = null;
Microsoft.Office.Interop.Excel.Range o_Name = null;
Microsoft.Office.Interop.Excel.Range o_Tel = null;
Microsoft.Office.Interop.Excel.Range o_QQ = null;
Microsoft.Office.Interop.Excel.Range o_Email = null;
Microsoft.Office.Interop.Excel.Range indexs = null;
try
{
m_objExcel = new Microsoft.Office.Interop.Excel.Application();
m_objExcel.Visible = false;
m_objBooks = m_objExcel.Workbooks;
m_objBooks.Add(true);
m_objBook = m_objExcel.Workbooks.Add(@"D:T.xlt");
m_objSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet;
m_objExcel.DisplayAlerts = false;
foreach (Microsoft.Office.Interop.Excel.Name nm in m_objExcel.Names)
{
switch (nm.Name)
{
case "NO":
o_NO = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Name":
o_Name = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Tel":
o_Tel = m_objExcel.get_Range(nm, Type.Missing);
break;
case "QQ":
o_QQ = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Email":
o_Email = m_objExcel.get_Range(nm, Type.Missing);
break;
case "index":
indexs = m_objExcel.get_Range(nm, Type.Missing);
break;
default:
break;
}
}
for (int i = 0; i < 5; i++)
{
if (i > 0)
{
indexs.Rows.EntireRow.Activate();
indexs.Rows.EntireRow.Cells.Copy(Type.Missing);
indexs.Rows.EntireRow.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
indexs.Rows.EntireRow.ClearContents();
}
o_NO.set_Item(Type.Missing, Type.Missing, (i + 1).ToString());
o_Name.set_Item(Type.Missing, Type.Missing, "王康");
o_Tel.set_Item(Type.Missing, Type.Missing, "138XXXX4349");
o_QQ.set_Item(Type.Missing, Type.Missing, "3987223");
o_Email.set_Item(Type.Missing, Type.Missing, "Walter.K.Wang@gmail.com");
}
m_objExcel.DisplayAlerts = false;
//保存XLS
m_objBook.SaveAs(@"D:" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls",
Microsoft.Office.Interop.Excel.XlFileFormat.xlTemplate,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
m_objBook.Close(false, false, Type.Missing);
}
catch(Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Email);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_QQ);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Tel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Name);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_NO);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch
{
o_Email = null;
o_QQ = null;
o_Tel = null;
o_Name = null;
o_NO = null;
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application m_objExcel = null;
Microsoft.Office.Interop.Excel.Workbooks m_objBooks = null;
Microsoft.Office.Interop.Excel.Workbook m_objBook = null;
Microsoft.Office.Interop.Excel.Sheets m_objSheets = null;
Microsoft.Office.Interop.Excel.Worksheet m_objSheet = null;
Microsoft.Office.Interop.Excel.Range o_NO = null;
Microsoft.Office.Interop.Excel.Range o_Name = null;
Microsoft.Office.Interop.Excel.Range o_Tel = null;
Microsoft.Office.Interop.Excel.Range o_QQ = null;
Microsoft.Office.Interop.Excel.Range o_Email = null;
Microsoft.Office.Interop.Excel.Range indexs = null;
try
{
m_objExcel = new Microsoft.Office.Interop.Excel.Application();
m_objExcel.Visible = false;
m_objBooks = m_objExcel.Workbooks;
m_objBooks.Add(true);
m_objBook = m_objExcel.Workbooks.Add(@"D:T.xlt");
m_objSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet;
m_objExcel.DisplayAlerts = false;
foreach (Microsoft.Office.Interop.Excel.Name nm in m_objExcel.Names)
{
switch (nm.Name)
{
case "NO":
o_NO = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Name":
o_Name = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Tel":
o_Tel = m_objExcel.get_Range(nm, Type.Missing);
break;
case "QQ":
o_QQ = m_objExcel.get_Range(nm, Type.Missing);
break;
case "Email":
o_Email = m_objExcel.get_Range(nm, Type.Missing);
break;
case "index":
indexs = m_objExcel.get_Range(nm, Type.Missing);
break;
default:
break;
}
}
for (int i = 0; i < 5; i++)
{
if (i > 0)
{
indexs.Rows.EntireRow.Activate();
indexs.Rows.EntireRow.Cells.Copy(Type.Missing);
indexs.Rows.EntireRow.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
indexs.Rows.EntireRow.ClearContents();
}
o_NO.set_Item(Type.Missing, Type.Missing, (i + 1).ToString());
o_Name.set_Item(Type.Missing, Type.Missing, "王康");
o_Tel.set_Item(Type.Missing, Type.Missing, "138XXXX4349");
o_QQ.set_Item(Type.Missing, Type.Missing, "3987223");
o_Email.set_Item(Type.Missing, Type.Missing, "Walter.K.Wang@gmail.com");
}
m_objExcel.DisplayAlerts = false;
//保存XLS
m_objBook.SaveAs(@"D:" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls",
Microsoft.Office.Interop.Excel.XlFileFormat.xlTemplate,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
m_objBook.Close(false, false, Type.Missing);
}
catch(Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Email);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_QQ);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Tel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_Name);
System.Runtime.InteropServices.Marshal.ReleaseComObject(o_NO);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch
{
o_Email = null;
o_QQ = null;
o_Tel = null;
o_Name = null;
o_NO = null;
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
}
}
}
}
}