从DataTable导出Excel,并下载,删除Excel进程。

页面上加入一个button即可.
源代码如下:

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  Microsoft.Office.Interop.Excel;
None.gif
None.gif
namespace  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// TestExcel 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class TestExcel : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string DownloadPath=Server.MapPath(".");    //副本的文件夹路径。
InBlock.gif                
//副本的文件名。
InBlock.gif
                string TempFileName = DateTime.Now.ToString("yyyyMMdd"+ DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS"
InBlock.gif                
this.txtTempFileName.Text=TempFileName;
InBlock.gif                
object missing = System.Reflection.Missing.Value;
InBlock.gif                
object missing2 = System.Reflection.Missing.Value;
InBlock.gif                ApplicationClass myExcel
=new ApplicationClass();
InBlock.gif
InBlock.gif                Workbook myBook
=(Workbook)myExcel.Workbooks.Add(missing);
InBlock.gif                Worksheet curSheet 
= (Worksheet)myBook.Sheets[1];
InBlock.gif                
//设置Excel样式
InBlock.gif
                Range r1=(Range)myExcel.Cells[1,2];
InBlock.gif
InBlock.gif                Range r2
=(Range)myExcel.Cells[3,4];
InBlock.gif
InBlock.gif                r1.Font.Bold
=true;
InBlock.gif                r2.Font.Bold
=true;
InBlock.gif
InBlock.gif
InBlock.gif                
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
InBlock.gif
InBlock.gif                 System.Data.DataTable dt
=this.GetTable();
InBlock.gif                
int rc=dt.Rows.Count;
InBlock.gif                
//绘制边框
InBlock.gif
                Range rBorders=(Range)curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7+rc+1,10]);
InBlock.gif                rBorders.Borders.LineStyle
=1;
InBlock.gif                curSheet.get_Range(myExcel.Cells[
7,1],myExcel.Cells[7+rc+1,1]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
InBlock.gif
                curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7,10]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置设置上边线加粗
InBlock.gif
                curSheet.get_Range(myExcel.Cells[7+rc+1,1],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
InBlock.gif
                curSheet.get_Range(myExcel.Cells[7,10],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                
//Excel的表头信息
InBlock.gif
                myExcel.Cells[1,2]="表头信息";
InBlock.gif
InBlock.gif                myExcel.Cells[
2,2]="New Added:" + DateTime.Now.ToString();
InBlock.gif                myExcel.Cells[
3,4]="VENDOR  CODE  LIST  -  BY  PRODUCTS.";
InBlock.gif                myExcel.Cells[
4,4]="****************************************";
InBlock.gif
InBlock.gif                myExcel.Cells[
5,9]="DATE From:" + DateTime.Now.ToString();
InBlock.gif                myExcel.Cells[
6,9]="DATE To:" + DateTime.Now.ToString();
InBlock.gif
InBlock.gif                myExcel.Cells[
7,5]="PARTS SUPPLIER";
InBlock.gif
InBlock.gif                
//设置Excel表列头
InBlock.gif
                myExcel.Cells[8,1]="Item";
InBlock.gif                myExcel.Cells[
8,2]="OrgCode";
InBlock.gif                myExcel.Cells[
8,3]="VendorCode";
InBlock.gif                myExcel.Cells[
8,4]="VendorName";
InBlock.gif                myExcel.Cells[
8,5]="A";
InBlock.gif                myExcel.Cells[
8,6]="B";
InBlock.gif                myExcel.Cells[
8,7]="C";
InBlock.gif                myExcel.Cells[
8,8]="PayMentType";
InBlock.gif                myExcel.Cells[
8,9]="TermsCode";
InBlock.gif                myExcel.Cells[
8,10]="CreateTime";
InBlock.gif                
//设置表头字体风格
InBlock.gif
                curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[8,10]).Font.Bold=true;
InBlock.gif                
int j=1;//j为总结的Item数目的变量
InBlock.gif
                int i=9;
InBlock.gif                
while (i-8<=dt.Rows.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myExcel.Cells[i,
1]=j.ToString();
InBlock.gif                    myExcel.Cells[i,
2]=dt.Rows[i-9]["Name"].ToString().Trim();
InBlock.gif                    myExcel.Cells[i,
3]=dt.Rows[i-9]["cost"].ToString().Trim();
InBlock.gif                    myExcel.Cells[i,
4]=dt.Rows[i-9]["bug"].ToString().Trim();
InBlock.gif                    myExcel.Cells[i,
5]="";
InBlock.gif                    myExcel.Cells[i,
6]="";
InBlock.gif                    myExcel.Cells[i,
7]="";
InBlock.gif                    
InBlock.gif                    
//设置颜色,否则日期显示成"######"格式。
InBlock.gif
                    Range rCol10=(Range)myExcel.Cells[i,10];
InBlock.gif                    rCol10
=null;
InBlock.gif                    
//从1开始循环
InBlock.gif
                    j++;
InBlock.gif                    i
++;
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
InBlock.gif                myBook.Saved
=true;
InBlock.gif                myBook.SaveAs(DownloadFilePath,missing2,
"","",false,false,XlSaveAsAccessMode.xlNoChange,1,false,missing,missing,missing);
InBlock.gif
InBlock.gif                myBook.Close(
falsenull,null);
InBlock.gif                myExcel.Quit();
InBlock.gif                System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
InBlock.gif                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
InBlock.gif                myBook 
= null;
InBlock.gif                myExcel 
= null;
InBlock.gif                GC.Collect();
InBlock.gif                
//下载文件
InBlock.gif
                HttpResponse response = HttpContext.Current.Response; 
InBlock.gif                response.Clear();
InBlock.gif                response.WriteFile(DownloadFilePath);
InBlock.gif                
string httpHeader="attachment;filename=backup.Xls";
InBlock.gif                response.AppendHeader(
"Content-Disposition", httpHeader);
InBlock.gif                response.Flush();
InBlock.gif                
//删除临时文件
InBlock.gif
                System.IO.File.Delete(DownloadFilePath);
InBlock.gif                
InBlock.gif                killExcelProcess();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception Ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw Ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除Excel进程
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private void  killExcelProcess()dot.gif{
InBlock.gif            
//结束 Excel 进程
ExpandedSubBlockStart.gifContractedSubBlock.gif
            foreach(System.Diagnostics.Process xlProcess in System.Diagnostics.Process.GetProcesses())dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if( xlProcess.ProcessName.ToUpper().Equals("EXCEL")) dot.gif{
InBlock.gif                    
//结束 excel 进程 
InBlock.gif
                    xlProcess.Kill();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif                
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构建临时DataTable
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private System.Data.DataTable GetTable()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Data.DataTable dt 
= new System.Data.DataTable();
InBlock.gif            dt.Columns.Add(
"Name");
InBlock.gif            dt.Columns.Add(
"cost");
InBlock.gif            dt.Columns.Add(
"bug");
InBlock.gif            DataRow rw 
= dt.NewRow();
InBlock.gif            rw[
"Name"]= "";
InBlock.gif            rw[
"Cost"]= "12";
InBlock.gif            rw[
"bug"]= "5";
InBlock.gif            dt.Rows.Add(rw);
InBlock.gif            rw 
= dt.NewRow();
InBlock.gif            rw[
"Name"]= "";
InBlock.gif            rw[
"Cost"]= "15";
InBlock.gif            rw[
"bug"]= "2";
InBlock.gif            dt.Rows.Add(rw);
InBlock.gif            rw 
= dt.NewRow();
InBlock.gif            rw[
"Name"]= "";
InBlock.gif            rw[
"Cost"]= "8";
InBlock.gif            rw[
"bug"]= "1";
InBlock.gif            dt.Rows.Add(rw);
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

转载于:https://www.cnblogs.com/echo/archive/2005/05/24/161461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值