IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置: 首頁(yè)網(wǎng)絡(luò)編程.Net編程 → asp.net簡(jiǎn)單實(shí)現(xiàn)導(dǎo)出excel報(bào)表

asp.net簡(jiǎn)單實(shí)現(xiàn)導(dǎo)出excel報(bào)表

時(shí)間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)

  關(guān)于導(dǎo)出excel報(bào)表,網(wǎng)上也是一搜一大把。整理一下,無非就是幾種思路,有利用安裝excel軟件或插件的服務(wù)器直接生成,或者直接在客戶端生成(通常都是利用excel軟件或插件直接在瀏覽器生成)。反正萬變不離其宗,離開excel插件,這個(gè)活你還真的干不了,由此你可以看到軟件公司尤其是微軟的強(qiáng)大。下面貼一個(gè)比較簡(jiǎn)單的導(dǎo)出excel報(bào)表的方法。在安裝了office2003的機(jī)器上,通過ie瀏覽器可以成功生成excel,而且一直有人在使用。如果你在測(cè)試的時(shí)候發(fā)現(xiàn)這個(gè)根本無法使用,請(qǐng)注意,這個(gè)很可能和你的機(jī)器配置有關(guān),別懷疑代碼的正確性。下面就一個(gè)利用iBatis開發(fā)的例子來簡(jiǎn)單說明一下。

  1、實(shí)體類

  Code

  using System;

  using System.Collections.Generic;

  using System.Text;

  #region Apache Notice

  /*****************************************************************************

  * $Header: $

  * $Revision: 383115 $

  * $Date: 2006-04-15 13:21:51 +0800 (星期六, 04 三月 2006) $

  *

  * IBatisNetDemo

  * Copyright (C) 2006 - Shanyou Zhang

  *

  * Licensed under the Apache License, Version 2.0 (the "License");

  * you may not use this file except in compliance with the License.

  * You may obtain a copy of the License at

  *

  *????? http://www.apache.org/licenses/LICENSE-2.0

  *

  * Unless required by applicable law or agreed to in writing, software

  * distributed under the License is distributed on an "AS IS" BASIS,

  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  * See the License for the specific language governing permissions and

  * limitations under the License.

  *

  ********************************************************************************/

  #endregion

  namespace IBatisNetDemo.Domain

  {

  [Serializable]

  public class Person

  {

  private int id;

  private string firstName;

  private string lastName;

  private DateTime? birthDate;

  private double? weightInKilograms;

  private double? heightInMeters;

  public Person() { }

  public int Id

  {

  get { return id; }

  set { id = value; }

  }

  public string FirstName

  {

  get { return firstName; }

  set { firstName = value; }

  }

  public string LastName

  {

  get { return lastName; }

  set { lastName = value; }

  }

  public DateTime? BirthDate

  {

  get { return birthDate; }

  set { birthDate = value; }

  }

  public double? WeightInKilograms

  {

  get { return weightInKilograms; }

  set { weightInKilograms = value; }

  }

  public double? HeightInMeters

  {

  get { return heightInMeters; }

  set { heightInMeters = value; }

  }

  }

  }

  2、導(dǎo)出excel報(bào)表主程序方法

  Code

  using System;

  using System.Collections.Generic;

  using System.Text;

  using System.Web;

  using System.Web.UI;

  using System.IO;

  using System.Reflection;

#p#副標(biāo)題#e#

  namespace DotNet.Common.Util

  {

  ///

  /// 導(dǎo)出excel 簡(jiǎn)單實(shí)現(xiàn)

  ///

  public static class ExcelUtil

  {

  private static Page currentPage = HttpContext.Current.Handler? as Page;

  private static Object sycObj = new Object();

  private static int incremental = 10;

  ///

  /// 按照時(shí)間生成excel名稱 防止生成相同名的excel造成文件覆蓋

  ///

  ///

  private static string CreateExcelName()

  {

  lock (sycObj)

  {

  incremental = incremental + 1;

  if (incremental > 99)

  incremental = 10;

  return Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHHmmssfff") + incremental).ToString();

  }

  }

  ///

  /// 導(dǎo)出excel

  ///

  /// 泛型實(shí)體

  ///

  /// 要顯示的列名

  /// 要顯示的導(dǎo)出屬性名? 和實(shí)體的屬性名有關(guān),順序由顯示的列確定 可以同listColumes

  /// 實(shí)體集合

  public static void ExportExcel(HttpResponse response, IList listColumns, IList listProperty, IList listModel) where T : class, new()

  {

  if (listColumns.Count == 0)

  {

  throw new IndexOutOfRangeException("No Columnes!");

  }

  if (listColumns.Count != listProperty.Count)

  {

  throw new ArgumentException("Columns and properties length are not equal.");

  }

  string sheetName = "sheetName";

  using (StringWriter writer = new StringWriter())

  {

  writer.WriteLine("");

  writer.WriteLine("");

  writer.WriteLine("");

  writer.WriteLine("");

  writer.WriteLine("");

  writer.WriteLine("

");

  writer.WriteLine("

");

  foreach (string item in listColumns)

  {

  writer.WriteLine("

"); //列名

  }

  writer.WriteLine("

");

  //通過反射 顯示要顯示的列

  BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;//反射標(biāo)識(shí)

  Type objType = typeof(T);

  PropertyInfo[] propInfoArr = objType.GetProperties(bf);

  foreach (T model in listModel)

  {

  writer.WriteLine("

");

  foreach (PropertyInfo propInf

關(guān)鍵詞標(biāo)簽:asp.net,excel報(bào)表

相關(guān)閱讀

文章評(píng)論
發(fā)表評(píng)論

熱門文章 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 鐘離圣遺物推薦-原神鐘離圣遺物詞條 鐘離圣遺物推薦-原神鐘離圣遺物詞條 解決方法:應(yīng)用程序“DEFAULT WEB SITE”中的服務(wù)器錯(cuò)誤 解決方法:應(yīng)用程序“DEFAULT WEB SITE”中的服務(wù)器錯(cuò)誤 使用aspnet_regiis.exe 重新注冊(cè).NET Framework 使用aspnet_regiis.exe 重新注冊(cè).NET Framework

相關(guān)下載

    人氣排行 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 asp.net表單提交方法GET\POST 在ASP.NET中如何判斷用戶IE瀏覽器的版本 Asp.net中messagebox的實(shí)現(xiàn)方法 Asp.net中的web.config配置 在ASP.NET MVC中實(shí)現(xiàn)大文件異步上傳 asp.net獲取URL和IP地址 FileUpload上傳多文件出現(xiàn)錯(cuò)誤的解決方法

    " + item + "