ASP.NET中数据导入至Excel

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

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

 47InBlock.gif            catch
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
 49ExpandedSubBlockEnd.gif            }

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

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

108ExpandedSubBlockEnd.gif            }

109ExpandedSubBlockEnd.gif        }

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

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

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

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

152ExpandedSubBlockEnd.gif        #endregion

153ExpandedSubBlockEnd.gif    }

154ExpandedBlockEnd.gif}

155None.gif

转载于:https://www.cnblogs.com/linfuguo/archive/2006/03/23/357155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值