在线等了!如何导出为Excel文件? http://search.csdn.net/Expert/topic/2382/2382525.xml?temp=.3107721 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先使用Tlbimp这个工具将Excel9.0的对象库文件Excel9.olb转换成为dll,这样才能做为.Net平台Assembly来使用: TlbImp Excel9.olb Excel.dll 只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。"Excel9.olb"文件在安装office2000后就在你的硬盘上了,在项目中引用Excel.dll -------------------------Export(){...//这样保证Excel完全退出ex();GC.Collect();}void ex(){Excel.Application myExcel = new Excel.Application() ;myExcel.Visible = false ;myExcel.Application.Workbooks.Add(true);myExcel.Cells[1,1]="aaaa";myExcel.Cells[1,2]="bbbbbb";....myExcel.DisplayAlerts = false;myExcel.ActiveWorkbook.SaveAs("文件名",Excel.XlFileFormat.xlWorkbookNormal,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);myExcel.Quit();} private void Button1_Click(object sender, System.EventArgs e){ //写入Excel的方法: //定义需要参数。 string SourceFile="Data.XLS"; //源文件名称。 string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。 string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。 //副本的文件名。 string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS"; object missing = System.Reflection.Missing.Value; Excel.Application myExcel=new Excel.Application(); //打开新文件 myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing, missing,missing,missing,missing,missing,missing, missing,missing); Excel.Workbook myBook=myExcel.Workbooks[1]; Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2]; string DownloadFilePath=DownloadPath+"\\"+TempFileName; int i=0; while (i<=10) { myExcel.Cells[4+i,2]=i.ToString(); myExcel.Cells[4+i,3]=i.ToString(); myExcel.Cells[4+i,4]=i.ToString(); myExcel.Cells[4+i,5]=i.ToString(); myExcel.Cells[4+i,6]=i.ToString(); i++; } myBook.Saved=true; //myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing); myBook.PrintPreview(0); //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing); myBook.Close(false, null,null); myExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); myBook = null; myExcel = null; GC.Collect(); //Response.Redirect("ExcelDownload//"+TempFileName); //下载文件} public void ToExcel(System.Web.UI.WebControls.DataGrid ctl) { HttpContext.Current.Response.Charset =""; HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=money.xls"); HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword System.IO.StringWriter tw = new System.IO.StringWriter() ; System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw);Response.End();} 用下面的两个函数! #region 导出到EXCEL /************************************************************************************/ /* 导出到EXCEL表 /************************************************************************************/ // private void toExcel() { ExcelApplication myExcel = new ExcelApplication(); string curPath = System.Environment.CurrentDirectory; curPath = curPath + @"\Excel\非员工查询结果.xls"; if (File.Exists(curPath)) { DialogResult dialogResult = MessageBox.Show("已经存在文件\n" + curPath.Trim() + "\n!" + "\n是否将当前的EXCEL文档覆盖?", "文件存在", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (dialogResult == DialogResult.Yes) { try { File.Delete(curPath); } catch { MessageBox.Show("文件操作出错,可能正在运行当前的EXCEL文件!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error); return; } } else { return; } } try { myExcel.Workbooks.Add(Missing.Value); Worksheet worksheet = (Worksheet)myExcel.ActiveSheet; worksheet.Name = "非员工查询结果"; myExcel.Visible = false; int iRows = dataSet.Tables["queryPerson"].Rows.Count; int iCol = dataSet.Tables["queryPerson"].Columns.Count; for( int i=0; i<iCol; i++) { worksheet.Cells[1,i+1] = dataSet.Tables["queryPerson"].Columns[i].ToString(); } for(int i=0; i<iRows; i++) { for(int j=0; j<iCol; j++) { worksheet.Cells[i+2,j+1] = dataSet.Tables["queryPerson"].Rows[i][j].ToString(); } } worksheet.SaveAs( curPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } catch(Exception ex) { MessageBox.Show(ex.Message); return; } finally { DialogResult dialogResult = MessageBox.Show("已经成功将当前数据保存到\n" + curPath.Trim() + "\n文件中!" + "\n是否立即在EXCEL中打开该文档?", "保存成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (dialogResult == DialogResult.Yes) { myExcel.Visible = true; } else { myExcel.Quit(); } } GC.Collect(); } /************************************************************************************/ /* 因为调用EXCEL后,结束时EXCEL进程并没有立即KILL,用这个方法调用可以KILL掉EXCEL /************************************************************************************/ // public void callToExcel() { toExcel(); GC.Collect(); } #endregion 有Api的C#例子吗 急,传参问题 求助,继续昨天的问题啊~~~ 请教有关DataGird的问题。 A.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2"); 程序使用(*.sdf)本地数据库,在别的机子上运行需要安装哪些组件? C#与MySQL有嘛子关系吗? 分页实现中,每次删除、新增数据,当前页面如何刷新呢? 如何连接oracle数据库 C# 怎么保存换行 C#中怎样实现对VC编译成的.DLL文件的调用? c#父窗体中的控件和父窗体的子窗体的显示问题!
TlbImp Excel9.olb Excel.dll
只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。
"Excel9.olb"文件在安装office2000后就在你的硬盘上了,在项目中引用Excel.dll -------------------------
Export()
{
...
//这样保证Excel完全退出
ex();
GC.Collect();}
void ex()
{
Excel.Application myExcel = new Excel.Application() ;
myExcel.Visible = false ;
myExcel.Application.Workbooks.Add(true);myExcel.Cells[1,1]="aaaa";
myExcel.Cells[1,2]="bbbbbb";
....
myExcel.DisplayAlerts = false;
myExcel.ActiveWorkbook.SaveAs("文件名",Excel.XlFileFormat.xlWorkbookNormal,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
myExcel.Quit();
}
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}
{
HttpContext.Current.Response.Charset ="";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=money.xls");
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw);
Response.End();
}
/* 导出到EXCEL表
/************************************************************************************/
//
private void toExcel()
{
ExcelApplication myExcel = new ExcelApplication();
string curPath = System.Environment.CurrentDirectory;
curPath = curPath + @"\Excel\非员工查询结果.xls"; if (File.Exists(curPath))
{
DialogResult dialogResult = MessageBox.Show("已经存在文件\n" + curPath.Trim() + "\n!"
+ "\n是否将当前的EXCEL文档覆盖?", "文件存在",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dialogResult == DialogResult.Yes)
{
try
{
File.Delete(curPath);
}
catch
{
MessageBox.Show("文件操作出错,可能正在运行当前的EXCEL文件!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
else
{
return;
}
}
try
{
myExcel.Workbooks.Add(Missing.Value);
Worksheet worksheet = (Worksheet)myExcel.ActiveSheet;
worksheet.Name = "非员工查询结果";
myExcel.Visible = false; int iRows = dataSet.Tables["queryPerson"].Rows.Count;
int iCol = dataSet.Tables["queryPerson"].Columns.Count; for( int i=0; i<iCol; i++)
{
worksheet.Cells[1,i+1] = dataSet.Tables["queryPerson"].Columns[i].ToString();
} for(int i=0; i<iRows; i++)
{
for(int j=0; j<iCol; j++)
{
worksheet.Cells[i+2,j+1] = dataSet.Tables["queryPerson"].Rows[i][j].ToString();
}
}
worksheet.SaveAs( curPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
finally
{
DialogResult dialogResult = MessageBox.Show("已经成功将当前数据保存到\n" + curPath.Trim() + "\n文件中!"
+ "\n是否立即在EXCEL中打开该文档?", "保存成功",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dialogResult == DialogResult.Yes)
{
myExcel.Visible = true;
}
else
{
myExcel.Quit();
}
}
GC.Collect();
} /************************************************************************************/
/* 因为调用EXCEL后,结束时EXCEL进程并没有立即KILL,用这个方法调用可以KILL掉EXCEL
/************************************************************************************/
//
public void callToExcel()
{
toExcel();
GC.Collect();
}
#endregion