| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2449 人关注过本帖
标题:[求助]EXCEL导出的文件格式问题
只看楼主 加入收藏
哭泣的百合
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-7-13
收藏
得分:0 

有没有好消息!我现在真的是没有办法了,真是郁闷呀.郁闷!


我就不信温室里的花到户外就不能绽放,笼中的贵鸟就飞不到自己的开阔天空!我要自己打拼.
2007-07-23 09:08
狼度
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-7-20
收藏
得分:0 

你的问题帮不了你
帮你顶下吧


己所不欲 勿施于人
2007-07-23 09:25
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
以下是引用哭泣的百合在2007-7-20 8:49:36的发言:

非常感谢您的支持,我已经尝试过上述的方法4,但是总是有错误这段代码运行出现以下错误,Exception from HRESULT: 0x800A03EC,我也不知道他用的是那个版本,是9.0还是,我用的是11.0的

方法四我这儿运行正常哑


Flying without wings
2007-07-23 16:17
哭泣的百合
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-7-13
收藏
得分:0 

呵呵!这个问题我解决了,是因为表的传输问题,问楼上的,你那个是不是速度特别慢呀?我这里简直是超慢!效率太低,写一个13*800=10400个单元格,估计要等个5分中左右才能做完,你那边的速度怎么样呀?


我就不信温室里的花到户外就不能绽放,笼中的贵鸟就飞不到自己的开阔天空!我要自己打拼.
2007-07-23 16:37
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
呵呵,这个我倒是没去验证哈

楼上的MM,是因为表的什么传输问题哑,告诉大伙,长长见识哈

Flying without wings
2007-07-24 10:38
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

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环境无法给你调试好,抱歉!


真的想象风一样去流浪!
2007-07-24 17:45
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 
借了个优盘回切好好的研究一下,哎!忧闷的很啊,我住的地方不能上网.

真的想象风一样去流浪!
2007-07-24 17:49
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

昨晚忙了好一哈哈儿,才搞定的。下面是一段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内容
}


}
}


真的想象风一样去流浪!
2007-07-25 08:35
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

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)
{

}
}
}
这段代码是你的那段,有点问题的。


真的想象风一样去流浪!
2007-07-25 08:40
哭泣的百合
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-7-13
收藏
得分:0 

我已经经过改造,现在完全可以运行了,最近太忙了,没有时间来论坛,看到你这么认真我真的很感动,现在给你看:
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);
}
这段是已经运行成功的!你看一下!


我就不信温室里的花到户外就不能绽放,笼中的贵鸟就飞不到自己的开阔天空!我要自己打拼.
2007-07-27 09:26
快速回复:[求助]EXCEL导出的文件格式问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016891 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved