ASP.NET中数据导入至Excel (转)

把asp.net中的数据直接导入至excel表中的例子。
1.web.config中加入
 <identity impersonate="true"/>
2.用Microsoft.Excel 11.0 Object Library COM组件
3.下面代码。
  1 using  System;
  2 using  System.Collections;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Drawing;
  6 using  System.Web;
  7 using  System.Web.SessionState;
  8 using  System.Web.UI;
  9 using  System.Web.UI.WebControls;
 10 using  System.Web.UI.HtmlControls;
 11 using  System.Data.SqlClient;
 12 using  System.Configuration;
 13 using  System.Runtime.InteropServices;
 14 using  Excel;
 15 using  System.Reflection;
 16
 17 namespace  CSharpNET_ExcelTest
 18 ExpandedBlockStart.gifContractedBlock.gif {
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 20    /// WebForm1 的摘要说明。
 21    /// </summary>

 22    public class WebForm1 : System.Web.UI.Page
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 24        private void Page_Load(object sender, System.EventArgs e) 
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        
 26            Excel.Application oExcel=new Excel.Application();
 27            Excel.Workbooks oBooks;
 28            Excel.Workbook oBook;
 29            Excel.Sheets oSheets;
 30            Excel.Worksheet oSheet;
 31            Excel.Range oCells;
 32            string sFile,sTemplate;
 33            DataSet objDataset=new DataSet();
 34
 35            System.Data.DataTable dt = ((System.Data.DataTable)(CreateDataSource().Tables[0])); 
 36
 37            sFile=Server.MapPath(Request.ApplicationPath)+@"\MyExcel.xls";
 38            sTemplate=Server.MapPath(Request.ApplicationPath) +@"\MyTemplate.xls";
 39            oExcel.DisplayAlerts=false;
 40            oBooks=oExcel.Workbooks; 
 41 
 42            try
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 44                // 在VB.net中一般用open
 45                oBooks.Add(Server.MapPath(Request.ApplicationPath) + @"\MyTemplate.xls") ;
 46            }

 47            catch
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            
 49            }

 50
 51            oBook=oBooks[1];
 52            oSheets=oBook.Worksheets;
 53            oSheet=(Excel.Worksheet) oSheets[1];
 54
 55            oBook = oBooks[1]; 
 56            oSheet.Name="First Sheet";
 57            oCells=oSheet.Cells;
 58            DumpData(dt,oCells);
 59
 60            // 注意此处为10个参数
 61            oSheet.SaveAs(sFile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value, 
 62                Excel.XlSaveAsAccessMode.xlExclusive,Missing.Value,Missing.Value, 
 63                Missing.Value); 
 64
 65            oBook.Close(Missing.Value,Missing.Value,Missing.Value);
 66
 67            oExcel.Quit();
 68            Marshal.ReleaseComObject(oCells);
 69            Marshal.ReleaseComObject(oSheet);
 70 
 71            Marshal.ReleaseComObject(oSheets) ;
 72            Marshal.ReleaseComObject(oBook);
 73            Marshal.ReleaseComObject(oBooks);
 74            Marshal.ReleaseComObject(oExcel);
 75 
 76            oExcel = null;
 77            oBooks = null;
 78            oBook = null;
 79 
 80            oSheets = null;
 81            oSheet = null ;
 82            oCells = null;
 83            System.GC.Collect();
 84            Response.Redirect(sFile); 
 85
 86        }
 
 87
 88        private void DumpData(System.Data.DataTable dt,Excel.Range oCells)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 90            int iCol;
 91            int iRow; 
 92            DataRow dr;
 93            object[] ary;
 94            for (iCol=0;iCol<dt.Columns.Count;iCol++)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 96                oCells[2,iCol+1]=dt.Columns[iCol].ToString();
 97            }

 98
 99            for (iRow=0 ;iRow<dt.Rows.Count;iRow++)
100ExpandedSubBlockStart.gifContractedSubBlock.gif            {
101                dr=dt.Rows[iRow];
102                ary=dr.ItemArray;
103                for(iCol=0 ;iCol<ary.GetUpperBound(0);iCol++)
104ExpandedSubBlockStart.gifContractedSubBlock.gif                {
105                    oCells[iRow+3,iCol+1]=ary[iCol].ToString();
106                    Response.Write(ary[iCol].ToString()+"\t");
107                }

108            }

109        }

110
111        public System.Data.DataSet CreateDataSource() 
112ExpandedSubBlockStart.gifContractedSubBlock.gif        
113            System.Data.DataSet ds = new System.Data.DataSet(); 
114            System.Data.DataTable dt = new System.Data.DataTable(); 
115            System.Data.DataRow dr; 
116            dt.Columns.Add(new DataColumn("身份证号码"typeof(string))); 
117            dt.Columns.Add(new DataColumn("图书单价"typeof(float))); 
118            dt.Columns.Add(new DataColumn("购买数量"typeof(Int32))); 
119            dt.Columns.Add(new DataColumn("总价格"typeof(float))); 
120            for (Int32 i = 0; i <= 10; i++
121ExpandedSubBlockStart.gifContractedSubBlock.gif            
122                dr = dt.NewRow(); 
123                dr[0= "123456789123456789"
124                dr[1= 100 * i / 3
125                dr[2= i + 5
126                dr[3= (float)dr[1* (Int32) dr[2]; 
127                dt.Rows.Add(dr); 
128            }
 
129            ds.Tables.Add(dt); 
130            return ds; 
131        }

132
133
134ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
135        override protected void OnInit(EventArgs e)
136ExpandedSubBlockStart.gifContractedSubBlock.gif        {
137            //
138            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
139            //
140            InitializeComponent();
141            base.OnInit(e);
142        }

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

148        private void InitializeComponent()
149ExpandedSubBlockStart.gifContractedSubBlock.gif        {    
150            this.Load += new System.EventHandler(this.Page_Load);
151        }

152        #endregion

153    }

154}

转载于:https://www.cnblogs.com/zpylh/articles/1281022.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值