方法一,在已有的excel表格中添加 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; // 引用这个才能使用Missing字段 using Excel;namespace WindowsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //创建Application对象 Excel.Application xApp = new Excel.ApplicationClass(); string strPath = Application.StartupPath + "\\Sample.xls"; xApp.Visible = true; //得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件 Excel.Workbook xBook = xApp.Workbooks._Open(@"D:\Sample.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1]; Excel.Worksheet xSheet2 = (Excel.Worksheet)xBook.Sheets[2]; Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value); rng3.Value2 = "=MAX(B1:B6)"; Excel.Range rng4 = xSheet2.get_Range("C6", Missing.Value); rng4.Value2 = "=MAX(B1:B6)"; xBook.SaveAs(@"D:\CData.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } } }
方法二:新创建一个,没有模板的情况 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.OleDb;namespace ReadExcel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {
连接字符串
excel 2003
sqlstr="provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0'; datasource="+path;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection; // 引用这个才能使用Missing字段
using Excel;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
string strPath = Application.StartupPath + "\\Sample.xls"; xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook = xApp.Workbooks._Open(@"D:\Sample.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
Excel.Worksheet xSheet2 = (Excel.Worksheet)xBook.Sheets[2]; Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value);
rng3.Value2 = "=MAX(B1:B6)"; Excel.Range rng4 = xSheet2.get_Range("C6", Missing.Value);
rng4.Value2 = "=MAX(B1:B6)"; xBook.SaveAs(@"D:\CData.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value); }
}
}
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.OleDb;namespace ReadExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
DataRow workRow; DataTable workTable = new DataTable("Customers"); DataColumn workCol = workTable.Columns.Add("NO.", typeof(String));
workCol.AllowDBNull = false;
workCol.Unique = true;
string str = "syfdhf";
workTable.Columns.Add(str, typeof(UInt32));
//workTable.Columns.Add("CustFNam2e", typeof(String));
//workTable.Columns.Add("Purchase3s", typeof(Double));
workRow = workTable.NewRow();
workRow[0] = "Min Limited";
workTable.Rows.Add(workRow);
workRow = workTable.NewRow();
workRow[0] = "Max Limited";
workTable.Rows.Add(workRow);
//workTable.Columns.Add("222222", typeof(Double)); string strPath = Application.StartupPath + "\\BaoKe.xls";
string strSheetName = "Data";
WriteExcel(workTable, strPath, strSheetName);
} public static void WriteExcel(DataTable dtSource, string strPath, string strSheetName)
{ System.Data.OleDb.OleDbConnection OleDb_Conn = new OleDbConnection();
OleDb_Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';" + "Data Source=\"" + strPath + "\""; try
{
OleDb_Conn.Open();
OleDbCommand OleDb_Comm = new OleDbCommand();
OleDb_Comm.Connection = OleDb_Conn; string strCmd;
try
{
strCmd = "drop table [" + strSheetName + "]";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
} strCmd = "create Table [" + strSheetName + "](";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "[" + dc.ColumnName + "] nvarchar(20),";
}
strCmd = strCmd.Trim().Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd; OleDb_Comm.ExecuteNonQuery(); foreach (DataRow dr in dtSource.Rows)
{
if (dr.RowState != System.Data.DataRowState.Deleted)
{
strCmd = "insert into [" + strSheetName + "] values(";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "'" + dr[dc.ColumnName].ToString() + "',";
} strCmd = strCmd.Substring(0, strCmd.Length - 1);
strCmd += ")"; OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery(); }
}
OleDb_Conn.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
OleDb_Conn.Close();
}
}
}
}
参考这个帖子
在找的过程中你会学到很多东西.
Interop.Microsoft.Office.Core.dll
Interop.VBIDE.dll这几个控件
,就可以像使用数据库一样使用了