有没有好消息!我现在真的是没有办法了,真是郁闷呀.郁闷!
我就不信温室里的花到户外就不能绽放,笼中的贵鸟就飞不到自己的开阔天空!我要自己打拼.
Excel对象包含:
(1) Application对象。Application对象处于Excel对象层次结构的顶层,表示Excel自身的运行环境。
(2) Workbook对象。Workbook对象直接地处于Application对象的下层,表示一个Excel工作薄文件
(3) Worksheet对象。Worksheet对象包含于Workbook对象,表示一个Excel工作表。
(4) Range对象。Range对象包含于Worksheet对象,表示Excel工作表中的一个或多个单元格
你的那个上面的代码当然不行了,还要将将Excel的COM组件转换为.NET组件才行的.在项目中打开添加引用对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 9.0 Object Library"(Office 2000),然后将其加入到项目的引用中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。你是将数据读如了dt表吧?我把你的方法改了一下,你试试?公司里的没有VS环境,只能给你写大概代码,如果不行再说!
using Excel;
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
Excel.Application excel = new Excel.Application ();//引用Excel对象
excel.Application.Workbooks.Add ( true );//引用Excel工作簿
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];
//取得sheet1
Excel.Range range;
string FileName = saveFileDialog1.FileName;
outfilepath = FileName;
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int m = 0; m < dt.Columns.Count; m++)
{
strLine = strLine + dt.Columns[m].ColumnName.ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
if(excel==null)
{MessageBox.Show ("Can't open Excel!");return;}
//写入字段
for(int i=0;i<dt.Columns.Count;i++)
{
worksheet.Cells[1,i+1]=dt.Columns[i].ColumnName;
range=(Excel.Range)worksheet.Cells[1,i+1];
}
//写入标题
for(int r=0;r<dt.Rows.Count;r++)
{
worksheet.Cells[r+2,1]=r+1;
for(int i=0;i<dt.Columns.Count;i++)
{
//worksheet.Cells[r+2,i+1]=dt.Rows[r][i];
if(i+1!=dt.Columns.Count)
worksheet.Cells[r+2,i+2]=dt.Rows[r][i+1];
}
}
range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells [dt.Rows.Count+2,dt.Columns.Count]);
workbook.Saved =true;
objStreamWriter.WriteLine(strLine);
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
}
中间那段我是参考方法4的代码,如果有错我再看哈!因为没有VS03环境无法给你调试好,抱歉!
昨晚忙了好一哈哈儿,才搞定的。下面是一段SQLSERVER导出到EXCEL的代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data .SqlClient ;
//using Excel;
namespace 导出数据
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 128);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(232, 166);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel=new Excel .ApplicationClass ();
excel.Application .Workbooks .Add (true);
excel.Visible =true;
SqlConnection con=new SqlConnection ();
con.ConnectionString ="workstation id=localhost;integrated security=SSPI;database=yun";
SqlCommand comd=new SqlCommand();
comd.CommandText ="select * from TABLE1";
comd.Connection =con;
con.Open ();
SqlDataReader read;
read=comd.ExecuteReader ();
int row=2,col;
for(col=0;col<read.FieldCount ;col++)
{
// excel.cells[1,col+1]=read.GetName (col);
excel.Cells[ 1 , col+1 ] = read.GetName (col);
}
//得到标题
while (read.Read ())
{
for(col =0;col<read.FieldCount;col++)
excel.Cells [row,col+1]=read.GetValue(col).ToString();
row++;
}
//取得数据
excel.Visible = true ;
//显示Excel内容
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
//using Excel;
namespace output_excel
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "打开";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 150);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel=new Excel .ApplicationClass ();
excel.Workbooks .Add (true);
System.Data.DataTable dt=new System.Data.DataTable();
FileStream MyStream;
string s,filename;
filename=this.openFileDialog1 .FileName ;
MyStream=new FileStream (filename,FileMode.Open );
//有问题,我都想了好久了.就这儿.怎么样得到filename的路径?
//要是有好方法交流一哈!
StreamReader read;
read=new StreamReader (MyStream);
s=read.ReadLine ();
while(s!=null)
{
for(int row=1;row<dt.Rows .Count ;row++)
{
for(int col =1;col<dt.Columns .Count ;col++)
excel.Cells [row,col]=s;
}
}
excel.Visible =true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
=============================================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
//using Excel;
namespace output_excel
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "打开";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 150);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel=new Excel .ApplicationClass ();
excel.Workbooks .Add (true);
System.Data.DataTable dt=new System.Data.DataTable();
FileStream MyStream;
string s,filename;
filename=this.openFileDialog1 .FileName ;
// MyStream=new FileStream (filename,FileMode.Open );
//有问题,我都想了好久了.就这儿.怎么样得到filename的路径?
//要是有好方法交流一哈!
StreamReader read;
read=new StreamReader (MyStream);
s=read.ReadLine ();
while(s!=null)
{
for(int row=1;row<dt.Rows .Count ;row++)
{
for(int col =1;col<dt.Columns .Count ;col++)
excel.Cells [row,col]=s;
}
}
excel.Visible =true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
这段代码是你的那段,有点问题的。
我已经经过改造,现在完全可以运行了,最近太忙了,没有时间来论坛,看到你这么认真我真的很感动,现在给你看:
public void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string FileName = saveFileDialog1.FileName;
long totalCount = dt.Rows.Count;
long totalCount1 = dt.Columns.Count;
long rowRead = 0;
int percent = 0;
Form6 frm = new Form6();
frm.Show();
Excel.Application xlApp = null;
xlApp = new Excel.Application();
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
//取得sheet1
Excel.Range range;
//写入字段
for (int i = 0; i < totalCount1; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = (Excel.Range)worksheet.Cells[1, i + 1];
}
for (int r = 0; r < totalCount; r++)
{
for (int i = 0; i < totalCount1; i++)
{
worksheet.Cells[r+2,i+1]=dt.Rows[r][i];
if (i + 1 != totalCount1)
worksheet.Cells[r + 2, i + 2] = dt.Rows[r][i + 1];
}
rowRead++;
percent = (int)((100 * rowRead) / totalCount);
string info = "正在处理导出数据,已导出[" + percent.ToString("0.00") + "%]...";
frm.SetPersent(percent,info);
System.Windows.Forms.Application.DoEvents();
}
object cell1 = worksheet.Cells[2, 1];
object cell2 = worksheet.Cells[totalCount+ 1, totalCount1.Count];
range = worksheet.get_Range(cell1, cell2);
workbook.Saved = true;
workbook.SaveCopyAs(FileName);
}
这段是已经运行成功的!你看一下!