如题,应该需要调用库函数,并定义类以及语句操作,但我是新手,请路过的详细指点,等!!!!!!!!
解决方案 »
- WinForm ToolStripItemCollection动态加载自定义对象问题
- Socket,局域网内发送数据时,在XP环境下测试正常,但是在win2003下测试提示“未知主机”
- 数据库连接不上
- 如何在WIn Form中实现多个文件的上传,下载?
- 水晶报表无效密钥号码问题???急求!
- 如何注册"Microsoft Jet.4.0"提供程序
- 找字符串的问题
- 怎样输出13/0,1,3/5040,5041,5042所有组合?
- 在form 的activated event 里面使用messagebox的问题
- 如何改变窗口的弹出模式?如渐变谈出或有小到大弹出?
- 有没有开发winform 小工具
- C#调用dll里的方法问题?
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace TestAccess
{
class Program
{
static void Main(string[] args)
{
string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";
strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
DataSet ds = new DataSet();
myCommandd.Fill(ds, "[Sheet1$]");
System.Data.DataTable dt = ds.Tables["[Sheet1$]"];
Console.WriteLine(dt.Columns[0].ToString());
Console.WriteLine(dt.Columns[1].ToString());
DataRow drDisplay = dt.Rows[0];
int[] num = new int[dt.Columns.Count];
for (int j = 0; ; )
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (drDisplay[i] is DBNull) ;
else
num[i] += Convert.ToInt32(drDisplay[i]);
}
if (++j >= dt.Rows.Count) break;
drDisplay = dt.Rows[j];
}
objConnection.Close();
object MissingValue = Type.Missing;
Microsoft.Office.Interop.Excel.Application app = new Application();
Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
for (int i = 0; i < dt.Columns.Count; i++)
{
//注意下面是i+1,,excel小标默认从1开始
wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();
}
wbook.Save();
wbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet = null;
wbook = null;
app = null;
GC.Collect();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspx
Application app = new Application();
app.Visible=true;
Workbook book = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet sheet = (Worksheet)book.Sheets[1];
object misvalue = Missing.Value;
sheet.Cells[1, 1] = "ID";
sheet.Cells[1, 2] = "Name";
sheet.Cells[1, 3] = "Gender";
book.SaveAs(path, XlFileFormat.xlWorkbookNormal, misvalue, misvalue, misvalue, misvalue, XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue);
book.Close(true, misvalue, misvalue);
app.Quit();
调用的这个类库3F 基本已经是正解了